Ask Your Question
2

conversion from polynomial to symbolic

asked 10 years ago

rws gravatar image

updated 10 years ago

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.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 10 years ago

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
Preview: (hide)
link

Comments

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

rws gravatar imagerws ( 10 years ago )
0

answered 10 years ago

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'
Preview: (hide)
link

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: 10 years ago

Seen: 1,496 times

Last updated: Aug 21 '14