I have a specific issue with multiplying matrices in numpy. Here is an example:
P=np.arange(30).reshape((-1,3))
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23],
[24, 25, 26],
[27, 28, 29]])
I want to multiply each row by its transpose in order to obtain a 3x3 matrix for each row, for example for the first row:
P[0]*P[0][:,np.newaxis]
array([[0, 0, 0],
[0, 1, 2],
[0, 2, 4]])
and store the result in a 3-d matrix M:
M=np.zeros((10,3,3))
for i in range(10):
M[i] = P[i]*P[i][:,np.newaxis]
I think there might be a way to do this without looping, maybe with tensor-dot, but cannot find it.
Does someone have an idea?