I have these variables with the following sizes:
A - (3,) B - (4,) X_r - (3,K,N,nS) X_u - (4,K,N,nS) k - (K,)
and I want to calculate (A.dot(X_r[:,:,n,s])*B.dot(X_u[:,:,n,s])).dot(k) for all possible n and s , so as I do it now, this is the following:
np.array([[(A.dot(X_r[:,:,n,s])*B.dot(X_u[:,:,n,s])).dot(k) for n in xrange(N)] for s in xrange(nS)]) #nSxN
But this is very slow, and I was wondering if there is a better way to do this, but I'm not sure.
However, there is another calculation that I am doing, and I am sure that it can be optimized:
np.sum(np.array([(X_r[:,:,n,s]*B.dot(X_u[:,:,n,s])).dot(k) for n in xrange(N)]),axis=0)
In this, I create a numpy array to sum it on one axis and discard the array after. If it were a list in 1-D, I would use reduce and optimize it, what should I use for numpy arrays?