Ask Your Question
1

atan2 bug?

asked 2011-06-02 16:54:18 +0100

mhfrey gravatar image

updated 2023-01-10 00:01:08 +0100

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

4 Answers

Sort by ยป oldest newest most voted
2

answered 2011-06-02 17:04:24 +0100

kcrisman gravatar image

updated 2011-06-02 17:05:41 +0100

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

Comments

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 17:25:56 +0100 )edit
1

answered 2013-06-03 18:19:59 +0100

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

answered 2011-06-03 09:48:20 +0100

kcrisman gravatar image

updated 2011-06-03 09:49:23 +0100

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.

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.

edit flag offensive delete link more
0

answered 2011-06-02 17:45:51 +0100

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/___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) 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

Stats

Asked: 2011-06-02 16:54:18 +0100

Seen: 1,192 times

Last updated: Jun 03 '13