The order argument to the printers is definitely under-documented. This has been an issue for some time.
Aaron Meurer On Tue, Feb 13, 2024 at 10:41 AM Thomas Ligon <[email protected]> wrote: > > 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 > https://docs.sympy.org/latest/modules/printing.html > 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. > > On Tuesday, February 13, 2024 at 12:46:43 AM UTC+1 [email protected] wrote: >> >> 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 >> >> On Sat, Feb 10, 2024 at 1:58 PM Thomas Ligon <[email protected]> wrote: >> > >> > 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 >> > >> > -- >> > 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 [email protected]. >> > To view this discussion on the web visit >> > https://groups.google.com/d/msgid/sympy/166a5149-26b2-47f7-9956-943b9d379d92n%40googlegroups.com. > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/92fc1ab4-81bb-491c-a538-b5422e81e9dbn%40googlegroups.com. -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6KUTA0p2knb%2B345ONe9dk5fxpg0E8e%3DkpKMgZEt6_WV%3DQ%40mail.gmail.com.
