On Mar 21, 2010, at 2:13 PM, Ben Goodrich wrote:

> Hi,
> 
> I would like to write a function that rewrites an expression to
> eliminate square root signs. I have read through the simplify module,
> but I don't quite see how to do this. Is there something in the new
> polys module that would help me?
> 
> Here is an example. I can assume that everything is real if that helps
> somehow.
> 
> Sigma_12 = Symbol("Sigma_12")
> Sigma_13 = Symbol("Sigma_13")
> Sigma_14 = Symbol("Sigma_14")
> Sigma_23 = Symbol("Sigma_23")
> Sigma_24 = Symbol("Sigma_24")
> Sigma_34 = Symbol("Sigma_34")
> 
> tau_11 = Symbol("tau_11")
> tau_22 = Symbol("tau_22")
> tau_33 = Symbol("tau_33")
> tau_44 = Symbol("tau_44")
> 
> expr = ((Sigma_34/(tau_33*tau_44) - Sigma_13*Sigma_14/
> (tau_11**2*tau_33*tau_44))/((1 - Sigma_13**2/(tau_11**2*tau_33**2))*(1
> - Sigma_14**2/(tau_11**2*tau_44**2)))**(S(1)/2) - (Sigma_24/
> (tau_22*tau_44) - Sigma_12*Sigma_14/
> (tau_11**2*tau_22*tau_44))*(Sigma_23/(tau_22*tau_33) -
> Sigma_12*Sigma_13/(tau_11**2*tau_22*tau_33))/(((1 - Sigma_12**2/
> (tau_11**2*tau_22**2))*(1 - Sigma_13**2/(tau_11**2*tau_33**2)))**(S(1)/
> 2)*((1 - Sigma_12**2/(tau_11**2*tau_22**2))*(1 - Sigma_14**2/
> (tau_11**2*tau_44**2)))**(S(1)/2)))**2
> 
> expr
> 
> 
> 
> So if I wanted to simplify expr to eliminate the radicals, I would
> need to
> 
> 0) Combine these last two two square roots together
> 
> ((1 - Sigma_12**2/(tau_11**2*tau_22**2))*(1 - Sigma_13**2/
> (tau_11**2*tau_33**2)))**(S(1)/2)*((1 - Sigma_12**2/
> (tau_11**2*tau_22**2))*(1 - Sigma_14**2/(tau_11**2*tau_44**2)))**(S(1)/
> 2)
> 
> to get
> 
> ((1 - Sigma_12**2/(tau_11**2*tau_22**2))*(1 - Sigma_13**2/
> (tau_11**2*tau_33**2))*(1 - Sigma_12**2/(tau_11**2*tau_22**2))*(1 -
> Sigma_14**2/(tau_11**2*tau_44**2)))**(S(1)/2)
powsimp(((1 - Sigma_12**2/(tau_11**2*tau_22**2))*(1 - Sigma_13**2/\
(tau_11**2*tau_33**2)))**(S(1)/2)*((1 - Sigma_12**2/\
(tau_11**2*tau_22**2))*(1 - Sigma_14**2/(tau_11**2*tau_44**2)))**(S(1)/\
2))

will do this.  You need to assume the symbols are positive to simplify 
sqrt(x**2).  In this case, it will give:

((1 - Sigma_13**2/(tau_11**2*tau_33**2))*(1 - Sigma_14**2/(
tau_11**2*tau_44**2)))**(S(1)/2)*abs(1 - Sigma_12**2/(
tau_11**2*tau_22**2))

Ideally, something like 
refine(powsimp(expr.args[0]), Assume(1 - Sigma_12**2/tau_11**2/tau_22**2, 
Q.positive))**2

would help you eliminate the absolute value (which I am assuming you want to 
do), 
but it seems that the new assumptions aren't quite there yet.

Also, maybe cse() could be of assistance.  For example, it takes a bit of work 
(or rather introspection) on your part to get to it, but this works:

together(refine(expand(powsimp(a[1][0].args[0])**2, multinomial=False, 
mul=False), \
Assume(a[0][15][0], Q.positive))).subs(dict(a[0])).subs(dict(a[0]))

(hopefully cse will return the same thing on your machine as on mine).  

But yes, you are right that assumptions are important if you want to simplify 
expressions with square roots.

> 1) Recognize that there is now a squared term under the radical and
> pull that out to get
> 
> (1 - Sigma_12**2/(tau_11**2*tau_22**2)) * ((1 - Sigma_13**2/
> (tau_11**2*tau_33**2))*(1 - Sigma_14**2/(tau_11**2*tau_44**2)))**(S(1)/
> 2)
> 
> 2) Make a common denominator with the first term in expr to get this
> within the outermost parentheses
> 
> ((1 - Sigma_12**2/(tau_11**2*tau_22**2)) * (Sigma_34/(tau_33*tau_44) -
> Sigma_13*Sigma_14/(tau_11**2*tau_33*tau_44)) - (Sigma_23/
> (tau_22*tau_33) - Sigma_12*Sigma_13/(tau_11**2*tau_22*tau_33))) / (((1
> - Sigma_13**2/(tau_11**2*tau_33**2))*(1 - Sigma_14**2/
> (tau_11**2*tau_44**2)))**(S(1)/2) * (1 - Sigma_12**2/
> (tau_11**2*tau_22**2)))
> 
> 3) Square both the numerator and denominator to finally get
> 
> (((1 - Sigma_12**2/(tau_11**2*tau_22**2)) * (Sigma_34/(tau_33*tau_44)
> - Sigma_13*Sigma_14/(tau_11**2*tau_33*tau_44)) - (Sigma_23/
> (tau_22*tau_33) - Sigma_12*Sigma_13/(tau_11**2*tau_22*tau_33))) / (((1
> - Sigma_13**2/(tau_11**2*tau_33**2))*(1 - Sigma_14**2/
> (tau_11**2*tau_44**2)))**(S(1)/2) * (1 - Sigma_12**2/
> (tau_11**2*tau_22**2))))**2
> 
> That would be perfect if I could automate these steps, or it would
> also be okay if it wanted to expand or simplify the numerator further.
> But I haven't been able to figure out how to do that without manual
> intervention. Everything I have tried takes a long time to yield a
> giant expression that still has square root signs. Can anyone get me
> started?

Sometimes, if you only want to simplify a part of an expression, you have to 
pull it apart with
.args and then put it back together again.  

Aaron Meurer
> 
> 
> Thanks,
> Ben
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to 
> sympy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sympy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to