Reputation: 1533
I tried in ipython following code: I want to use vectorize and give the function prox(x,tau). But the first value in lambda comes always two times.
In [32]: a = np.array([[ 1., 2.],[ 3., 4.]])
In [33]: def prox(x, tau):
...: print x, tau
...: if x >= tau:
...: print "first"
...: return x-tau
...: if -tau <= x and x <= tau:
...: print "second"
...: return 0.0
...: if x <= -tau:
...: print "third"
...: return x+tau
In [34]: b = np.vectorize(lambda x: prox(x, 2))(a[:,1:])
In [35]: b
2.0 2
first
2.0 2
first
4.0 2
first
Why in line 35 is two times printed the same value? 2.0 2
Upvotes: 1
Views: 142
Reputation: 231615
If you don't specify otypes
, then vectorize
performs a test calculation with the first value, and uses that to determine the dtype
of the array it returns. Hence the double evaluation of the first item.
Usually that extra calculation is unimportant. But be careful. If that initial calculation returns an integer (e.g. scalar 0) the returned array will also be integer, loosing any float values in subsequent calculations.
For more details review the docs for vectorize
.
vectorize is indeterminate - An error produced by an unintended integer otype. I've seen this error in other SO questions.
Upvotes: 2