Thanks Leonid for this quick and helpful answer!
I just have a few follow-up questions:
On Thu, Feb 22, 2018 at 11:38 PM, Leonid Kovalev wrote:
> 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]')
Ah, that's interesting!
I was actually using a Jupyter notebook with MathJax output most of
the time, and there this is not true!
Same for the raw LaTeX output:
>>> sp.latex(p)
'\\operatorname{Poly}{\\left( a_{0} + a_{1} t + a_{2} t^{2} +
a_{3} t^{3}, t, domain=\\mathbb{Z}\\left[a_{0}, a_{1}, a_{2},
a_{3}\\right] \\right)}'
Is this a bug?
> 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'
That's great, I think 'grevlex' is what I want!
I was actually already playing around with 'lex', 'revlex' and
'grevlex', but I got confused at some point.
It looks like this doesn't work if the highest power doesn't have a
symbolic coefficient, e.g.:
>>> sp.sstrrepr(t**2 + a1 * t, order='grevlex')
'a1*t + t**2'
I assume there is a perfectly reasonable explanation for that, and in
my case such expressions didn't actually appear yet, so that's fine
for me.
I think I will mainly use 'grevlex' with:
sp.init_printing(order='grevlex')
But when I'm dealing with expressions that have only the coefficients
and no powers of t in them, e.g.:
3*a3 + 2*a2 + a1
... then I'll temporarily switch:
sp.init_printing(order='rev-lex')
Or is this a bad idea?
Or is there a better way to temporarily change the order?
Is there a way to specify the order for a single Jupyter output cell?
> The printing module has a number of printers which support a number of
> settings.
Thanks for the reference to
http://docs.sympy.org/latest/modules/printing.html, that's a very
helpful page.
cheers,
Matthias