Ask Your Question

conversion from polynomial to symbolic

asked 2014-08-18 16:39:16 +0200

rws gravatar image

updated 2014-08-18 16:42:33 +0200

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')

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-08-18 23:29:12 +0200

tmonteil gravatar image

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
edit flag offensive delete link more


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

rws gravatar imagerws ( 2014-08-19 07:48:08 +0200 )edit

answered 2014-08-21 17:04:30 +0200

slelievre gravatar image

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'
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


Asked: 2014-08-18 16:39:16 +0200

Seen: 1,115 times

Last updated: Aug 21 '14