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.

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.

share|improve this question
    
You problem is very similar to the one answered in this post. –  halirutan Mar 31 at 10:10
    
Related How to make traditional output for derivatives. –  Artes Mar 31 at 11:41
    
@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 at 12:49
    
Are you checking the terms by hand? –  Michael E2 Mar 31 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 at 12:57

1 Answer 1

up vote 2 down vote accepted

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
share|improve this answer
    
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 at 21:35

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.