2

I tried

expr = a^2 - a  b + b^2 - a  c - b  c + c^2;

HoldForm[+##]&@@MonomialList[expr,#]& /@ { "Lexicographic","DegreeLexicographic", "DegreeReverseLexicographic","NegativeLexicographic","NegativeDegreeLexicographic", "NegativeDegreeReverseLexicographic" } // Column

and The ordering problem of multivariate polynomials.

Neither not get the expected result. How can I get the result $a^2+b^2+c^2-a b-b c-c a$?

vector
  • 179
  • 1
  • 6

1 Answers1

2
expr = a^2 - a b + b^2 - a c - b c + c^2;
HoldForm[+##] & @@ 
 ReverseSortBy[
  MonomialList[expr], {Total@Exponent[#, Variables[expr]] &, 
   Max@Exponent[#, Variables[expr]] &}]

$a^2+b^2+c^2-a b-a c-b c$

The same code in a shorter form:

expr = a^2 - a b + b^2 - a c - b c + c^2;
HoldForm[+##] & @@ 
 ReverseSortBy[
  MonomialList[
   expr], #@*(Exponent[#, Variables[expr]] &) & /@ {Total, Max}]
azerbajdzan
  • 15,863
  • 1
  • 16
  • 48