ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 21 Aug 2014 10:04:30 -0500conversion from polynomial to symbolichttp://ask.sagemath.org/question/23818/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.Mon, 18 Aug 2014 09:39:16 -0500http://ask.sagemath.org/question/23818/conversion-from-polynomial-to-symbolic/Answer by tmonteil for <p>I want to convert a huge polynomial from ZZ[] into SR but with the following code I get an expression that is not expanded:</p>
<pre><code>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
</code></pre>
<p>Is there a way to get the result in expanded form without using <code>expand()</code>? Presumably the unnecessary grouping and subsequent expansion can take some time with huge polynomials, so I would like to prevent this from the start.</p>
http://ask.sagemath.org/question/23818/conversion-from-polynomial-to-symbolic/?answer=23822#post-id-23822You 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
Mon, 18 Aug 2014 16:29:12 -0500http://ask.sagemath.org/question/23818/conversion-from-polynomial-to-symbolic/?answer=23822#post-id-23822Comment by rws for <p>You can construct the symbolic polynomial from the list of coefficients of the algebraic one as follows:</p>
<pre><code>sage: sum([b*n^a for (a,b) in enumerate(p)])
16*n^5 - 20*n^3 + 5*n
</code></pre>
http://ask.sagemath.org/question/23818/conversion-from-polynomial-to-symbolic/?comment=23828#post-id-23828That is indeed faster than SR(p).expand() so I'll accept the answer, many thanks.Tue, 19 Aug 2014 00:48:08 -0500http://ask.sagemath.org/question/23818/conversion-from-polynomial-to-symbolic/?comment=23828#post-id-23828Answer by slelievre for <p>I want to convert a huge polynomial from ZZ[] into SR but with the following code I get an expression that is not expanded:</p>
<pre><code>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
</code></pre>
<p>Is there a way to get the result in expanded form without using <code>expand()</code>? Presumably the unnecessary grouping and subsequent expansion can take some time with huge polynomials, so I would like to prevent this from the start.</p>
http://ask.sagemath.org/question/23818/conversion-from-polynomial-to-symbolic/?answer=23886#post-id-23886If 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'
Thu, 21 Aug 2014 10:04:30 -0500http://ask.sagemath.org/question/23818/conversion-from-polynomial-to-symbolic/?answer=23886#post-id-23886