## Numpy: Efficient matrix calculation A*xj where xj is row j in X

Question

So the background to this "problem" is that I'm trying to optimize a large python project. I started timing the program and noticed that almost 50% of the run time is spent on a calculation similar to this one:

```
import numpy as np
# Example
A = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
X = np.random.multivariate_normal([0,0,0,0],np.eye(4),15000)
# Create a lambda function to use row based
F = lambda x: np.dot(A,x)
# Now calculating the value
answer = np.apply_along_axis(F, 1, X)
print answer.shape
```

I've tried to find a way to make this faster, but keep running into a wall. Is this really the optimal of doing this?

Show source

## Answers to Numpy: Efficient matrix calculation A*xj where xj is row j in X ( 1 )

We could use

`np.dot`

on`X`

and`A`

to lose their second axes each. To input into`np.dot`

, we will use`X`

as the first input and transpose`A`

to bring its second axis to the front to be used as the second input.Thus, we would have the output like so -

Runtime test for sample inputs listed in question -