[Tutor] How to vectorize a constant function?

Peter Otten __peter__ at web.de
Fri Feb 25 17:48:36 CET 2011


Jose Amoreira wrote:

> I am trying to define a constant vectorized function, that is, one that
> returns 1. (for instance) when called with a scalar argument x and that
> returns array([1.,1.,1.]) when the argument is a three-element array, etc.

With the help of google I found

http://docs.scipy.org/doc/numpy/reference/generated/numpy.frompyfunc.html

>>> import numpy
>>> def f(x): return 42
...
>>> f = numpy.frompyfunc(f, 1, 1)
>>> f(1)
42
>>> f(numpy.zeros(7))
array([42, 42, 42, 42, 42, 42, 42], dtype=object)




More information about the Tutor mailing list