[SciPy-user] Divide by zero is inf?

Alexander Borghgraef alexander.borghgraef.rma at gmail.com
Mon Dec 12 10:01:44 CST 2005


Hi all,

 I'm trying to convert a field of 2Dvectors (3D-array) from Cartesian to
pole coordinates. Now I would like to use as
few for loops as possible, and use ufuncs instead. However, in calculating
the angle coordinate via the formula
"theta = arctan(y/x)", there is a risk of division by zero. In a for-loop
scheme, this is easily solved by adding a test inside
the loop, checking for x == 0, but there's no such thing for a ufunc scheme.

 Now, I am aware of the existence of "inf" in scipy, and  since "arctan(inf)
== pi /2", there should be no problem with the
following code:

vectorField = array( [ zeros( [10,10] ), ones( [10,10] ) ] )
 poleField = array( [ sqrt( sum( vectorField **2 ) ), arctan( vectorField[
1, :, :] / vectorField[ 0, :, :]  ) ] )

 However, I can't seem to find out how to do division by zero resulting in
"inf", I always get a "ZeroDivisionError"
exception. How do I do this? Any other suggestions of solving similar
problems with ufuncs? I'm currently using scipy 0.3.2,
with Numeric 24.1.

 --
Alex Borghgraef
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.scipy.net/pipermail/scipy-user/attachments/20051212/8124b9d0/attachment-0001.htm


More information about the SciPy-user mailing list