Numerical approximation of symbolic coefficients

90 views
Skip to first unread message

Michael Orlitzky

unread,
Feb 5, 2012, 11:09:47 PM2/5/12
to sage-support
I've got these polynomials in two variables, `x`, and `u`. The
polynomials are low degree (eight at the moment), but I'm working
symbolically, so they print exactly:

..+ 314069483520)*sqrt(3) - 80295755776*x + 4831838208)/(1953125*x^63
- 73828125*x^61...

All I would really like is to see these displayed with approximate
coefficients, and to compute their roots.

First attempt: loop through each term and try to n() the coefficient.
Madness.

Second attempt: leave SR and work in `Polynomial`s over RR. This works
for display purposes, but once I have an MPolynomial_polydict object, I
can't figure out how to get the roots (potentially in either variable).

I can go back to SR, take the roots, and then.. go back to Polynomials?
Is there a better way to go about it? I feel like I'm reinventing the wheel.

D. S. McNeil

unread,
Feb 5, 2012, 11:23:59 PM2/5/12
to sage-s...@googlegroups.com
> First attempt: loop through each term and try to n() the coefficient.
> Madness.

Based on a suggestion Mike Hansen once gave me --
http://ask.sagemath.org/question/411/substituting-expressions-for-numbers
-- I tend to use subclasses of Converter when I need to do something
like this, so as not to get lost in the madness. :^) Something like:


from sage.symbolic.expression_conversions import Converter

class Evaluator(Converter):
def arithmetic(self, ex, operator):
return reduce(operator, map(self, ex.operands()))
def pyobject(self, ex, obj):
return ex.n()

sage: E = Evaluator()
sage: var("u x")
(u, x)
sage: q = ((314069483520)*sqrt(3/(sin(u+2)))*u - 80295755776*x +
4831838208)/(1953125*x^63)
sage: q
33554432/1953125*(9360*sqrt(3)*u*sqrt(1/sin(u + 2)) - 2393*x + 144)/x^63
sage: E(q)
17.179869183999998*(16211.99555884469*u*(1/sin(u + 2.0))^0.5 -
2393.0*x + 144.0)/x^63.0

Doug

Michael Orlitzky

unread,
Feb 6, 2012, 12:33:29 AM2/6/12
to sage-s...@googlegroups.com
On 02/05/2012 11:23 PM, D. S. McNeil wrote:
>> First attempt: loop through each term and try to n() the coefficient.
>> Madness.
>
> Based on a suggestion Mike Hansen once gave me --
> http://ask.sagemath.org/question/411/substituting-expressions-for-numbers
> -- I tend to use subclasses of Converter when I need to do something
> like this, so as not to get lost in the madness. :^) Something like:
>

This is neat, thanks. It should work for other transformations, too, not
just numerical approximation.

Reply all
Reply to author
Forward
0 new messages