[SciPy-user] vectorize question

Travis Oliphant oliphant at ee.byu.edu
Fri Aug 6 01:00:08 CDT 2004


bulatov at cs.orst.edu wrote:

>What's the best way to evaluate a function at every point of a space (ie, for
>plotting purposes)?
>
>For instance, here's how I'd do it in regular python
>
>values=zeros((5,5), Float)
>for i in range(5):
>  for j in range(5):
>    values[i,j]=some_custom_function(i,j)
>plot(values)
>
>Is there some SciPy way of doing it more concisely? I looked at vectorize, but I
>can only use it for similar task in 1 dimension.
>
>  
>
Use ogrid (open grid)

and

If your function is made up of ufuncs it should vectorize automatically, 
otherwise you use vectorize.

Example:

i,j = ogrid[0:5,0:5]  (or mgrid if you really want to fill out the values)

If some_custom_function is made up only of ufuncs

values = some_custom_function(i,j)

otherwise

new_custom_function = vectorize(some_custom_function)
values = new_custom_funcion(i,j)


-Travis O.



More information about the SciPy-user mailing list