1
votes

I am looking for an efficient way to perform a matrix multiplication (dot product) of two time-dependent 2D matrices to end up with one time-dependent 2D matrix.

For example:

a = np.zeros([7200,13,4])
b = np.zeros([7200,4,7])

And I want to end up with

c = np.zeros([7200,13,7])

I already found np.tensordot, however this yields me a 4D matrix instead of a 3D matrix. Also other numpy functions did not yield me the required shape. So I wonder if there is any way to perform this matrix multiplication without the use of for-loops?

Best regards,

Timothy Van Daele

1
I just digged a little bit deeper and I believe I found an efficient solution using the einsum function in numpy. a = np.zeros([7200,13,4])+1 b = np.zeros([7200,4,7])+1 c = np.einsum('ijk,ikl->ijl',a,b) c.shape (7200, 13, 7) Best regards, Timothy Van Daele - TimothyVD
so you can delete the question? - usethedeathstar
You should write your comment as an answer and (when the time limit is up) mark it as accepted to help future visitors to this page. - YXD
How does 'time-dependent' (from the title) play a role in this question? - Midnighter
@Midnighter. A time-denpendent matrix M(t) can be represented as N copies of M, one for each time point of interest, which adds another dimension to the matrix. Thus, the two 2D matrices become two 3D matrices as shown in definition of a and b. - Jim Parker

1 Answers

1
votes

I just digged a little bit deeper and I found the numpy function einsum. This gives a lot of freedom for doing vector multiplications.

a = np.zeros([7200,13,4])
b = np.zeros([7200,4,7])

c = np.einsum('ijk,ikl->ijl',a,b) 
c.shape (7200, 13, 7)