Simplify in Mathematica


by Fredrik
Tags: mathematica, simplify
Fredrik
Fredrik is offline
#1
Feb17-13, 07:35 AM
Emeritus
Sci Advisor
PF Gold
Fredrik's Avatar
P: 9,018
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
{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.
Phys.Org News Partner Science news on Phys.org
SensaBubble: It's a bubble, but not as we know it (w/ video)
The hemihelix: Scientists discover a new shape using rubber bands (w/ video)
Microbes provide insights into evolution of human language
Bill Simpson
Bill Simpson is offline
#2
Feb17-13, 01:13 PM
P: 973
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?
Fredrik
Fredrik is offline
#3
Feb17-13, 03:46 PM
Emeritus
Sci Advisor
PF Gold
Fredrik's Avatar
P: 9,018
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".

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.

Bill Simpson
Bill Simpson is offline
#4
Feb17-13, 09:39 PM
P: 973

Simplify in Mathematica


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?
Fredrik
Fredrik is offline
#5
Feb17-13, 10:09 PM
Emeritus
Sci Advisor
PF Gold
Fredrik's Avatar
P: 9,018
p is the 31 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.
Bill Simpson
Bill Simpson is offline
#6
Feb18-13, 12:01 AM
P: 973
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."
Fredrik
Fredrik is offline
#7
Feb18-13, 12:14 AM
Emeritus
Sci Advisor
PF Gold
Fredrik's Avatar
P: 9,018
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
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.
Fredrik
Fredrik is offline
#8
Feb19-13, 09:40 AM
Emeritus
Sci Advisor
PF Gold
Fredrik's Avatar
P: 9,018
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.
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.
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.
Simplify[v^2/Abs[v], v\[Element]Reals && v!=0]
Why do I get ##v^2/|v|## instead of just ##|v|##?
DaleSpam
DaleSpam is offline
#9
Feb19-13, 03:59 PM
Mentor
P: 16,488
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.

Quote Quote by Fredrik View Post
As you can see, I even tried to tell it that the thing under the square roots is positive.
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.
Fredrik
Fredrik is offline
#10
Feb19-13, 07:11 PM
Emeritus
Sci Advisor
PF Gold
Fredrik's Avatar
P: 9,018
Quote Quote by DaleSpam View Post
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.
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.

Quote Quote by DaleSpam View Post
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.
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.
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.
DaleSpam
DaleSpam is offline
#11
Feb20-13, 08:45 AM
Mentor
P: 16,488
Quote Quote by Fredrik View Post
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.
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:
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:

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:
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}
Fredrik
Fredrik is offline
#12
Feb20-13, 11:35 AM
Emeritus
Sci Advisor
PF Gold
Fredrik's Avatar
P: 9,018
Wow. FullSimplify really worked much better on this expression. Thanks for the tip. That substitution thing is interesting too.
Ben Niehoff
Ben Niehoff is offline
#13
Feb20-13, 01:38 PM
Sci Advisor
P: 1,563
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

var -> (some long expression)


Register to reply

Related Discussions
MATHEMATICA : Forcing Logarithms to Simplify Math & Science Software 3
Simplify this expression with Mathematica Math & Science Software 4
Help urgently needed in Mathematica Simplify Math & Science Software 1
Mathematica can't simplify Math & Science Software 4
How to simplify an expression in Mathematica Math & Science Software 15