ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 09 Jun 2012 18:30:29 +0200simplify trig, abs, and sqrt expressionhttps://ask.sagemath.org/question/9031/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)]
Tue, 05 Jun 2012 13:32:00 +0200https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/Answer by daniel.e2718 for <p>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 <code>simplify_full</code> followed by <code>simplify_trig</code>. My code is below. What I've done is not elegant. Can anyone suggest a cleaner approach?</p>
<pre><code>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
</code></pre>
<p>The result is:</p>
<pre><code>[-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)]
</code></pre>
https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?answer=13655#post-id-13655I'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.Tue, 05 Jun 2012 22:40:33 +0200https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?answer=13655#post-id-13655Comment by Eviatar Bach for <p>I'm not sure what the question here is, exactly... but simply defining a function should be easy enough.</p>
<pre><code>def simp(f):
return f.simplify_full().simplify_trig()
</code></pre>
<p>'simp' isn't used by sage, so that could be your "master simplify" function if you want.</p>
https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?comment=19678#post-id-19678It 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.Wed, 06 Jun 2012 19:49:42 +0200https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?comment=19678#post-id-19678Comment by daniel.e2718 for <p>I'm not sure what the question here is, exactly... but simply defining a function should be easy enough.</p>
<pre><code>def simp(f):
return f.simplify_full().simplify_trig()
</code></pre>
<p>'simp' isn't used by sage, so that could be your "master simplify" function if you want.</p>
https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?comment=19661#post-id-19661Perhaps a parameter? simplify_full(expression, **loop=True**)Thu, 07 Jun 2012 17:37:38 +0200https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?comment=19661#post-id-19661Comment by calc314 for <p>I'm not sure what the question here is, exactly... but simply defining a function should be easy enough.</p>
<pre><code>def simp(f):
return f.simplify_full().simplify_trig()
</code></pre>
<p>'simp' isn't used by sage, so that could be your "master simplify" function if you want.</p>
https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?comment=19684#post-id-19684I 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!Wed, 06 Jun 2012 09:53:42 +0200https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?comment=19684#post-id-19684Answer by Eviatar Bach for <p>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 <code>simplify_full</code> followed by <code>simplify_trig</code>. My code is below. What I've done is not elegant. Can anyone suggest a cleaner approach?</p>
<pre><code>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
</code></pre>
<p>The result is:</p>
<pre><code>[-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)]
</code></pre>
https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?answer=13662#post-id-13662Here'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()Wed, 06 Jun 2012 20:03:34 +0200https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?answer=13662#post-id-13662Comment by calc314 for <p>Here's a function you can use which will keep simplifying until it can't any longer:</p>
<pre><code>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()
</code></pre>
https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?comment=19674#post-id-19674This is a great idea. Seems like this would be a nice option to have built into Sage in a future version!Wed, 06 Jun 2012 22:37:25 +0200https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?comment=19674#post-id-19674Comment by Eviatar Bach for <p>Here's a function you can use which will keep simplifying until it can't any longer:</p>
<pre><code>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()
</code></pre>
https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?comment=19635#post-id-19635Thanks. I opened up a trac ticket for this: http://trac.sagemath.org/sage_trac/ticket/13099Sat, 09 Jun 2012 18:30:29 +0200https://ask.sagemath.org/question/9031/simplify-trig-abs-and-sqrt-expression/?comment=19635#post-id-19635