ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 03 Jun 2013 11:19:59 -0500atan2 bug?http://ask.sagemath.org/question/8146/atan2-bug/The following generated an error, what am I doing wrong?
Tst = 256
phi_p = 0.0
dF= pi
State = var('State')
x = 1 - 2 * State / Tst
ps_a=phi_p+4.0*dF*x
ps_b=phi_p+3.0*dF*x
ps_c=phi_p+2.0*dF*x
ps_d=phi_p+1.0*dF*x
ps_e=phi_p+0.0*dF*x
ps_f=phi_p-1.0*dF*x
ps_g=phi_p-2.0*dF*x
ps_h=phi_p-3.0*dF*x
ps_i=phi_p-4.0*dF*x
px = (cos(ps_a)/2+cos(ps_b)+cos(ps_c)+cos(ps_d)+cos(ps_e)+cos(ps_f)+cos(ps_g)+cos(ps_h)+cos(ps_i)/2)
py = (sin(ps_a)/2+sin(ps_b)+sin(ps_c)+sin(ps_d)+sin(ps_e)+sin(ps_f)+sin(ps_g)+sin(ps_h)+sin(ps_i)/2)
ps_pc= (atan2 (py, px))
rfs_pc=sqrt(px*px+py*py)/8
amp = (rfs_pc*cos(ps_pc))
This Breaks:
AmpP =[amp(State=r).n() for r in srange(Tst)]
This works, but I need "amp " in another place and it returns the same error.
AmpP =[((rfs_pc(State=r)).n()*cos(ps_pc(State=r))).n() for r in srange(Tst)]
Thu, 02 Jun 2011 09:54:18 -0500http://ask.sagemath.org/question/8146/atan2-bug/Answer by kcrisman for <p>The following generated an error, what am I doing wrong?</p>
<pre><code>Tst = 256
phi_p = 0.0
dF= pi
State = var('State')
x = 1 - 2 * State / Tst
ps_a=phi_p+4.0*dF*x
ps_b=phi_p+3.0*dF*x
ps_c=phi_p+2.0*dF*x
ps_d=phi_p+1.0*dF*x
ps_e=phi_p+0.0*dF*x
ps_f=phi_p-1.0*dF*x
ps_g=phi_p-2.0*dF*x
ps_h=phi_p-3.0*dF*x
ps_i=phi_p-4.0*dF*x
px = (cos(ps_a)/2+cos(ps_b)+cos(ps_c)+cos(ps_d)+cos(ps_e)+cos(ps_f)+cos(ps_g)+cos(ps_h)+cos(ps_i)/2)
py = (sin(ps_a)/2+sin(ps_b)+sin(ps_c)+sin(ps_d)+sin(ps_e)+sin(ps_f)+sin(ps_g)+sin(ps_h)+sin(ps_i)/2)
ps_pc= (atan2 (py, px))
rfs_pc=sqrt(px*px+py*py)/8
amp = (rfs_pc*cos(ps_pc))
</code></pre>
<p>This Breaks:</p>
<pre><code>AmpP =[amp(State=r).n() for r in srange(Tst)]
</code></pre>
<p>This works, but I need "amp " in another place and it returns the same error.</p>
<pre><code>AmpP =[((rfs_pc(State=r)).n()*cos(ps_pc(State=r))).n() for r in srange(Tst)]
</code></pre>
http://ask.sagemath.org/question/8146/atan2-bug/?answer=12413#post-id-12413Thanks for the more explicit update, that clarifies something for me - I should have checked atan2(0,0) first!
In sage.symbolic.pynac.pyx, we have the following for the `py_atan2(0,0)` case.
else:
if sgn_x > 0:
return 0
elif x == 0:
raise ValueError, "arctan2(0,0) undefined"
Pynac is how we handle symbolics. So the problem is that you have a symbolic arctan2, which has (0,0) plugged in, and then is evaluated. Compare:
sage: atan2(0,0)
0
sage: atan2(0,0,hold=True)
arctan2(0, 0)
sage: atan2(0,0,hold=True).n()
ValueError: arctan2(0,0) undefined
Since Maxima also doesn't like this
sage: atan2(0,0,hold=True).simplify()
-----------------------------------------------------
TypeError: Error executing code in Maxima
CODE:
sage1 : atan2(0,0)$
Maxima ERROR:
atan2: atan2(0,0) is undefined.
-- an error. To debug this try: debugmode(true);
the zero behavior seems most wrong. Mathematica also returns various things, depending on where you look, that are not zero. I say you are right about atan2(0,0). At the very least we need consistency. Maybe Ginac/Pynac says it's zero? Anyway, this is now ticket 11423.
I don't know that this will help your computation. You may need to try something more sophisticated - like other branch cuts for the arctangent :( What would you *like* to get from this computation - perhaps in a simpler case that still has atan2(0,0), in principle.Fri, 03 Jun 2011 02:48:20 -0500http://ask.sagemath.org/question/8146/atan2-bug/?answer=12413#post-id-12413Answer by tmonteil for <p>The following generated an error, what am I doing wrong?</p>
<pre><code>Tst = 256
phi_p = 0.0
dF= pi
State = var('State')
x = 1 - 2 * State / Tst
ps_a=phi_p+4.0*dF*x
ps_b=phi_p+3.0*dF*x
ps_c=phi_p+2.0*dF*x
ps_d=phi_p+1.0*dF*x
ps_e=phi_p+0.0*dF*x
ps_f=phi_p-1.0*dF*x
ps_g=phi_p-2.0*dF*x
ps_h=phi_p-3.0*dF*x
ps_i=phi_p-4.0*dF*x
px = (cos(ps_a)/2+cos(ps_b)+cos(ps_c)+cos(ps_d)+cos(ps_e)+cos(ps_f)+cos(ps_g)+cos(ps_h)+cos(ps_i)/2)
py = (sin(ps_a)/2+sin(ps_b)+sin(ps_c)+sin(ps_d)+sin(ps_e)+sin(ps_f)+sin(ps_g)+sin(ps_h)+sin(ps_i)/2)
ps_pc= (atan2 (py, px))
rfs_pc=sqrt(px*px+py*py)/8
amp = (rfs_pc*cos(ps_pc))
</code></pre>
<p>This Breaks:</p>
<pre><code>AmpP =[amp(State=r).n() for r in srange(Tst)]
</code></pre>
<p>This works, but I need "amp " in another place and it returns the same error.</p>
<pre><code>AmpP =[((rfs_pc(State=r)).n()*cos(ps_pc(State=r))).n() for r in srange(Tst)]
</code></pre>
http://ask.sagemath.org/question/8146/atan2-bug/?answer=15020#post-id-15020This bug was fixed in sage-5.1 :
sage: arctan2(0,0)
RuntimeError: arctan2_eval(): arctan2(0,0) encountered
sage: atan2(0,0)
RuntimeError: arctan2_eval(): arctan2(0,0) encountered
sage: maxima.atan2(0,0)
TypeError: Error executing code in Maxima
Hence, everyone agrees that `arctan2(0,0)` is not defined. Except python:
sage: math.atan2(0,0)
0.0
Mon, 03 Jun 2013 11:19:59 -0500http://ask.sagemath.org/question/8146/atan2-bug/?answer=15020#post-id-15020Answer by mhfrey for <p>The following generated an error, what am I doing wrong?</p>
<pre><code>Tst = 256
phi_p = 0.0
dF= pi
State = var('State')
x = 1 - 2 * State / Tst
ps_a=phi_p+4.0*dF*x
ps_b=phi_p+3.0*dF*x
ps_c=phi_p+2.0*dF*x
ps_d=phi_p+1.0*dF*x
ps_e=phi_p+0.0*dF*x
ps_f=phi_p-1.0*dF*x
ps_g=phi_p-2.0*dF*x
ps_h=phi_p-3.0*dF*x
ps_i=phi_p-4.0*dF*x
px = (cos(ps_a)/2+cos(ps_b)+cos(ps_c)+cos(ps_d)+cos(ps_e)+cos(ps_f)+cos(ps_g)+cos(ps_h)+cos(ps_i)/2)
py = (sin(ps_a)/2+sin(ps_b)+sin(ps_c)+sin(ps_d)+sin(ps_e)+sin(ps_f)+sin(ps_g)+sin(ps_h)+sin(ps_i)/2)
ps_pc= (atan2 (py, px))
rfs_pc=sqrt(px*px+py*py)/8
amp = (rfs_pc*cos(ps_pc))
</code></pre>
<p>This Breaks:</p>
<pre><code>AmpP =[amp(State=r).n() for r in srange(Tst)]
</code></pre>
<p>This works, but I need "amp " in another place and it returns the same error.</p>
<pre><code>AmpP =[((rfs_pc(State=r)).n()*cos(ps_pc(State=r))).n() for r in srange(Tst)]
</code></pre>
http://ask.sagemath.org/question/8146/atan2-bug/?answer=12406#post-id-12406The explicit error is:
Traceback (most recent call last): ps_a=phi_p+4.0*dF*x
File "", line 1, in <module>
File "/tmp/tmp7lgeO4/___code___.py", line 26, in <module>
AmpP =[(amp(State=r)).n() for r in srange(Tst)]
File "expression.pyx", line 3902, in sage.symbolic.expression.Expression._numerical_approx (sage/symbolic/expression.cpp:17489)
File "expression.pyx", line 816, in sage.symbolic.expression.Expression._convert (sage/symbolic/expression.cpp:4951)
File "pynac.pyx", line 1369, in sage.symbolic.pynac.py_atan2 (sage/symbolic/pynac.cpp:12772)
ValueError: arctan2(0,0) undefinedThu, 02 Jun 2011 10:45:51 -0500http://ask.sagemath.org/question/8146/atan2-bug/?answer=12406#post-id-12406Answer by kcrisman for <p>The following generated an error, what am I doing wrong?</p>
<pre><code>Tst = 256
phi_p = 0.0
dF= pi
State = var('State')
x = 1 - 2 * State / Tst
ps_a=phi_p+4.0*dF*x
ps_b=phi_p+3.0*dF*x
ps_c=phi_p+2.0*dF*x
ps_d=phi_p+1.0*dF*x
ps_e=phi_p+0.0*dF*x
ps_f=phi_p-1.0*dF*x
ps_g=phi_p-2.0*dF*x
ps_h=phi_p-3.0*dF*x
ps_i=phi_p-4.0*dF*x
px = (cos(ps_a)/2+cos(ps_b)+cos(ps_c)+cos(ps_d)+cos(ps_e)+cos(ps_f)+cos(ps_g)+cos(ps_h)+cos(ps_i)/2)
py = (sin(ps_a)/2+sin(ps_b)+sin(ps_c)+sin(ps_d)+sin(ps_e)+sin(ps_f)+sin(ps_g)+sin(ps_h)+sin(ps_i)/2)
ps_pc= (atan2 (py, px))
rfs_pc=sqrt(px*px+py*py)/8
amp = (rfs_pc*cos(ps_pc))
</code></pre>
<p>This Breaks:</p>
<pre><code>AmpP =[amp(State=r).n() for r in srange(Tst)]
</code></pre>
<p>This works, but I need "amp " in another place and it returns the same error.</p>
<pre><code>AmpP =[((rfs_pc(State=r)).n()*cos(ps_pc(State=r))).n() for r in srange(Tst)]
</code></pre>
http://ask.sagemath.org/question/8146/atan2-bug/?answer=12405#post-id-12405Your error is
ValueError: arctan2(0,0) undefined
Well, that is true! The arctan of $0/0$ doesn't really make sense. Is it possible that you want
[amp(State=r).n() for r in srange(1,Tst)]
instead? (This doesn't raise an error.)
Remember, Python and Sage ranges start at zero.Thu, 02 Jun 2011 10:04:24 -0500http://ask.sagemath.org/question/8146/atan2-bug/?answer=12405#post-id-12405Comment by mhfrey for <p>Your error is </p>
<pre><code>ValueError: arctan2(0,0) undefined
</code></pre>
<p>Well, that is true! The arctan of $0/0$ doesn't really make sense. Is it possible that you want </p>
<pre><code>[amp(State=r).n() for r in srange(1,Tst)]
</code></pre>
<p>instead? (This doesn't raise an error.) </p>
<p>Remember, Python and Sage ranges start at zero.</p>
http://ask.sagemath.org/question/8146/atan2-bug/?comment=21633#post-id-21633This helps, but this is really a bug because atan2(0,0) works. The function can be tuned so that it will hit atan2(0,0) some where in the middle and blow up.Thu, 02 Jun 2011 10:25:56 -0500http://ask.sagemath.org/question/8146/atan2-bug/?comment=21633#post-id-21633