4

Is there a way in which mathematica can preserve the order of operations while it evaluates an expression?, for instance: In my short example

w = f[r,Θ] ;   
lapla12 = -(r/(2 μ)) (1/r D[w/r r, {r, 2}] - 1/r^2 (D[w/r, {Θ, 2}] + Cot[Θ] D[w /r, {Θ, 1}])) // ExpandAll // TraditionalForm

The output that mathematica gave me is

enter image description here

However, I know the real order is

enter image description here

In my real problem, I have more than 100 terms and is quite difficult to me sort all terms to check the results.

EDITED: For instance the first 35 terms in my real expression appears in this order and i would like that they appears in the correct orderenter image description here

I'm using the code from Jens for derivatives formatting purposes.

Here is the code i'm using to obtain the above output.

ClearAll["Global`*"]
SetOptions[$FrontEndSession, PrintingStyleEnvironment -> "Condensed"]
$PrePrint = # /. {Csc[z_] :> 1/Defer@Sin[z], 
     Sec[z_] :> 1/Defer@Cos[z], 
     Cot[z_] :> Defer@Cos[z]/Defer@Sin[z]} &;


Subscript[w, 1] = f[Subscript[r, 1], Subscript[r, 2], α, β, γ, Subscript[Θ, 12]];

Set[J, -(D[Subscript[w, 1], {β, 2}] + 1/Sin[β]^2 (D[Subscript[w, 1], {α, 2}] + 
         D[Subscript[w, 1], {γ, 2}]) + Cot[β] D[Subscript[w, 1], β] - 
      2 Cot[β]/ Sin[β] D[Subscript[w, 1], α, γ])] // TraditionalForm ;

Set[Subscript[a, 1], 2 D[Subscript[w, 1], {β, 2}] + D[Subscript[w, 1], {γ, 2}] + J]
     // TraditionalForm ;

Set[Subscript[a, 2],  2 Cot[β]/ Sin[β] D[Subscript[w, 1], {α, 1}] - (1 + 2 Cot[β]^2) 
    D[Subscript[w, 1], {γ, 1}] - 2/Sin[β] D[Subscript[w, 1], β, α] + 2 Cot[β] 
    D[Subscript[w, 1], β, γ]] // TraditionalForm ;

Set[Subscript[f, 1], 1/(2 Sin[Subscript[Θ, 12]]^2) (-J + Cos[Subscript[Θ, 12]] 
    (Sin[2 γ] Subscript[a, 2] - Cos[2 γ] Subscript[a, 1]) + Sin[Subscript[Θ, 12]]
    (Sin[2 γ] Subscript[a, 1] + Cos[2 γ] Subscript[a, 2])) - D[Subscript[w, 1], 
    Subscript[Θ, 12], γ] - 1/2 Cot[Subscript[Θ, 12]] D[Subscript[w, 1], {γ, 1}] +
    (1/4 - 1/(2 Sin[Subscript[Θ, 12]]^2)) D[Subscript[w, 1], {γ, 2}] 
    ] // ExpandAll // TraditionalForm ;

Set[lapla1, -(1/(2 Subscript[μ, 1])) (1/Subscript[r, 1] D[Subscript[w, 1] 
    Subscript[r, 1], {Subscript[r, 1], 2}] - 1/Subscript[r, 1]^2
    (D[Subscript[w,  1], {Subscript[Θ, 12], 2}] + Cot[Subscript[Θ, 12]] 
    D[Subscript[w, 1], {Subscript[Θ, 12], 1}]) - Subscript[f, 1]/Subscript[r, 1]^2)
    ] // ExpandAll // TraditionalForm

EDITED 2 In this case the correct order is ruled by the expression for lapla1.enter image description here

where terms in red (radial terms) go first, then terms in purple (internal angular terms), and finally the terms in blue (coupling between the orientational angular terms and the internar angular terms). Blue terms are ruled by the expression for $f_1$ from left to right.

shadraws
  • 139
  • 1
  • 1
  • 7
  • You problem is very similar to the one answered in this post. – halirutan Mar 31 '14 at 10:10
  • @Artes Actually my problem is not related with the traditional outputs for derivatives, as you can see above i added a picture of my real problem. – shadraws Mar 31 '14 at 12:49
  • Are you checking the terms by hand? – Michael E2 Mar 31 '14 at 12:54
  • @MichaelE2 Yes, I need to do it, this will be the input to other program, and i need to introduce it in the correct order. – shadraws Mar 31 '14 at 12:57
  • Is there a well-defined rule for sorting the terms? I can't see the pattern in your example. (You have two #5's, by the way.) – Michael E2 Mar 31 '14 at 13:01
  • @shadraws Actually uou haven't read those answers. Mine demonstrates that with PolynomialForm[ expr, TraditionalOrder -> True]// TraditionalForm you can adjust the ordering of terms. – Artes Mar 31 '14 at 13:04
  • @Artes i tried before with PolynomialForm and the result was exactly the same. I'm trying right now with HoldForm however isn't working as i was expecting.

    @MichaelE2 what do you means with two #5???

    – shadraws Mar 31 '14 at 13:32
  • The term in the lower left is labeled 5, and the antepenultimate term is also labeled 5. – evanb Mar 31 '14 at 21:11
  • Is there a reason you need the sum? I would just replace

    lapla12 = { -(r/(2 μ)) (1/r D[w/r r, {r, 2}], - 1/r^2 (D[w/r, {Θ, 2}], + Cot[Θ] D[w /r, {Θ, 1}])) } in your example

    – evanb Mar 31 '14 at 21:13
  • Sorry @evanb i can't see the label 5. Could you please show me how to see it?. I'm quite new with mathematica. – shadraws Mar 31 '14 at 21:27
  • @shadraws Sorry, it's not a Mathematica thing we were talking about---it's in the image of your real full-sized problem (not your simplified example). I assume that the blue labels denote the what number the term should canonically be. The first few blue numbers are 12 (right next to Out[114]//TraditionalForm= and the next few are 29, 25, 19....

    The bottom row of blue numbers in your image are 5, 13, 30, 23, 17, 5, 1, 2; 5 showing up twice.

    – evanb Mar 31 '14 at 22:01

1 Answers1

3

This is a quick possibility:

myExpand[expr_] := Module[{tmp},
  Unprotect[Plus];
  ClearAttributes[Plus, Orderless];
  tmp = HoldForm @@ {Expand[expr] /. a_Plus :> Reverse[a]};
  SetAttributes[Plus, Orderless];
  tmp];

w = f[r, Θ];
lapla12 = -(r/(2 μ)) (1/r D[w/r r, {r, 2}] - 1/r^2 (D[w/r, {Θ, 2}] + 
          Cot[Θ] D[w/r, {Θ, 1}])) // myExpand
Kuba
  • 136,707
  • 13
  • 279
  • 740
Rolf Mertig
  • 17,172
  • 1
  • 45
  • 76
  • 1
    I really like the trick +1 :), but I'm not sure if OP too, in the long expr there are cases with (a + b) D[f, x] which are separated so it will be though to achieve what OP needs. Not your fault of course, it's the question that is vague. – Kuba Mar 31 '14 at 21:35