[SciPy-User] vectorizing a function with non scalar arguments

Luke hazelnusse at gmail.com
Mon Aug 10 17:28:05 EDT 2009


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



More information about the SciPy-User mailing list