# conversion from polynomial to symbolic

I want to convert a huge polynomial from ZZ[] into SR but with the following code I get an expression that is not expanded:

sage: R.<x> = ZZ[]
sage: var('n')
n

sage: p=16*x^5 - 20*x^3 + 5*x
sage: p.subs(x=n)
(4*(4*n^2 - 5)*n^2 + 5)*n
sage: SR(p)
(4*(4*x^2 - 5)*x^2 + 5)*x


Is there a way to get the result in expanded form without using expand()? Presumably the unnecessary grouping and subsequent expansion can take some time with huge polynomials, so I would like to prevent this from the start.

edit retag close merge delete

Sort by ยป oldest newest most voted

You can construct the symbolic polynomial from the list of coefficients of the algebraic one as follows:

sage: sum([b*n^a for (a,b) in enumerate(p)])
16*n^5 - 20*n^3 + 5*n

more

That is indeed faster than SR(p).expand() so I'll accept the answer, many thanks.

( 2014-08-19 00:48:08 -0600 )edit

If all you care about is the string you can do str(p).replace('x','n').

sage: R.<x> = ZZ[]
sage: p = 16*x^5 - 20*x^3 + 5*x
sage: p
16*x^5 - 20*x^3 + 5*x
sage: str(p).replace('x','n')
'16*n^5 - 20*n^3 + 5*n'

more