Ask Your Question
0

Can I get a polynomial type from a symbolic expression type?

asked 2015-01-13 21:45:46 +0100

ff524 gravatar image

I am doing some things to a symbolic variable C...

sage: C = var('C')
sage: # ... do a whole bunch of stuff with C as a symbolic variable,
sage: # and at some point get
sage: g = solve(f, C)
sage: print(g)
[0 == 25*C^5 - 325*C^4 + 16276*C^3 - 228488*C^2 - 5940688]
sage: # implicit solution is polynomial in C

... resulting in a polynomial in C that is typed as a symbolic expression:

sage: print g[0].rhs()
25*C^5 - 325*C^4 + 16276*C^3 - 228488*C^2 - 5940688
sage: type(g[0].rhs())
<type 'sage.symbolic.expression.Expression'>

I need to call quo_rem on this expression, so I need it to be a

<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>

I can copy and paste the output of print(g[0].rhs()) into something new and get

sage: P.<C> = PolynomialRing(QQ)
sage: p0 = 25*C^5 - 325*C^4 + 16276*C^3 - 228488*C^2 - 5940688
sage: type(p0)
<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>

and now I can call quo_rem on p0.

How can I get the polynomial type out of the symbolic expression without having to resort to copy and paste?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

answered 2015-01-13 22:53:52 +0100

FrédéricC gravatar image

Like that ?

P.<C> = PolynomialRing(QQ)
P(g[0].rhs())

A basic example:

sage: P = PolynomialRing(QQ,'x')
sage: type(x)
<type 'sage.symbolic.expression.Expression'>
sage: P(x**3+4)
x^3 + 4
sage: type(P)
<class 'sage.rings.polynomial.polynomial_ring.PolynomialRing_field_with_category'>
edit flag offensive delete link more

Comments

Yup. I didn't know about this P() syntax. Thanks a lot!

ff524 gravatar imageff524 ( 2015-01-13 23:01:15 +0100 )edit
0

answered 2015-01-16 17:01:34 +0100

rws gravatar image

It's even easier than in Frédéric's answer:

sage: C = var('C')
sage: g=25*C^5 - 325*C^4 + 16276*C^3 - 228488*C^2 - 5940688
sage: type(g)
<type 'sage.symbolic.expression.Expression'>
sage: g.polynomial(QQ)
25*C^5 - 325*C^4 + 16276*C^3 - 228488*C^2 - 5940688
sage: type(_)
<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-01-13 21:45:46 +0100

Seen: 471 times

Last updated: Jan 16 '15