vectorize pitfall

Travis Oliphant oliphant.travis at ieee.org
Wed Oct 25 06:39:41 CDT 2006


A. M. Archibald wrote:
> Hi,
>
> Vectorize is a very handy function, but it has at least one pitfall:
>
> def f(x):
>     if 1.3<x<2.5:
>         return sqrt(x)
>     else:
>         return 0
>
> Now vectorize(f)(2)=1.41421356237 but vectorize(f)(array([1,2]))=array([0,1]).
>
> The problem is that, when given an array as input, vectorize feeds in
> the first element, looks at the return type, and returns an array of
> that type - and I didn't put a "." after the zero.
>
> This should perhaps be in the docstring of vectorize, since I can't
> see any way to work around it, but it can easily lead to
> difficult-to-find bugs. It may seem like an artificial example, but it
> came up with a function I was working on. But it's confusing
> behaviour.
>
> vectorize appears to support an "otypes" argument, but it doesn't take
> standard numpy type objects, and it doesn't do anything obvious.
>   
It takes old "type-codes" as one big string so you say

vectorize(f,otypes='d')

This should be modernized to handle a list of dtype objects.

I've fixed vectorize in SVN.


-Travis


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the Numpy-discussion mailing list