I am looking for a quick way to calculate the sum of n external products.
Essentially, I start with two matrices generated from normal distributions - there are n vectors with v elements:
A = np.random.normal(size = (n, v)) B = np.random.normal(size = (n, v))
I would like to calculate the external products of each vector of size v in A and B and sum them together.
Note that A * BT does not work - A is of size nxv, while B is of size vx n.
The best I can do is create a loop in which external products are created and then summed up later. I have it like this:
outers = np.array([A[i] * B[i].T])
This creates an nxvxv array (the loop is within the comprehension of the list, which is subsequently converted to an array), which can then be summarized using np.sum(outers, axis = 0) . However, this is pretty slow, and I was wondering if there is a vector function that I could use to speed it up.
If anyone has any advice, I would really appreciate it!
source share