I see a strange behavior with ufunc where
clause for Numpy 1.15.3
.
In [1]: import numpy as np
In [2]: x = np.array([[1,2],[3,4]])
In [3]: y = np.ones(x.shape) * 2
In [4]: print(x, "\n", y)
[[1 2]
[3 4]]
[[2. 2.]
[2. 2.]]
In [5]: np.add(x, y, where=x==3)
Out[5]:
array([[2., 2.], #<=========== where do these 2s come from???
[5., 2.]])
In [6]: np.add(x, y, where=x==3, out=np.zeros(x.shape))
Out[6]:
array([[0., 0.],
[5., 0.]])
In [7]: np.add(x, y, where=x==3, out=np.ones(x.shape))
Out[7]:
array([[1., 1.],
[5., 1.]])
In [8]: np.add(x, y, where=x==3)
Out[8]:
array([[1., 1.], # <========= it seems these 1s are remembered from last computation.
[5., 1.]])
ADD1
It seems I can only get the rational result with the out
parameter.
Below is without the out
parameter:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.linspace(-2,2,60)
y = np.linspace(-2,2,60)
xx, yy = np.meshgrid(x,y)
r= np.ones((60,60), dtype=float) * 2
z = np.sqrt(r**2 - xx**2 - yy**2, where=(r**2 - xx**2 - yy**2)>=0) # <==== HERE!!
surf = ax.plot_surface(xx, yy, z, cmap="viridis")
This generates a ridiculous image:
If I add the out
parameter as below, everything works fine.
z = np.zeros(xx.shape)
np.sqrt(r**2 - xx**2 - yy**2, where=(r**2 - xx**2 - yy**2)>=0, out=z)