[Numpy-discussion] vectorize problem
Arnd Baecker
arnd.baecker at web.de
Wed Apr 26 00:52:01 EDT 2006
Hi,
On Tue, 25 Apr 2006, Steffen Loeck wrote:
> Hello all,
>
> I have a problem using scalar variables in a vectorized function:
>
> from numpy import vectorize
>
> def f(x):
> if x>0: return 1
> else: return 0
>
> F = vectorize(f)
>
> F(1)
>
> gives the error message:
> ---------------------------------------------------------------------------
> exceptions.AttributeError Traceback (most recent call last)
>
> .../function_base.py in __call__(self, *args)
> 619
> 620 if self.nout == 1:
> --> 621 return self.ufunc(*args).astype(self.otypes[0])
> 622 else:
> 623 return tuple([x.astype(c) for x, c in
> zip(self.ufunc(*args), self.otypes)])
>
> AttributeError: 'int' object has no attribute 'astype'
Ouch - that's not nice - a lot of my code relies the fact that (old
scipy) vectorize happily eats scalars *and* arrays.
I am not familiar with the code of numpy.vectorize (which has indeed
changed quite a bit compared to the old scipy.vectorize),
but maybe it is only a simple change?
> Is there any way to get vectorized functions working with scalars again?
+1
(or is there a particular reason why "vectorized" functions
should not be able to operate on scalars?)
Best, Arnd
More information about the NumPy-Discussion
mailing list