zero division: warning and exception

Hi list,
I have to make a division that sometimes yields and inf, and I want to replace it by 0.
I have try this:

···

import pylab as p
p.seterr(divide=‘raise’)
l = array vector defined along the program
try:
a = (drRdl)/(1.-((R0/R)*p.sin(l))2)(1./2)
except FloatingPointError:
a=0

It works, but it doesn’t return an array as expect, if some of the values are zero, then a = 0.
So I tried:

a = p.zeros(len(l))
for i in range(len(l)):
try:
a[i] = (drRdl)/(1.-((R0/R)*p.sin(l[i]))2)(1./2)
except FloatingPointError:
a[i]=0

But doing it this way I’m not able to get an exception:
array([ Inf])
And I don’t know what I have to change to get an exception doing things this way.

Thank you,
Illa

Hi list,
I have to make a division that sometimes yields and inf, and I want to
replace it by 0.
I have try this:
---------------------------------------
import pylab as p
p.seterr(divide='raise')
l = array vector defined along the program
try:
a = (dr*R*dl)/(1.-((R0/R)*p.sin(l))**2)**(1./2)

Does your code fail with a ZeroDivisionError or simply fill the array
with infs. You could try replacing the infs with zeros with

  mask = np.isinf(x)
  x[mask] = 0.

Eg,

In [1]: import numpy as np

In [2]: x = np.arange(0., 1., 0.1)

In [3]: y = 1/x

In [4]: y
Out[4]:
array([ Inf, 10. , 5. , 3.33333333,
         2.5 , 2. , 1.66666667, 1.42857143,
         1.25 , 1.11111111])

In [5]: mask = np.isinf(y)

In [6]: y[mask] = 0.

In [7]: y
Out[7]:
array([ 0. , 10. , 5. , 3.33333333,
         2.5 , 2. , 1.66666667, 1.42857143,
         1.25 , 1.11111111])

JDH

···

On Thu, May 14, 2009 at 7:26 PM, darkside <in.the.darkside@...287...> wrote:

except FloatingPointError:
        a=0

---------------------------------
It works, but it doesn't return an array as expect, if some of the values
are zero, then a = 0.
So I tried:
--------------------------
a = p.zeros(len(l))
for i in range(len(l)):
try:
a[i] = (dr*R*dl)/(1.-((R0/R)*p.sin(l[i]))**2)**(1./2)
except FloatingPointError:
a[i]=0
--------------------------------
But doing it this way I'm not able to get an exception:
array([ Inf])
And I don't know what I have to change to get an exception doing things this
way.

Thank you,
Illa

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

darkside schrieb:

Hi list,
I have to make a division that sometimes yields and inf, and I want to
replace it by 0.
I have try this:
---------------------------------------
import pylab as p
p.seterr(divide='raise')
l = array vector defined along the program
    try:
            a = (dr*R*dl)/(1.-((R0/R)*p.sin(l))**2)**(1./2)
    except FloatingPointError:
            a=0
---------------------------------
It works, but it doesn't return an array as expect, if some of the values
are zero, then a = 0.
So I tried:
--------------------------
a = p.zeros(len(l))
    for i in range(len(l)):
        try:
            a[i] = (dr*R*dl)/(1.-((R0/R)*p.sin(l[i]))**2)**(1./2)
        except FloatingPointError:
            a[i]=0
--------------------------------
But doing it this way I'm not able to get an exception:
array([ Inf])
And I don't know what I have to change to get an exception doing things this
way.

You can do it that way:
a = rand(5,5)
b = a.round()
c = a/b
c[isinf(c)] = 0 # use indexing with boolean array [1] to set zero

In your case:
a = (dr*R*dl)/(1.-((R0/R)*p.sin(l))**2)**(1./2)
a[isinf(a)] = 0

HTH
Armin

[1]<http://www.scipy.org/Tentative_NumPy_Tutorial#head-0dffc419afa7d77d51062d40d2d84143db8216c2>