19
$\begingroup$

I could very well be missing something obvious, but this has always bugged me with Mathematica and I don't know why it does it or how to fix it.

If I enter any polynomial, say, x^2 + x - 1 for example, the output is always in the form:

-1 + x + x^2

And again:

input: Expand[(x^2 - 1) ((-3 + x)^2 - 4)]
output: -5 + 6 x + 4 x^2 - 6 x^3 + x^4

I find this much more difficult to read than the traditional way, from highest power to lowest. Is there anything I can do to change this? I'm aware that TraditionalForm prints them properly, but it is generally not recommended to do calculations with TraditionalForm so I'd like to avoid that if possible. Then again, IS IT that bad to do calculations with TraditionalForm like it warns?

$\endgroup$
5
  • 5
    $\begingroup$ Orderless entities are placed in the same order that Sort uses. Sort works with very general expressions and has no notion of "polynomialhood". You might try using TraditionalForm on your expression, as that will often display polynomials, at least univariate ones, in the order you expect. $\endgroup$ Sep 27, 2013 at 20:39
  • 1
    $\begingroup$ You can preform all calculations before applying TraditionalForm. $\endgroup$
    – ybeltukov
    Sep 27, 2013 at 21:25
  • 2
    $\begingroup$ For two reasons, it's actually more natural to write a polynomial in terms of ascending powers of the variable x instead of the more usual, traditional notation with descending powers: (1) a polynomial is a special case of a power series, and power series are written with ascending powers of x; and (2) ascending powers lead more readily to the computationally efficient Horner's form a0 + x(a1 + x(a2 + x(...}) ...). $\endgroup$
    – murray
    Sep 28, 2013 at 20:13
  • $\begingroup$ Related: (6358), (15744), (20714), (30216) $\endgroup$
    – Mr.Wizard
    Aug 13, 2016 at 21:41
  • 1
    $\begingroup$ The question asked why the terms are output in the order given by Mathematica. One justification is that it fits nicely with a Laurant expansion, which includes negative powers of the variable, too. $\endgroup$
    – murray
    Nov 19, 2019 at 19:02

2 Answers 2

16
$\begingroup$

As Daniel Lichtblau wrote in the comment you can use TraditionalForm

Expand[(x^2 - 1) ((-3 + x)^2 - 4)] // TraditionalForm

$x^4-6 x^3+4 x^2+6 x-5$

However, it works perfectly only with univariate polynomials

Expand[(x + y + 1)^5] // TraditionalForm

$x^5+5 x^4 y+5 x^4+10 x^3 y^2+20 x^3 y+10 x^3+10 x^2 y^3+30 x^2 y^2+30 x^2 y+10 x^2+5 x y^4+20 x y^3+30 x y^2+20 x y+5 x+y^5+5 y^4+10 y^3+10 y^2+5 y+1$

You can see that $5x$ is before $y^5$ and so on.

My solution consist in the manual sorting of monomials

OrderedForm = HoldForm[+##] & @@ MonomialList[#][[
     Ordering[Total[#] & @@@ CoefficientRules[#], All, GreaterEqual]]] &;

Expand[(x + y + 1)^5] // OrderedForm
x^5+5 x^4 y+10 x^3 y^2+10 x^2 y^3+5 x y^4+y^5+5 x^4+20 x^3 y+30 x^2 y^2+20 x y^3+5 y^4+
10 x^3+30 x^2 y+30 x y^2+10 y^3+10 x^2+20 x y+10 y^2+5 x+5 y+1
$\endgroup$
6
  • $\begingroup$ I like your solution! I must say that I don't understand how your function works, I don't know much about anything that uses the # symbol in Mathematica yet, but it does order things well. The only downside is that it seems like calculations cannot be done until converted back to StandardForm. Is there a way around that? $\endgroup$
    – Brendan
    Sep 27, 2013 at 23:15
  • 2
    $\begingroup$ @Brendan There is great compilation about such symbols: What the @#%^&*?! do all those funny signs mean?. You don't need to perform calculations on converted expressions (TraditionalForm, OrderedForm, etc.). Just convert only the final result. If you want to see intermediate results use something like (p3=p1+p2)//OrderedForm. $\endgroup$
    – ybeltukov
    Sep 27, 2013 at 23:31
  • 1
    $\begingroup$ Alternatively, one can use some undocumented functionality: PolynomialForm[Expand[(x + y + 1)^5], TraditionalOrder -> True] $\endgroup$ Apr 10, 2017 at 7:57
  • $\begingroup$ How do I save it for further manipulation? TraditionalForm does not help in that case. $\endgroup$
    – BabaYaga
    Oct 7, 2019 at 16:03
  • $\begingroup$ @Boogeyman you have to make a choice between "looks pretty" and "usable in further operations"; can't have both. $\endgroup$ May 14, 2020 at 1:23
2
$\begingroup$
Clear["Global`*"]

You can use a temporary dummy variable to result in the traditional ordering of multinomials.

tradPoly[expr_, 
  vars_] := (expr /. Thread[vars -> temp*vars] // TraditionalForm) /. temp -> 1

tradPoly[expr = (x + y + 1)^5 // Expand, {x, y}]

enter image description here

Since the wrapper is not included in the definition of expr, the wrapper does not affect subsequent use of expr

expr // Simplify

(* (1 + x + y)^5 *)
$\endgroup$
2
  • $\begingroup$ That should be vars not var, right? $\endgroup$
    – Greg Hurst
    May 15, 2020 at 14:46
  • $\begingroup$ @ChipHurst Thanks. Corrected typo. $\endgroup$
    – Bob Hanlon
    May 15, 2020 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.