When we call vectorized function for some vector, for some reason it is called twice for the first vector element. What is the reason, and can we get rid of this strange effect (e.g. when this function needs to have some side effect, e.g. counts some sum etc)
Example:
import numpy
@numpy.vectorize
def test(x):
print(x)
test([1,2,3])
Result:
1
1
2
3
numpy 1.26.4
This is well defined in the
vectorizedocumentation:If you don't want this, you can define
otypes:In any case, be aware that
vectorizeis just a convenience around a python loop. If you need to have side effects you might rather want to use an explicit python loop.