Ask Your Question

atan2 bug?

asked 2011-06-02 09:54:18 -0600

mhfrey gravatar image

updated 2013-06-03 11:20:15 -0600

tmonteil gravatar image

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
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))
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)]
edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted

answered 2011-06-02 10:04:24 -0600

kcrisman gravatar image

updated 2011-06-02 10:05:41 -0600

Your 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.

edit flag offensive delete link more


This 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.

mhfrey gravatar imagemhfrey ( 2011-06-02 10:25:56 -0600 )edit

answered 2011-06-03 02:48:20 -0600

kcrisman gravatar image

updated 2011-06-03 02:49:23 -0600

Thanks 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.

    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)
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
    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.

edit flag offensive delete link more

answered 2013-06-03 11:19:59 -0600

tmonteil gravatar image

This 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)  
edit flag offensive delete link more

answered 2011-06-02 10:45:51 -0600

mhfrey gravatar image

The explicit error is:

Traceback (most recent call last): ps_a=phi_p+4.0dFx

File "", line 1, in <module>

File "/tmp/tmp7lgeO4/", 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) undefined

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-06-02 09:54:18 -0600

Seen: 440 times

Last updated: Jun 03 '13