8
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Is it only for display purposes that you want this? Or something more? $\endgroup$
    – march
    Feb 23, 2016 at 17:24
  • $\begingroup$ Only for display purposes. $\endgroup$
    – Simon
    Feb 23, 2016 at 17:30

1 Answer 1

5
$\begingroup$

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]

enter image description here

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]

enter image description here

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}}], "+"]]
$\endgroup$
4
  • $\begingroup$ 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 ? $\endgroup$
    – Simon
    Feb 24, 2016 at 14:36
  • $\begingroup$ @Simon. See the updated versions. $\endgroup$
    – march
    Feb 24, 2016 at 16:37
  • $\begingroup$ 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 ] $\endgroup$
    – Simon
    Feb 25, 2016 at 17:24
  • $\begingroup$ @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! $\endgroup$
    – march
    Feb 25, 2016 at 17:29

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.