vectorizing a function with non scalar arguments
I have a function of the following form: def f(q, qd, parameter_list): # Unpacking the parameters m, g, I11, I22, I33 = parameter_list # Unpacking the qdots q1, q2, q3, q4, q5, q6 = q q1p, q2p, q3p, q4p, q5p, q6p = qd ....Some calculations to determine u1,u2,u3,u4,u5,u6.... return [u1, u2, u3, u4, u5, u6] As is visible above, q and qdot need to be length 6 tuples/lists, while parameter_list needs to be length 5. I would like to vectorize this function so that I could pass it: 1) q and qd as a 2-d numpy arrays of shape (n, 6) 2) parameter_list as a 1-d numpy array of shape (5,) and it would return a 2-d numpy array of shape (n, 6) parameter_list is a bunch of constants that don't change, while q and qd are the things that are different between say q[n, :] and q[n+1, :]. I realize that I can use a for loop to loop from 0 to n-1. My question is whether there a way to use vectorize (or some other decorator), to obtain the behavior? Or is there another nice method that people might be able to recommend? The documentation for vectorize is very sparse and seems like it is geared only towards function which have scalar arguments. Thanks, ~Luke
On 10-Aug-09, at 5:28 PM, Luke wrote:
I have a function of the following form: def f(q, qd, parameter_list): # Unpacking the parameters m, g, I11, I22, I33 = parameter_list # Unpacking the qdots q1, q2, q3, q4, q5, q6 = q q1p, q2p, q3p, q4p, q5p, q6p = qd ....Some calculations to determine u1,u2,u3,u4,u5,u6.... return [u1, u2, u3, u4, u5, u6]
We'd really need to see "...Some calculations..." but if it's something that's expressible as simple arithmetic/calls to numpy ufuncs, then it's probable that you can pull this off.
I would like to vectorize this function so that I could pass it: 1) q and qd as a 2-d numpy arrays of shape (n, 6) 2) parameter_list as a 1-d numpy array of shape (5,)
hsplit() and hstack() to unpack/pack the 2D arrays, respectively (though there are more memory efficient/cache friendly ways, if you care about speed).
and it would return a 2-d numpy array of shape (n, 6)
parameter_list is a bunch of constants that don't change, while q and qd are the things that are different between say q[n, :] and q[n+1, :].
I realize that I can use a for loop to loop from 0 to n-1.
My question is whether there a way to use vectorize (or some other decorator), to obtain the behavior? Or is there another nice method that people might be able to recommend?
Again, if you split into 1d arrays and are just doing arithmetic on them (or you use numpy functions that can take scalar or vector arguments) then it should "vectorize itself". But it might be helpful if you have any helper functions of one scalar variable that you need to evaluate across an entire array of q1's, etc.
The documentation for vectorize is very sparse and seems like it is geared only towards function which have scalar arguments.
Yes, 'vectorize' won't do it for you in this case. David
participants (2)
-
David Warde-Farley
-
Luke