# simplify trig, abs, and sqrt expression

I'm computing the Frenet frame for a helix. Sage does not seem to want to simplify expressions like $\sqrt{|r^2 \sin(\theta)|^2+|r^2 \cos(\theta)^2|}$ without using simplify_full followed by simplify_trig. My code is below. What I've done is not elegant. Can anyone suggest a cleaner approach?

var('r,theta,x,y,t,R')
assume(r>0)
assume(R>0)
assume(t,'real')
f(t) = (R*cos(t),R*sin(t),t)
tangent=diff(f(t),t)
normal=diff(tangent,t)
binormal=tangent.cross_product(normal)
norm_of_tangent=tangent.norm().simplify_full().simplify_trig()
norm_of_normal=normal.norm().simplify_full().simplify_trig()
norm_of_binormal=binormal.norm().simplify_full().simplify_trig()
F=matrix([tangent/norm_of_tangent,normal/norm_of_normal,binormal/norm_of_binormal]).transpose()
F


The result is:

[-R*sin(t)/sqrt(R^2 + 1), -cos(t), sin(t)/sqrt(R^2 + 1)]
[R*cos(t)/sqrt(R^2 + 1), -sin(t), -cos(t)/sqrt(R^2 + 1)]
[1/sqrt(R^2 + 1), 0, (R^2*sin(t)^2 + R^2*cos(t)^2)/(sqrt(R^2 + 1)*R)]

edit retag close merge delete

Sort by » oldest newest most voted

Here's a function you can use which will keep simplifying until it can't any longer:

def simp(f):
old = f
new = old.simplify_full()
while 1:
if hash(new) == hash(old):
return old
else:
old = new
new = new.simplify_full()

more

This is a great idea. Seems like this would be a nice option to have built into Sage in a future version!

I'm not sure what the question here is, exactly... but simply defining a function should be easy enough.

def simp(f):
return f.simplify_full().simplify_trig()


'simp' isn't used by sage, so that could be your "master simplify" function if you want.

more

I see now that I wasn't very clear. I just wanted to know whether this was the simplest approach to simplifying the square root, absolute value, and trig expression that I mentioned. Using the simplify command twice seemed a bit awkward, and I figured there had to be a slick way around that. Of course, I do realize that automated simplification is difficult, and any CAS might have difficulty doing something like this in one step. Thanks for looking at it!

It occurs to me that it would be easy to add an option to simplify_full that would keep simplifying until the expression stopped changing (see my answer). Of course, this would make trivial simplifications take twice as long, but if it's just an option it wouldn't matter.

Perhaps a parameter? simplify_full(expression, **loop=True**)