In SymPy, polynomials have extra structure that distinguishes them from generic expressions. a3 * t**3 + a2 * t**2 + a1 * t + a0 is an expression. If you create a polynomial in t, it will print with the order of terms being from highest to lowest.
>>> p = sp.Poly([a3, a2, a1, a0], t) >>> print(p) Poly(a3*t**3 + a2*t**2 + a1*t + a0, t, domain='ZZ[a0,a1,a2,a3]') Also, the order can be specified in the print command >>> pprint(a3 * t**3 + a2 * t**2 + a1 * t + a0, order='grevlex') 3 2 a₃⋅t + a₂⋅t + a₁⋅t + a₀ or, staying with str format, >>> sstrrepr(a3 * t**3 + a2 * t**2 + a1 * t + a0, order='grevlex') 'a3*t**3 + a2*t**2 + a1*t + a0' The printing module <http://docs.sympy.org/latest/modules/printing.html> has a number of printers which support a number of settings. On Thursday, February 22, 2018 at 2:02:20 PM UTC-5, Matthias Geier wrote: > > Dear SymPy list. > > I'm playing around with polynomials in the context of spline curves. > > I want to use a cubic polynomial with yet unknown coefficients like this: > > >>> import sympy as sp > >>> t, a0, a1, a2, a3 = sp.symbols('t, a:4', real=True) > >>> a3 * t**3 + a2 * t**2 + a1 * t + a0 > a0 + a1*t + a2*t**2 + a3*t**3 > > The problem here is that the displayed order of terms is reversed, > normally the highest power of t should come first. > I guess this is because SymPy doesn't know that the coefficients a0 > etc. are constants and shouldn't be treated like variables. > So in fact this polynomial isn't sorted by powers of t but instead by > the coefficients. > > Is there a way to get around this? > > At some later point, I have expressions like this (without t): > > a1 + 2*a2 + 3*a3 > > It would make sense in my case to also display them reversed like this: > > 3*a3 + 2*a2 + a1 > > Is it possible to create a new type of symbol with non-default ordering? > Is it possible to define that this order is "ascending": a3, a2, a1, a0? > It doesn't have to be a generic solution, I'm OK with having those 4 > special symbols. > > Or is there an entirely different and much better way to do this? > > I know that I could just use a, b, c, d instead of a3, a2, a1, a0 and > it would work, but I would really like to see the connection between a > coefficient and its power of t. > > For the record, I also quickly tried to use IndexedBase to get a3, a2, > a1 and a0, and it turns out that although the LaTeX display of the > symbols looks the same (in text mode it's different), they are sorted > differently. > > >>> b = sp.IndexedBase('b') > >>> b[3] * t**3 + b[2] * t**2 + b[1] * t + b[0] > t**3*b[3] + t**2*b[2] + t*b[1] + b[0] > > They are sorted after the powers of t, which isn't what I want, either. > > cheers, > Matthias > -- 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+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at https://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/3ed2faac-f7cf-412b-9392-ddb157935681%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.