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.Thu, 21 Aug 2014 20:09:42 +0200Simplification of atanhttps://ask.sagemath.org/question/23869/simplification-of-atan/ I'm teaching a course on dynamical systems based on the book by Devaney. Today I tried to show the map $f(\theta)=2\theta$ on the circle using Sage, where $\theta$ is given in radians. I could not find functions already defined to perform addition in $\mathbb{R}$ modulo $2\pi$, so I tried to devise my own. I stumbled upon this problem:
atan(sin(1/5*pi)/cos(1/5*pi))
produces
arctan(4*sin(1/5*pi)/(sqrt(5) + 1)).
Is there a way to obtain `1/5*pi`?
(By the way, _.simplify() does nothing in this case.)
Thu, 21 Aug 2014 03:13:28 +0200https://ask.sagemath.org/question/23869/simplification-of-atan/Answer by rws for <p>I'm teaching a course on dynamical systems based on the book by Devaney. Today I tried to show the map $f(\theta)=2\theta$ on the circle using Sage, where $\theta$ is given in radians. I could not find functions already defined to perform addition in $\mathbb{R}$ modulo $2\pi$, so I tried to devise my own. I stumbled upon this problem:</p>
<pre><code>atan(sin(1/5*pi)/cos(1/5*pi))
</code></pre>
<p>produces</p>
<pre><code>arctan(4*sin(1/5*pi)/(sqrt(5) + 1)).
</code></pre>
<p>Is there a way to obtain <code>1/5*pi</code>? </p>
<p>(By the way, _.simplify() does nothing in this case.)</p>
https://ask.sagemath.org/question/23869/simplification-of-atan/?answer=23873#post-id-23873If you do much symbolic manipulation you should read about Sage's subsystem Maxima. There is however a catch with your case.
sage: ex=atan(sin(1/5*pi)/cos(1/5*pi, hold=True))
sage: m=ex.maxima_methods()
sage: m.trigreduce()
1/5*pi
The problem is that `cos(pi/5)` gets immediately simplified to `(sqrt(5)+1)/4` in Sage before Maxima can be called. This can be prevented by saying `cos(pi/5,hold=True)`.
Thu, 21 Aug 2014 08:57:59 +0200https://ask.sagemath.org/question/23869/simplification-of-atan/?answer=23873#post-id-23873Comment by Rafael for <p>If you do much symbolic manipulation you should read about Sage's subsystem Maxima. There is however a catch with your case.</p>
<pre><code>sage: ex=atan(sin(1/5*pi)/cos(1/5*pi, hold=True))
sage: m=ex.maxima_methods()
sage: m.trigreduce()
1/5*pi
</code></pre>
<p>The problem is that <code>cos(pi/5)</code> gets immediately simplified to <code>(sqrt(5)+1)/4</code> in Sage before Maxima can be called. This can be prevented by saying <code>cos(pi/5,hold=True)</code>. </p>
https://ask.sagemath.org/question/23869/simplification-of-atan/?comment=23889#post-id-23889Thanks. Unfortunately, it seems that the solution is not general enough. For example. `arctan(sin(2,hold=True)/cos(2,hold=True)).trigreduce()` gives an error
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'trigreduce'
instead of returning 2.Thu, 21 Aug 2014 19:45:25 +0200https://ask.sagemath.org/question/23869/simplification-of-atan/?comment=23889#post-id-23889Comment by kcrisman for <p>If you do much symbolic manipulation you should read about Sage's subsystem Maxima. There is however a catch with your case.</p>
<pre><code>sage: ex=atan(sin(1/5*pi)/cos(1/5*pi, hold=True))
sage: m=ex.maxima_methods()
sage: m.trigreduce()
1/5*pi
</code></pre>
<p>The problem is that <code>cos(pi/5)</code> gets immediately simplified to <code>(sqrt(5)+1)/4</code> in Sage before Maxima can be called. This can be prevented by saying <code>cos(pi/5,hold=True)</code>. </p>
https://ask.sagemath.org/question/23869/simplification-of-atan/?comment=23884#post-id-23884Nice! I suggest adding this to the documentation somewhere. Very interesting, I am surprised Sage/Ginac does that reduction - why not for pi/8?Thu, 21 Aug 2014 15:57:15 +0200https://ask.sagemath.org/question/23869/simplification-of-atan/?comment=23884#post-id-23884Answer by tmonteil for <p>I'm teaching a course on dynamical systems based on the book by Devaney. Today I tried to show the map $f(\theta)=2\theta$ on the circle using Sage, where $\theta$ is given in radians. I could not find functions already defined to perform addition in $\mathbb{R}$ modulo $2\pi$, so I tried to devise my own. I stumbled upon this problem:</p>
<pre><code>atan(sin(1/5*pi)/cos(1/5*pi))
</code></pre>
<p>produces</p>
<pre><code>arctan(4*sin(1/5*pi)/(sqrt(5) + 1)).
</code></pre>
<p>Is there a way to obtain <code>1/5*pi</code>? </p>
<p>(By the way, _.simplify() does nothing in this case.)</p>
https://ask.sagemath.org/question/23869/simplification-of-atan/?answer=23877#post-id-23877The unit circle is better handled as a set of complex numbers (and doubling the angle is equivalent to squaring), so you can try to play with:
sage: x = exp(I*pi/5)
sage: f = lambda x : x^2
sage: f(x)
e^(2/5*I*pi)
sage: f(f(x))
e^(4/5*I*pi)
sage: f(f(f(x)))
e^(8/5*I*pi)
You can also stay on the real line by working modulo $1$ (not $2\pi$, this rescaling does not change the nature of the dynamical system), and use the fractional part:
sage: f = lambda x : (2*x) - int(2*x)
sage: f(0.2)
0.400000000000000
sage: f(0.8)
0.600000000000000
sage: f(5/4)
1/2
sage: f(pi)
2*pi - 6
You will enjoy a nice phenomenon : the orbit of every numerical number goes very fast to zero, because of the nature of floating-point numbers, which is also a nice way to explain why we should be very careful about numerical simulations in dynamical systems (note that standard floating points numbers have 53 bits of precision):
sage: x = RDF.random_element() ; x
0.963562235713
sage: for i in range(60):
....: x = f(x)
....: print(x)
Thu, 21 Aug 2014 13:37:20 +0200https://ask.sagemath.org/question/23869/simplification-of-atan/?answer=23877#post-id-23877Comment by tmonteil for <p>The unit circle is better handled as a set of complex numbers (and doubling the angle is equivalent to squaring), so you can try to play with:</p>
<pre><code>sage: x = exp(I*pi/5)
sage: f = lambda x : x^2
sage: f(x)
e^(2/5*I*pi)
sage: f(f(x))
e^(4/5*I*pi)
sage: f(f(f(x)))
e^(8/5*I*pi)
</code></pre>
<p>You can also stay on the real line by working modulo $1$ (not $2\pi$, this rescaling does not change the nature of the dynamical system), and use the fractional part:</p>
<pre><code>sage: f = lambda x : (2*x) - int(2*x)
sage: f(0.2)
0.400000000000000
sage: f(0.8)
0.600000000000000
sage: f(5/4)
1/2
sage: f(pi)
2*pi - 6
</code></pre>
<p>You will enjoy a nice phenomenon : the orbit of every numerical number goes very fast to zero, because of the nature of floating-point numbers, which is also a nice way to explain why we should be very careful about numerical simulations in dynamical systems (note that standard floating points numbers have 53 bits of precision):</p>
<pre><code>sage: x = RDF.random_element() ; x
0.963562235713
sage: for i in range(60):
....: x = f(x)
....: print(x)
</code></pre>
https://ask.sagemath.org/question/23869/simplification-of-atan/?comment=23890#post-id-23890You can do f(f(f(f(x)))).simplify() which will result in e^(-4/5*I*pi)Thu, 21 Aug 2014 20:09:42 +0200https://ask.sagemath.org/question/23869/simplification-of-atan/?comment=23890#post-id-23890Comment by Rafael for <p>The unit circle is better handled as a set of complex numbers (and doubling the angle is equivalent to squaring), so you can try to play with:</p>
<pre><code>sage: x = exp(I*pi/5)
sage: f = lambda x : x^2
sage: f(x)
e^(2/5*I*pi)
sage: f(f(x))
e^(4/5*I*pi)
sage: f(f(f(x)))
e^(8/5*I*pi)
</code></pre>
<p>You can also stay on the real line by working modulo $1$ (not $2\pi$, this rescaling does not change the nature of the dynamical system), and use the fractional part:</p>
<pre><code>sage: f = lambda x : (2*x) - int(2*x)
sage: f(0.2)
0.400000000000000
sage: f(0.8)
0.600000000000000
sage: f(5/4)
1/2
sage: f(pi)
2*pi - 6
</code></pre>
<p>You will enjoy a nice phenomenon : the orbit of every numerical number goes very fast to zero, because of the nature of floating-point numbers, which is also a nice way to explain why we should be very careful about numerical simulations in dynamical systems (note that standard floating points numbers have 53 bits of precision):</p>
<pre><code>sage: x = RDF.random_element() ; x
0.963562235713
sage: for i in range(60):
....: x = f(x)
....: print(x)
</code></pre>
https://ask.sagemath.org/question/23869/simplification-of-atan/?comment=23888#post-id-23888Thanks. Trying to follow your first suggestion, do you know a way to simplify further iterations? Since `f(f(f(f(x)))` results in `e^(16/5*I*pi)` and not, say, `e^(6/5*I*pi)`.Thu, 21 Aug 2014 19:39:11 +0200https://ask.sagemath.org/question/23869/simplification-of-atan/?comment=23888#post-id-23888