[SciPy-user] Vectorize vs Map

Fernando Perez fperez.net@gmail....
Thu Mar 15 12:15:34 CDT 2007

On 3/15/07, Robert Kern <robert.kern@gmail.com> wrote:
> Lorenzo Isella wrote:
> > Dear All,
> > Probably another newbie question: I like quite a lot the vectorize()
> > command which allows me to skip iterations on functions, but the map()
> > command on a list performs a similar task if I am not mistaken.
> > Is there any reason to favour one above the other or is it just a
> > matter of taste?
> vectorize() takes a Python function and turns it into a ufunc. ufuncs do a lot
> more than map() does. They can take multidimensional arrays. n-ary ufuncs can
> take multiple inputs and broadcast them against each other. ufuncs have methods
> like .inner() and .reduce() which are quite powerful.

Mmh, isn't that what 'frompyfunc' does instead?  vectorize doesn't
seem to produce a true ufunc.  Perhaps I'm just misunderstanding

In [10]: def foo(x):return x

In [11]: vfoo = N.vectorize(foo)

In [12]: vfoo.outer?
Object `vfoo.outer` not found.

In [13]: type vfoo
-------> type(vfoo)
Out[13]: <class 'numpy.lib.function_base.vectorize'>

In [14]: ufoo = N.frompyfunc(foo,1,1)

In [15]: type ufoo
-------> type(ufoo)
Out[15]: <type 'numpy.ufunc'>

In [16]: ufoo.outer?
Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
Namespace:      Interactive
    <no docstring>

I'm actually not exactly sure why the two do exist, to be honest.  I
realize frompyfunc has (probably by necessity) a more complicated API,
but it does return a true ufunc, which vectorize() doesn't.

Insight welcome.



More information about the SciPy-user mailing list