numpy-ufunc

numpy ufuncs speed vs for loop speed

99封情书 提交于 2019-12-03 05:34:53
I've read a lot "avoid for loops with numpy". So, I tried. I was using this code (simplified version). Some auxiliary data: In[1]: import numpy as np resolution = 1000 # this parameter varies tim = np.linspace(-np.pi, np.pi, resolution) prec = np.arange(1, resolution + 1) prec = 2 * prec - 1 values = np.zeros_like(tim) My first implementation was with for loop: In[2]: for i, ti in enumerate(tim): values[i] = np.sum(np.sin(prec * ti)) Then, I got rid of the explicit for cycle, and achieved this: In[3]: values = np.sum(np.sin(tim[:, np.newaxis] * prec), axis=1) And this solution was faster for

What is the default of numpy functions, with where=False?

谁说胖子不能爱 提交于 2019-11-28 14:21:42
The ufunc documentation states: where New in version 1.7. Accepts a boolean array which is broadcast together with the operands. Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone. What is the default behavior, when out is not given? I observed some behavior, which doesn't really make sense to me: import numpy as np a,b = np.ones((2,2)) np.add(a,b,where = False) #returns 0 np.exp(a, where = False) #returns 1 np.sin(a, where = False) #returns 1 np.sign(a, where = False) #returns 0 np.reciprocal(a, where = False)