I am noticing some odd behavior when trying to vectorize the following bump function. It should return positive values for any inputs in the open interval (-1,1), and return 0 for inputs elsewhere:
>>import numpy as np
>>def bump(x):
if np.abs(x)<1:
return np.exp(-1/(1-x**2))
else:
return 0
>>vbump=np.vectorize(bump)
When I then try to evaluate the function vbump on an array which contains only values in (-1,1) it behaves as expected:
>>x=np.linspace(-0.99,0.99,10)
>>vbump(x)
array([ 1.50022600e-22, 8.57431637e-02, 2.38427081e-01,
3.25559999e-01, 3.63401061e-01, 3.63401061e-01,
3.25559999e-01, 2.38427081e-01, 8.57431637e-02,
1.50022600e-22])
On the other hand, when I try to evaluate it on an array which contains values outside of (-1,1) I get all zeros, even for the values which should be positive:
>>x2=np.linspace(-1.1,1.1,10)
>>vbump(x2)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
Why is it behaving like this? I'm fairly new to python and numpy, but from my understanding when I vectorize a function it should allow the function to be threaded over lists element-wise. Why is putting a larger array in affecting the element-wise evaluations elsewhere?
I should add that I have found a solution to the immediate problem of evaluating the bump function over an array, using
>>np.piecewise(x, [x <=-1, x >= 1,(-1<x)&(x<1)], [0,0,lambda t:np.exp(-1/(1-t**2))])
but I'd still like to understand what is going on with my initial approach.