![](https://secure.gravatar.com/avatar/ec91d42d7e0f591d345ce91b4a3e259d.jpg?s=120&d=mm&r=g)
Hi, This is a really quick and stupid question. Let's say I have a function and I want to calculate its value for a set of points. What's the best way of quickly doing this avoiding loops? def MyFunction ( p1, p2, p3, t, y) return *some_value* t = numpy.array ( ..... ) y = numpy.array ( ....) a1,a2,a3 = numpy.mgrid [ 0:100, 0:1:100j, 0:100 ] evaluated_function = MyFunction (a1, a2, a3, t, y) evaluated_function.shape would be (100,100,100), and evaluated_function [m, n, k] should be equal to MyFunction ( a1[m], a2[n], a3[k], t, y ) I think mgrid (ogrid) are the right tools, but I don't really understand how I can get it to do what I want. I have read < http://docs.scipy.org/doc/scipy/reference/tutorial/basic.html#id5>, but that doesn't clear things up. I guess that what I want to do is just mentioned at the end of this Section of the tutorial, but can anyone maybe give an example? many thanks! J
![](https://secure.gravatar.com/avatar/36b04adcdce59af3ecbdec908fa4158f.jpg?s=120&d=mm&r=g)
Hi Jose, I can see two types of answers, depending on what's inside your function MyFunction. Either the function contains only simple operations like basic arithmetic operations, or more generally ufuncs (numpy's "universal functions"), then it's possible that you can take advantage of broadcasting (see http://www.scipy.org/Tentative_NumPy_Tutorial#head-081b5e0ff3963e102f4bb39b3... for more information about broadcasting). Here is a small example
import numpy as np Y, X = np.ogrid[0:5, 0:6] X array([[0, 1, 2, 3, 4, 5]]) Y array([[0], [1], [2], [3], [4]]) def add_plus_one(x, y, a=1): return x + y + a ... add_plus_one(X, Y) array([[ 1, 2, 3, 4, 5, 6], [ 2, 3, 4, 5, 6, 7], [ 3, 4, 5, 6, 7, 8], [ 4, 5, 6, 7, 8, 9], [ 5, 6, 7, 8, 9, 10]])
Note that I have used ogrid instead of mgrid here, because I knew the flat arrays would be broadcast to 5x6 arrays. Computations can be much faster when you use broadcasting. However, if you do more complex operations on p1, p2, p3 you can't always use braodcasting. Then you can create a new "vectorized" function which will be an instance of the numpy.vectorize class. Here'a another example (quite far-feched, I'm afraid...):
show() def sig_rand(n): a = np.random.rand(n) return a.std() ... vec_sig = np.vectorize(sig_rand) vec_sig(np.arange(10)) array([ 0. , 0. , 0.09590652, 0.24932485, 0.22653281, 0.19517703, 0.30371537, 0.24547412, 0.31915861, 0.27624239])
Nevertheless, I don't think vectorize speeds up the code compared to a for loop (unless it can do some broadcasting). It just allows to write more compact code. If you can't use broadcasting, you were right to use mgrid instead of ogrid, but check first if you can't do some broadcasting. Cheers, Emmanuelle On Fri, Jul 31, 2009 at 05:59:51PM +0100, Jose Gomez-Dans wrote:
Hi, This is a really quick and stupid question. Let's say I have a function and I want to calculate its value for a set of points. What's the best way of quickly doing this avoiding loops?
def MyFunction ( p1, p2, p3, t, y) � return *some_value*
t = numpy.array ( ..... ) y = numpy.array ( ....) a1,a2,a3 = numpy.mgrid [ 0:100, 0:1:100j, 0:100 ]
evaluated_function = MyFunction (a1, a2, a3, t, y)
evaluated_function.shape would be (100,100,100), and evaluated_function [m, n, k] should be equal to MyFunction ( a1[m], a2[n], a3[k], t, y )
I think mgrid (ogrid) are the right tools, but I don't really understand how I can get it to do what I want. I have read <[1]http://docs.scipy.org/doc/scipy/reference/tutorial/basic.html#id5>, but that doesn't clear things up. I guess that what I want to do is just mentioned at the end of this Section of the tutorial, but can anyone maybe give an example?
many thanks! J
References
Visible links 1. http://docs.scipy.org/doc/scipy/reference/tutorial/basic.html#id5
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
participants (2)
-
Emmanuelle Gouillart
-
Jose Gomez-Dans