# Convert expression to univariate polynomial over symbolic ring properly

If I create polynomial like this

R.<x> = SR[]
var('a')
f = a*x^3+1


I get such list of variables

sage: f.variables()
(x,)


But how to convert existing expression to univariate polynomial over symbolic ring?

When I do like this

var('a')
f = a*x^3+1
P.<x> = SR[]


I get following output

sage: P(f).variables()
()
sage: SR['x'](f).variables()
()


How to convert f to polynomial in a right way with x as variable.

edit retag close merge delete

Sort by » oldest newest most voted

You could do this:

sage: a, x = var('a x')
sage: f = a * x^3 + 1
sage: P.<x> = SR[]
sage: ff = sum(c*x^j for (c,j) in f.coefficients(x))
sage: ff.parent()
Univariate Polynomial Ring in x over Symbolic Ring
sage: ff.variables()
(x,)

more

Here is a possibility:

sage: var('a x')
(a, x)
sage: f = a*x^3+1
sage: f.parent()
Symbolic Ring
sage: q = f.polynomial(SR)
sage: q
a*x^3 + 1
sage: q.parent()
Multivariate Polynomial Ring in a, x over Symbolic Ring
sage: q.variables()
(a, x)


So, at this point you got a polynomial with coefficients in SR but with both a and x as polynomial variables. Hence, it is not possible to send it into the ring R:

sage: R.<x> = SR[]
sage: R(q)
TypeError: not a constant polynomial


So, you have to substitute the polynomial variable a of q into the symbolic variable a:

sage: s = q.subs(a=SR.var('a'))
sage: s.parent()
Multivariate Polynomial Ring in a, x over Symbolic Ring
sage: P = R(s)
sage: P
a*x^3 + 1
sage: P.parent()
Univariate Polynomial Ring in x over Symbolic Ring
sage: P.variables()
(x,)

more