numpy.where does not filter points == 0 out

45 Views Asked by At

using numpy.where() to filter out cases where the input array element is == 0 in order to avoid divisin by 0 does not work.

using a simple for loop everything is fine, but using np.where() the RuntimeWarning appears. I tried explicitly defining the array dtype but the warning still appears. Why is this the case?

# test array
x = np.array([1e-20, 1, 10, 0], dtype=np.float64)

# divide by x except where x == 0 
np.where(x == 0, 1/np.pi, np.sin(x)/(np.pi*x))
# returns correct values but with RuntimeWarning 

# for loop works with no warnings appearing
for x_i in x:
    if x_i == 0:
        print(1/np.pi)
    else:
        print(np.sin(x_i)/(np.pi*x_i))
0

There are 0 best solutions below