# How to sort terms in symbolic expression of polynomial?

I have a symbolic expression in the following form

s =2*z^3 + z^2 + z


How can I tell sage to print s as the following form?

z + z^2 +  2*z^3

edit retag close merge delete

Sort by » oldest newest most voted

I guess symbolic expressions do not have this feature. However, for polynomial rings, the order of the monomials has some importance (e.g. in the computation of a Groebner basis). Hence, you could transform your symbolic expression into a polynomial belonging to some polynomial ring with an appropriate ordering. Note that since ordering of monomials usually matters for multivariate polynomial rings, we have to construct a multivariate polynomial rings with one variable instead of an univeriate polynomial ring. Here is how:

sage: z = SR.var('z')
sage: s =2*z^3 + z^2 + z
sage: s
2*z^3 + z^2 + z
sage: s.parent()
Symbolic Ring

sage: R.<z>=PolynomialRing(ZZ,1,order='neglex')
sage: R(s)
z + z^2 + 2*z^3
sage: R(s).parent()
Multivariate Polynomial Ring in z over Integer Ring


To see which orders are available, you can do:

sage: sage.rings.polynomial.term_order?

more

How to convert a symbolic polynomial to a member of polynomial ring? Does sage has such a function?

The line

sage: R(s)


does exactly this. It converts the symbolic polynomial s as an element of R.