[Numpy-discussion] vectorize problem

Travis Oliphant oliphant.travis at ieee.org
Wed Apr 26 10:05:01 EDT 2006


Arnd Baecker wrote:
> 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?
>   
It is just a simple change.   Scalars are supposed to be supported.  
They aren't only as a side-effect of the switch to not return 
object-scalars.    I did not update the vectorize code to handle the 
scalar return value from the object ufunc (which is now no-longer an 
object-scalar with the methods of arrays (like astype) but is instead 
the underlying object). 

I'll add a check.

-Travis





More information about the NumPy-Discussion mailing list