# Simplify in Mathematica

1. Feb 17, 2013

### Fredrik

Staff Emeritus
Why do I keep getting output that looks like this when I use Mathematica?
$$\frac{2 b k |a+b v| (2 a+b v+b v \cos (t)) \sin ^2\left(\frac{t}{2}\right)}{(a+b v)^2 \sqrt{\left(\frac{1}{\sqrt{4 a^2 k^2 \sin ^4\left(\frac{t}{2}\right)+d^2 (a+b v)^2 \sin ^2(t)}}\right)^* \sqrt{4 a^2 k^2 \sin ^4\left(\frac{t}{2}\right)+d^2 (a+b v)^2 \sin ^2(t)}} \sqrt{4 a^2 k^2 \sin ^4\left(\frac{t}{2}\right)+d^2 (a+b v)^2 \sin ^2(t)}}$$ This is the output of a "Simplify" with the options
Code (Text):
{a, b, d, v, t} \[Element] Reals && k > 0
It's like Mathematica is ignoring that I've tried to tell it that all variables are real.

2. Feb 17, 2013

### Bill Simpson

No assumptions about domain and this is the result

In[1]:= Simplify[(2b k Abs[a+b v](2a+b v+b v Cos[t]) Sin[t/2]^2)/((a+b v)^2) Sqrt[(1/Sqrt[4a^2k^2 Sin[t/2]^4+d^2(a+b v)^2 Sin[t]^2])^2 Sqrt[4a^2k^2 Sin[t/2]^4+d^2(a+b v)^2 Sin[t]^2] Sqrt[4a^2k^2 Sin[t/2]^4+d^2(a+b v)^2 Sin[t]^2]]]

Out[1]= (2*b*k*Abs[a + b*v]*(2*a + b*v + b*v*Cos[t])*Sin[t/2]^2)/(a + b*v)^2

So either I'm making a mistake when I have to type all your stuff back in just to give it to Mathematica or you have a problem in your desktop publishing, like that superscript "*" I see and I guessed that was supposed to be "2", or maybe there is a version difference.

What is your actual raw code without any desktop publishing?

Last edited: Feb 17, 2013
3. Feb 17, 2013

### Fredrik

Staff Emeritus
I should have included the full code in my post. Sorry about that. You can see it below. I should perhaps also mention that this is my first attempt to use Mathematica to do anything at all really. So it's possible that I'm doing many things wrong. I've been doing this on someone else's copy of Mathematica 7. I don't have access to the latest version at the moment.

The * superscript comes out as ^\[Conjugate] when I do a "copy as plain text".

Code (Text):
X={{a,b,0 ,0},{-v d,d,0,0},{0,0,k,0},{0,0,0,k}}
F={{1,0,0,0},{0,Cos[t],-Sin[t],0},{0,Sin[t],Cos[t],0},{0,0,0,1}}
Y=Simplify[Inverse[X].F.X]

p=Take[Y,{2,4},{1,1}]
q=Take[Y,{1,1},{2,4}]
B=Take[Y,{2,4},{2,4}]
Simplify[Norm[p],{a,b,d,v,t}\[Element]Reals && k>0];

temp1=Simplify[{{1,0,0}}.p/Norm[p],{a,b,d,v,t}\[Element]Reals && k>0];
temp2=Simplify[{{0,1,0}}.p/Norm[p],{a,b,d,v,t}\[Element]Reals && k>0];
T=(Abs[v]/v)temp1[[1,1]];
S=(Abs[v]/v) temp2[[1,1]];

R1={{T,S,0},{-S,T,0},{0,0,1}}
R1p=Simplify[R1.p]
R1p1=R1p[[1,1]]

R1B=Simplify[R1.B,{a,b,d,v,t}\[Element]Reals && k > 0]

x=Take[R1B,{2,2},{1,3}];
z={{x[[1,2]],-x[[1,1]],0}};
zz=Simplify[z/Norm[z],{a,b,d,v,t}\[Element]Reals && k>0];

R2={{zz[[1,1]],-zz[[1,2]],0},{zz[[1,2]],zz[[1,1]],0},{0,0,1}}

J=Simplify[R1.B.R2,{a,b,d,v,t}\[Element]Reals && k>0]

Simplify[J,a k^2==d^2(a+b v)]

qR2=Simplify[q.R2,{a,b,d,v,t}\[Element]Reals && k>0]

Simplify[qR2,a k^2==d^2(a+b v)]

w=-R1p[[1,1]]/J[[1,1]]

Things are getting really ugly with the variable I called R2. But something is already wrong with R1p. The top component of that matrix has |a+b v| in the numerator and (a+b v)2 in the denominator. I tried adding options to that Simplify operation as well, but that doesn't change anything.

Last edited: Feb 17, 2013
4. Feb 17, 2013

### Bill Simpson

The first thing I would look at is

Norm[p]

because it appears that so much of your code depends on that.

Is Norm[p] supposed to be something that you are not seeing in your notebook?

Last edited: Feb 17, 2013
5. Feb 17, 2013

### Fredrik

Staff Emeritus
p is the 3×1 matrix
$$\begin{pmatrix} \frac{2 a v \sin ^2\left(\frac{t}{2}\right)}{a+b v} \\ -\frac{d v \sin (t)}{k} \\ 0 \end{pmatrix}$$ and Norm[p] is the norm of p, i.e. $\|p\|=\sqrt{(p_1)^2+(p_2)^2+(p_3)^2}$. If I just evaluate Norm[p], I get
$$\sqrt{\frac{4 a v \sin ^2\left(\frac{t}{2}\right) (a v)^* \sin ^2\left(\frac{t^*}{2}\right)}{(a+b v) (a+b v)^*}+\frac{d v \sin (t) (d v \sin (t))^*}{k k^*}}$$ If I evaluate Simplify[Norm[p],{a,b,d,v,t}\[Element]Reals && k>0], I get
$$\frac{|v| \sqrt{4 a^2 k^2 \sin ^4\left(\frac{t}{2}\right)+d^2 \sin ^2(t) (a+b v)^2}}{k |a+b v|}$$ So I would say that "Simplify" is doing OK so far.

6. Feb 18, 2013

### Bill Simpson

I'm not finding an explanation.

Perhaps someone else can spot something.

Something that might relate, many programming languages make a big distinction between row vectors and column vectors. Mathematica doesn't, a vector will be interpreted as row or column as needed. This means you don't need to go to the trouble to try to construct column vectors. In fact in some old versions of Mathematica adding an extra layer of {} to try to force elements to be in a column could cause problems, but they may have found and fixed those by now. I do see that you have column vectors with the extra layer of {} at places in your code.

Hopefully someone else can spot something that I just seem to be blind to at the moment.

If you could show specific examples of where the calculations seem to be incorrect that might help someone, something more specific than "But something is already wrong with R1p."

7. Feb 18, 2013

### Fredrik

Staff Emeritus
That's OK. Thanks for trying. I did say what the problem with R1p is. The first component is of the form
$$\frac{|a+bv|X}{(a+bv)^2 Y}.$$ Since all the variables are real, this can be simplified to
$$\frac{X}{|a+bv|Y},$$ but Mathematica doesn't do that, even when I explicitly tell it that all variables are real by changing the definition to
Code (Text):
R1p=Simplify[R1.p,{a,b,d,v,t}\[Element]Reals && k>0]
Edit: Hold on a second... If I change the simplify assumption to a+bv>0, then Mathematica goes through with the simplification. So it was probably just "concerned" that the obvious simplification may involve division by 0. (That doesn't entirely make sense to me, since if a+bv=0, we would have a division by zero problem even if we don't simplify).

Edit 2: I think the problem with the example in the OP is that Mathematica is concerned that the things under the square roots are zero or negative.

Last edited: Feb 18, 2013
8. Feb 19, 2013

### Fredrik

Staff Emeritus
I could still use some help from someone who understands the Simplify feature better than I do. One of the expressions that show up in the calculation can also be generated by the following code.
Code (Text):
z=(d v (a+b v) Sin[t])/(Abs[v] Sqrt[Conjugate[1/Sqrt[4 a^2 k^2 Sin[t/2]^4+d^2 (a+b v)^2 Sin[t]^2]] Sqrt[4 a^2 k^2 Sin[t/2]^4+d^2 (a+b v)^2 Sin[t]^4]] Sqrt[4 a^2 k^2 Sin[t/2]+d^2 (a+b v)^2 Sin[t]])
The output is
$$\frac{d v \sin(t) (a+b v)}{|v| \sqrt{4 a^2 k^2 \sin \left(\frac{t}{2}\right) +d^2 \sin(t) (a+b v)^2} \sqrt{\sqrt{4 a^2 k^2 \sin^4\left(\frac{t}{2}\right) +d^2 \sin^4(t) (a+b v)^2} \left(\frac{1}{\sqrt{4 a^2 k^2 \sin^4\left(\frac{t}{2}\right) +d^2 (a+b v)^2 \sin ^2(t)}}\right)^*}}$$ The following Simplify does absolutely nothing.
Code (Text):
Simplify[z,{a,b,d,v,k,t}\[Element]Reals && 4 a^2 k^2 Sin[t/2]^4+d^2 (a+b v)^2 Sin[t]^2>0 && k>0]
As you can see, I even tried to tell it that the thing under the square roots is positive.

Here's a simpler thing that Mathematica fails to simplify.
Code (Text):
Simplify[v^2/Abs[v], v\[Element]Reals && v!=0]
Why do I get $v^2/|v|$ instead of just $|v|$?

Last edited: Feb 19, 2013
9. Feb 19, 2013

### Staff: Mentor

Try using FullSimplify instead of Simplify.

FullSimplify[v^2/Abs[v], v \[Element] Reals && v != 0]
gives
Abs[v]

FullSimplify is a lot slower than Simplify (even when it achieves the same end result) because it uses a lot more transformation rules.

Note, that the things under the square roots are all different. They have different powers of sin. I am not sure that the expression you have can be simplified further.

Last edited: Feb 19, 2013
10. Feb 19, 2013

### Fredrik

Staff Emeritus
Interesting. I have tried FullSimplify many times when I wasn't satisfied with what I got, and so far I've been getting exactly the same result as from Simplify.

Oops. I'm looking at the actual Mathematica output now, and it's not like that there. I mean, if I run the "z=..." command in the code box in my previous post, I do get the output that we see here. But I must have made several mistakes when I made that post. I did a "copy as plain text" on the relevant part of the Mathematica output, and pasted the result into the code box (after typing "z="). But that produces things like "sin^2(t)" which (I think) can't be used as input, so I changed these things manually. I would change this to Sin[t]^2. I must have made three(!) mistakes when I did this. For example I must have typed a 4 in one place where I meant to type a 2. The actual output contains Sin[t/2]^4 and Sin[t]^2, but never Sin[t]^4. The two "small" square roots under the big one are definitely supposed to be the same.

To produce the LaTeX you see, I ran the code from the code box and then did a copy as LaTeX.

My latest version of the code (I did everything over from scratch) doesn't produce an output with "dv" in the numerator anywhere, but I get a similar expression in which all three of the "small" square roots are the same. They are identical to the one that's being complex conjugated in the incorrect LaTeX above.

I will try to describe the problem again, since my description contains several mistakes. Consider this code.
Code (Text):
M={{a,b,0,0},{-v d,d,0,0},{0,0,k,0},{0,0,0,k}}
F={{1,0,0,0},{0,Cos[t],-Sin[t],0},{0,Sin[t],Cos[t],0},{0,0,0,1}}
X=Simplify[Inverse[M].F.M]

p=Take[X,{2,4},{1,1}]
q=Take[X,{1,1},{2,4}]
S=Take[X,{2,4},{2,4}]
r=X[[1,1]]

pn=Simplify[p/Norm[p],{a,b,d,v,k,t}\[Element]Reals]
R=Simplify[{{pn[[1,1]],pn[[2,1]],0},{-pn[[2,1]],pn[[1,1]],0},{0,0,1}},a+b v>0]
RS = Simplify[R.S, k > 0]

y=Take[RS,{2,2},{1,3}]
yn=Simplify[y/Norm[y],{a,b,d,v,k,t}\[Element]Reals  && k>0]
yn[[1,1]]

The output from that last yn[[1,1]] looks like this:
$$\frac{d v \sin (t) (a+b v)}{|v| \sqrt{4 a^2 k^2 \sin ^4\left(\frac{t}{2}\right)+d^2 \sin ^2(t) (a+b v)^2} \sqrt{\sqrt{4 a^2 k^2 \sin ^4\left(\frac{t}{2}\right)+d^2 \sin ^2(t) (a+b v)^2} \left(\frac{1}{\sqrt{4 a^2 k^2 \sin ^4\left(\frac{t}{2}\right)+d^2 (a+b v)^2 \sin ^2(t)}}\right)^*}}$$ This should be accurate, since the only thing I changed manually was the complex conjugation symbol.

11. Feb 20, 2013

### Staff: Mentor

Unfortunately, many times Simplify and FullSimplify give you the same results, and I have never found a good way to tell when it will work better. However, you have a case where it does:
Code (Text):

yn=FullSimplify[y/Norm[y],{a,b,d,v,k,t}\[Element]Reals  && k>0]

I think this gives you a result that cannot be simplified any further without additional assumptions.

Now, if you know that the term under the square root is not just real, but positive, then it can simplify further, but putting in a condition like:

Code (Text):
4 a^2 k^2 Sin[t/2]^4 + d^2 (a + b v)^2 Sin[t]^2 > 0

seems to confuse it more than help.

What you can do in situations like that is make a direct substitution rather than a simplifying assumption:
Code (Text):

yn = FullSimplify[y/Norm[y], {a, b, d, v, k, t} \[Element] Reals && k > 0]
/. {Sign[4 a^2 k^2 Sin[t/2]^4 + d^2 (a + b v)^2 Sin[t]^2] -> 1}

Last edited: Feb 20, 2013
12. Feb 20, 2013

### Fredrik

Staff Emeritus
Wow. FullSimplify really worked much better on this expression. Thanks for the tip. That substitution thing is interesting too.

13. Feb 20, 2013

### Ben Niehoff

Code (Text):
Sign[4 a^2 k^2 Sin[t/2]^4 + d^2 (a + b v)^2 Sin[t]^2] -> 1
Be careful with substitutions like this. Mathematica does not look for that mathematical expression and substitute 1. It looks for that exact string of symbols and substitutes 1. So it will not catch other expressions mathematically equivalent to that one. It will even fail if the terms are written in a different order.

It's usually better to solve for one variable in the substitution you want to make, and write something of the form

Code (Text):
var -> (some long expression)