Display polynomial retaining MonomialList[ ] order?

MonomialList is a neat functions for rearranging a polynomial, but if you convert it back to a polynomial with Total[] or Plus[], the output reverts to the default lexicographic order, which sabotages the functionality of MonomialList[]. Is there a way to rearrange a polynomial according to MonomialList[] options? By that, I mean a way that’s easier than editing the list back into a polynomial character by character?

For example:
Say you have a polynomial like 4 x y^2 z + 4 z^2 - 5 x^3 + 7 x^2 z^2 and want to display it three different ways – Lexicographic order, DegreeLexicographic order and NegativeDegreeLexicographic order.

Hi Jay,

Here is one way that I saw on MSE. I searched but cannot find it

ClearAll[x, y, x];
exp = 4 x y^2 z + 4 z^2 - 5 x^3 + 7 x^2 z^2

orders = MonomialList[exp, #] & /@ {"Lexicographic", "DegreeLexicographic", 
  "DegreeReverseLexicographic", "NegativeLexicographic", 
  "NegativeDegreeLexicographic", "NegativeDegreeReverseLexicographic"}
(* {{-5 x^3, 7 x^2 z^2, 4 x y^2 z, 4 z^2}, {7 x^2 z^2, 4 x y^2 z, -5 x^3,
   4 z^2}, {4 x y^2 z, 7 x^2 z^2, -5 x^3, 4 z^2}, {4 z^2, 4 x y^2 z, 
  7 x^2 z^2, -5 x^3}, {4 z^2, -5 x^3, 7 x^2 z^2, 
  4 x y^2 z}, {4 z^2, -5 x^3, 4 x y^2 z, 7 x^2 z^2}}  *)

Replace[orders, {x__} :> HoldForm[+x], 1]

7334plot.png

Shown as an image since it is a held expression and releasing the hold will revert to lexicographic.

Thanks, Rohit. You are a genius. I just read the Replace[] documentation, and I still don’t understand why this works. But it does. Thumbs up.

You are a genius

Ummm… no. Definitely not me. I searched again on MSE and found it.

You’re too modest. I would not have recognized that MSE thread as an answer to my question had I seen it. In fact, I’ve seen it now, and I still don’t recognize it as an answer to my question. Clarity is the ultimate genius. The guys who figured out what Gauss was saying were smarter than Gauss.

Rohit Namjoshi solution works for standard order, but I’m having trouble generalizing this other variable orders. For example,…

Hi Jay,

Isn’t this the same issue that was solved using the Injector Pattern earlier in this post?

MonomialList[a x^2 + b x y + c y^2, {y, x}] // Replace[#, {x__} :> HoldForm[+x]] &
(* c y^2 + b x y + a x^2 *)

If you evaluate it, it will of course revert to lexicographic.

Yes, Rohit. It appears to be the same issue. I think I misunderstood the “orders = …” line in your previous post. Today I found the tutorial on patterns. I’m going through it now.

Another possibility is to just use an inactivated version of Plus:

expr = 4 x y^2 z + 4 z^2 - 5 x^3 + 7 x^2 z^2;
ordList = {"Lexicographic", "DegreeLexicographic", "DegreeReverseLexicographic",
           "NegativeLexicographic", "NegativeDegreeLexicographic",
           "NegativeDegreeReverseLexicographic"};

Apply[Inactive[Plus], MonomialList[expr, #]] & /@ ordList

and one can easily get back the original polynomial using Activate[]; e.g.

Activate[Last[%]]

Thanks. J.M. I didn’t know about Inactive.