Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The first three expressions evaluate as expected and the polynomial is displayed in what I would call "textbook" form. The last expression, however, switches the order of terms. Mathematica employs this change for two-term polynomials if it results in getting rid of the leading negative sign (at least that is the best I can deduce).

x^2 + x + 5 // TraditionalForm
(* x^2 + x + 5 *)

-x^2 + x + 5 // TraditionalForm
(* -x^2 + x + 5 *)

x^2 + x // TraditionalForm
(* x^2 + x *)

-x^2 + x // TraditionalForm
(* x - x^2 *)

These polynomials are the result of prior symbolic manipulation, so I cannot simply use HoldForm or the equivalent to maintain the desired order.

Is there a way to change this behavior in general so that the last expression displays as -x^2 + x? I can think of substitution rules to fix this particular example, but would like to find a robust solution that applies as transparently as possible across the board.

Edit

Additionally, PolynomialForm produces the same results:

PolynomialForm[-x^2 + x , TraditionalOrder -> True]
(* x - x^2 *)

PolynomialForm[-x^2 - x , TraditionalOrder -> True]
(* -x^2 - x *)

It seems that Mathematica will produce the traditional order for polynomial terms except when there are only two terms and reversing the order eliminates the leading negative sign.

share|improve this question
    
I was expecting to close this question with a reference to PolynomialForm[#, TraditionalOrder -> True] & but I see that you're going the other way. Let me think about that. –  Mr.Wizard Mar 5 '13 at 23:41
    
@Mr.Wizard: Yes, I already tried PolynomialForm and that produces the same results. I will add that information to the question because that will probably be a common thought pattern. –  RandomBits Mar 5 '13 at 23:45
    
Previous questions relating to this usually creates a new function to handle Plus. But would be nice with a way that lets you override the displayed order of Orderless arguments. –  ssch Mar 6 '13 at 0:21
    
    
@ssch I don't think those solve this one (which I assume is why you didn't post an answer.) RandomBits doesn't want to prevent ordering, he wants to control it. –  Mr.Wizard Mar 6 '13 at 0:46

3 Answers 3

Since the two other answers don't seem to do exactly what's needed, I'll try my luck:

order[poly_] := 
 Replace[Reverse@Sort[List @@ poly], List[x__] :> HoldForm[Plus[x]]]

order /@ {x^2 + x + 5, -x^2 + x + 5, x^2 + x, -x^2 - x}

$\left\{x^2+x+5,-x^2+x+5,x^2+x,-x^2- x\right\}$

Here I end up with HoldForm wrapping an expression that should have the sorted terms, and this ordering would be maintained when feeding it into TraditionalForm or TeXForm.

I avoided using Row for the output (and hence also don't use Format or MakeBoxes) so that I don't have to worry about getting things like $+\,-\,x$.

share|improve this answer
    
Well I was just getting around to this method myself but you were six minutes faster so +1. :-) –  Mr.Wizard Aug 3 '13 at 5:37
    
Jens, do you see anything wrong with my second method? What do you see as the advantage or disadvantage of Sort[List @@ #] versus MonomialList@#? –  Mr.Wizard Aug 3 '13 at 5:50
    
@Mr.Wizard MonomialList is the right choice, I think. But using Sort seems like a cute, original twist. –  Jens Aug 3 '13 at 6:09

I hope there is a better way but here is something to build upon:

TraditionalForm @ Row[MonomialList@#, "+"] & /@
  {x^2 + x + 5, -x^2 + x + 5, x^2 + x, -x^2 + x}

Mathematica graphics


Jens pointed out a bug in my original method. Here's another:

HoldForm[+##] & @@ MonomialList@# & /@
 {x^2 + x + 5, -x^2 + x + 5, x^2 + x, -x^2 + x, -x^2 - x}

enter image description here

share|improve this answer
2  
Similar Row[MonomialList[-x^2 + x, {x}, "DegreeLexicographic"], "+"] –  ssch Mar 6 '13 at 0:06
    
Is it the case that Plus@@MonomialList[expr] == expr for any expression? If so, then I can define a format: Format[xPlus[x___]] := Row[Riffle[{expr}, "+"]] and then xPlus := Plus and then use HoldForm[xPlus@@MonomialList[expr]] to the display ordering that I want while still having a valid expression when the hold is released (because the xPlus will be changed to Plus). –  RandomBits Mar 6 '13 at 3:47
1  
After I upvoted this, I realized it's not quite right: Try TraditionalForm@Row[MonomialList@#, "+"] &[-x^2 - x] and you get $-x^2+\,-\,x$. The same error happens with the solution by @ssch. –  Jens Aug 3 '13 at 5:16
    
@Jens Okay, maybe a hybrid of the two answers will work. Let me try some stuff. –  Mr.Wizard Aug 3 '13 at 5:25
poly[x_] := 
 Block[{Plus}, 
  x // Sort // Reverse // Evaluate // HoldForm // TraditionalForm]
poly[x - x^2]
(* -x^2+x *)
poly[-x^2 + x]
(* -x^2+x *)
share|improve this answer
    
This is the best answer, I think (+1). But do you really need HoldFirst? –  Jens Aug 3 '13 at 5:18
    
@Jens This changes the order of all the other terms, which in my understanding is not desired. I believe all but the last expression should agree with {x^2 + x + 5, -x^2 + x + 5, x^2 + x, -x^2 + x} // TraditionalForm. That is to say, it should match my answer. –  Mr.Wizard Aug 3 '13 at 5:19
    
@Mr.Wizard You're right. I thought Sort on blocked Plus is a clever idea, but it doesn't quite do what's needed either. –  Jens Aug 3 '13 at 5:24
    
@Jens, Mr. Wizard good catches - guess I should have done more testing. –  VF1 Aug 3 '13 at 5:33
    
@Jens moreover, it fails with -x + 1. –  Kuba May 23 at 9:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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