# How to order terms in a polynomial in two variables negative lexicographically

I have several polynomials in variables p and q, each term in which has total degree n, a constant. I would like to output the polynomial in increasing powers of p (and hence decreasing powers of q), and within each term I would like the factors to be in alphabetical order, i.e. the power of p followed by the power of q.

For example, I would like 2p q + p^2 to be displayed exactly like that. (Or preferably, with the powers as superscripts, but this is not so important.)

Everything I try yields p^2 + 2p q instead. I have tried TraditionalForm, PolynomialForm, and these answers:

How to reorder and combine terms in a polynomial with multi-variables?

How do I disable that Mathematica orders terms in lexicographic order?

How to simplify a polynomial and get the results in the order that I want?

How can I reorder the factors in the terms of a polynomial?

I am using Mathematica version 9.

• Is it only for display purposes that you want this? Or something more? – march Feb 23 '16 at 17:24
• Only for display purposes. – Simon Feb 23 '16 at 17:30

Fixing the +- Problem

displayPolynomial[poly_] := Block[{Plus},
DisplayForm@ToBoxes[
Plus @@ MonomialList[poly, {{0, 1}, {-1, 0}}]
] /. {a___, "+", RowBox[{"-", b_}], c___} :> {a, RowBox[{"-\[ThinSpace]", b}], c}
]
poly = 2 p q + p^2 - q^3 - q^7 + p^2 q^3
displayPolynomial[poly]


or

displayPolynomial[poly_] := Module[{expr},
Unprotect[Plus]; ClearAttributes[Plus, Orderless];
expr = DisplayForm@ToBoxes[Plus @@ MonomialList[poly, {{0, 1}, {-1, 0}}]];
SetAttributes[Plus, Orderless]; Protect[Plus];
expr
]


Nicer Version

displayPolynomial[poly_] :=
Block[{Plus}, DisplayForm@ToBoxes[Plus @@ MonomialList[poly, {{0, 1}, {-1, 0}}]]]
poly = 2 p q + p^2 + q^3 + q^7 + p^2 q^3
displayPolynomial[poly]


The important part here is the second argument of MonomialList, which specifies the ordering. See the documentation page for PolynomialOrdering.

Original Version

As long as there are only two variables, I believe this should work:

displayPolynomial[poly_] :=
DisplayForm@RowBox[ToBoxes /@ Riffle[MonomialList[poly, {{0, 1}, {-1, 0}}], "+"]]

• Thank you very much. In the end I adapted your code to this: DisplayForm[RowBox[Map[ToBoxes, Riffle[MonomialList[g[p, q], {p, q}, {{-1, 0}, {0, 0}}], "+"]]]], which works in my particular case, because the order depends only on the power of p, and also all the coefficients are non-negative. Is there a way to avoid the output "+-" in the more general case where you have negative coefficients ? – Simon Feb 24 '16 at 14:36
• @Simon. See the updated versions. – march Feb 24 '16 at 16:37
• Thank you very much again. Now using "Module" version of your code. Works perfectly. Changed the matrix to {{-1,0},{0,0}} to suit my needs. Also found that I needed to specify explicitly to MonomialList that the variables are p and q, in order to avoid error messages when poly = p^2, for example. My final adaptation looks like this: displayPolynomial[poly_] := Module[{expr}, Unprotect[Plus]; ClearAttributes[Plus, Orderless]; expr = DisplayForm[ ToBoxes[Apply[Plus, MonomialList[poly, {p, q}, {{-1, 0}, {0, 0}}]]]]; SetAttributes[Plus, Orderless]; Protect[Plus]; expr ] – Simon Feb 25 '16 at 17:24
• @Simon. Great! Yeah, I wasn't sure about the correct ordering exactly, but I figured drawing attention to the third argument to MonomialList would help. Glad it worked! – march Feb 25 '16 at 17:29