I know that python allows a fast evalutation of a real-valued, one variable function f(x) on a numpy array xarr = np.array([x0,x1,...xN]):
f(xarr) = np.array([f(x0), f(x1), ..., f(xN)])
However, this doesn't seem to work syntax-wise for a multivariable function. Say I have a real-valued function f(x,y), where x and y are two real numbers. Is there a correct syntax to evaluate the function on, say, [(0,0), (0,1), (1,0), (1,1)], avoiding a loop (which is always slow on python...)?
Edit: below are the functions involved:
The 5-variable function I refer to is:
def chisqr(BigOmega, inc, taustar, Q0, U0):
QU = QandU(nusdata, BigOmega, inc, taustar, Q0, U0)
Q = QU[:,0]
U = QU[:,1]
return 1./(2.*N) * (np.sum(((Q - Qs)/errQs)**2.) + np.sum(((U - Us)/errUs)**2.))
where nusdata, Qs, and Us are arrays defined before calling the function. The function calls the following function:
def QandU(nu, BigOmega, inc, taustar, Q0, U0):
lambdalong = nu+omega-np.pi/2.
tau3 = taustar * ((1+ecc*np.cos(nu))/(1-ecc**2.))**gamma
delQ = -tau3 * (1+np.cos(inc)*np.cos(inc))*(np.cos(2.*lambdalong) np.sin(inc)*np.sin(inc))
delU = -2*tau3*np.cos(inc)*np.sin(2*lambdalong)
Q = Q0 + delQ*np.cos(BigOmega) - delU * np.sin(BigOmega)
U = U0 + delQ*np.sin(BigOmega) + delU * np.cos(BigOmega)
bounds = (inc < 0) or (inc > np.pi/2.) or (BigOmega < -2*np.pi) or (BigOmega > 2*np.pi) or (taustar < 0.) or (taustar > 1.)
if bounds:
Q = 10E10
U = 10E10
#return U
return np.column_stack((Q,U))
All variables which aren't the function's arguments are defined outside the function.
f = lambda x: x[0]*x[1]can work.