Reverse order of polynomial before printing

65 views
Skip to first unread message

Thomas Ligon

unread,
Feb 10, 2024, 3:58:48 PM2/10/24
to sympy

I am trying to reverse the order of an expression before printing it for my documentation, and I want ascending order, but SymPy always gives me descending order. All of the expressions are polynomials, specifically partial sums of power series. Since, according to Internet searches, there is no easy way to do this, I have tried a number of things, including manipulating the expression tree. The original expression is always Add of a tuple, and each tuple is a rational number and x**n. When I loop through the expression and Add the components, I always get the same order. The elegant solution would be to reverse the tuple and Add it all at once, but that is deprecated and gives me a tuple instead of a sum.

Here is some sample code, a test program that demonstrates the problem.

from sympy import symbols, Rational, Add, latex, Eq
x = symbols('x')
lhs = symbols('X')
rhs = Rational(3)/Rational(4)*x**3 + Rational(2)/Rational(5)*x**2 + Rational(1)/Rational(4)*x
X1 = rhs.args
#X2 = X1[::-1] # Why don't I need this? My debugger shows the expression and the tuple in reverse order.
X2 = X1
# Try the expected order of Add. This produces a sum, but with an extra set of parantheses, and not the desired order.
X3 = X2[0]
X2R = X2[1:len(X2)]
for indT in range(0, len(X2R)):
    termT = X2R[indT]
    X3 = Add(X3, termT, evaluate=False)
# Try the other order of Add. This looks the same as X3.
X4 = X2[0]
for indT in range(0, len(X2R)):
    termT = X2R[indT]
    X4 = Add(termT, X4, evaluate=False)
# Try single step. This is deprecated, gives the correct order, but returns a tuple instead of a sum.
#X2 = X1[::-1] # Why don't I need this?
X2 = X1
X5 = Add(X2, evaluate=False)
         
print(latex(Eq(lhs, rhs))) # original order
print(latex(Eq(lhs, X3))) # still original order
print(latex(Eq(lhs, X4))) # still original order
print(latex(Eq(lhs, X5))) # desired order, but tuple instead of sum

Aaron Meurer

unread,
Feb 12, 2024, 6:46:43 PM2/12/24
to sy...@googlegroups.com
The order in printing can be controlled with the order flag to the
printer. Using latex(expr, order='rev-lex') will cause polynomials to
print in reverse lexicographic order, which is the order you want.

It's not a good idea to use evaluate=False to try to control printing
behavior, as this can break other things. Instead, you should use the
existing flags in the printer in question, or customize the printer
with a subclass if the built-in behavior doesn't meet your needs.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/166a5149-26b2-47f7-9956-943b9d379d92n%40googlegroups.com.

Thomas Ligon

unread,
Feb 13, 2024, 12:41:05 PM2/13/24
to sympy
Thanks! This does exaclty what I wanted.
Now, for the question, should I be embarassed for not finding it myself. This is in fact documented in
It looks like all of my searching was for reversing the order of an expression, which is hard, but reversing the order or printing is easy.

Aaron Meurer

unread,
Feb 13, 2024, 5:20:01 PM2/13/24
to sy...@googlegroups.com
The order argument to the printers is definitely under-documented.
This has been an issue for some time.

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/92fc1ab4-81bb-491c-a538-b5422e81e9dbn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages