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.

I use Simplify mostly to make expressions look neat. However, Mathematica often does not produce expression in, what I consider, the optimal way. Look at this:

Mathematica expression

FullSimplify[
 -((E^(-((t-μ1)^2/(2 σ1^2))) p w1)/(Sqrt[2 π] σ1))+
 (E^(-((t-μ1)^2/(2 σ1^2))) p w2)/(Sqrt[2 π] σ1)-
 (E^(-((t-μ2)^2/(2 σ2^2))) (1-p) w3)/(Sqrt[2 π] σ2)+
 (E^(-((t-μ2)^2/(2 σ2^2))) (1-p) w4)/(Sqrt[2 π] σ2)]

Obviously, I would prefer the Sqrt[2 Pi] to appear next to the two σ's. This is just one example of how Simplify sometimes does not do what I want.

Any general tips?

share|improve this question

closed as primarily opinion-based by m_goldberg, bobthechemist, rasher, belisarius, Mr.Wizard Mar 14 at 8:36

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Please give Mathematica code insdead of a picture :) –  Öskå Mar 13 at 11:34
    
Pretty irrelevant in this case. –  LBogaardt Mar 13 at 11:43
1  
Try FullSimplify[expr] // Apart. –  Kuba Mar 13 at 11:45
2  
Including code is quite relevant for reproduction of your output and minimizes efforts to help you. –  Yves Klett Mar 13 at 11:59
1  
One person's optimal presentation is another one's non-preferred solution. You can probably imagine that your goal of using Sqrt[2 Pi] twice may be considered as suboptimal, especially as output of a function whose task it roughly is to minimize term count. –  Sjoerd C. de Vries Mar 13 at 15:48

1 Answer 1

up vote 1 down vote accepted

I use Simplify mostly to make expressions look neat.

The purpose of functions like Simplify is not to make expressions neater, but to transform them into a form which minimal with respect to a certain measure, usually something containing the LeafCount. This measure of how complex an expression is can be set with the ComplexityFunction option of Simplify.

Therefore, you are just using the wrong function for the problem you have. You want nice representation, which is completely subjective and different from what Simplify tries, because it makes the expression as small as possible.

As suggested by Kuba, you can use

FullSimplify[expr] // Apart

to get the form you want.. in this situation! Usually, it doesn't work that easy. Another possibility is to do the transformation manually

expr=-((E^(-((t-\[Mu]1)^2/(2 \[Sigma]1^2))) p w1)/(Sqrt[2 \[Pi]] 
  \[Sigma]1))+(E^(-((t-\[Mu]1)^2/(2 \[Sigma]1^2))) p w2)/(Sqrt[2 \[Pi]]
  \[Sigma]1)-(E^(-((t-\[Mu]2)^2/(2 \[Sigma]2^2))) (1-p) w3)/(Sqrt[2 \[Pi]]
  \[Sigma]2)+(E^(-((t-\[Mu]2)^2/(2 \[Sigma]2^2))) (1-p) w4)/(Sqrt[2 \[Pi]] \[Sigma]2);

expr2 = expr /. Plus[a_, b_, c_, d_] :> Plus[Together[c + d], Together[a + b]]

Mathematica graphics

As you can see, although I turned the sum around to make the minus term to the right, Mathematica still puts it in the front. This is the point where you have to work with the Hold functions the prevent evaluation of your expressions. Furthermore, some transformations are pretty ugly because the internal representation differs from what you see. One example is, if you try to put the 1/Sqrt[2Pi] in front of the whole expression. The problem here is, that Mathematica stores 1/Sqrt[2Pi] in a different form. It is not stored as

$$\sqrt{2\pi}^{-1}$$

Power[Sqrt[2Pi],-1]//FullForm
(* Power[Times[2,Pi],Rational[-1,2]] *)

Therefore, the transformation rule requires that you have looked at the FullForm for a while:

expr2 /. Times[a1__, Power[Times[2, Pi], Rational[-1, 2]], a2___] + 
   Times[b1___, Power[Times[2, Pi], Rational[-1, 2]], b2___] :> 
  HoldForm[HoldForm[1/Sqrt[2 Pi]] (Times[b1, b2] + Times[a1, a2])]

Mathematica graphics

As you can see, with the HoldForm we can even get the terms in a different order to make the minus sign appear in the middle.

share|improve this answer
    
Great answer. Thank you for the effort! –  LBogaardt Mar 19 at 23:09

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