Python: fast matrix multiplication with extra indices

Viewed 246

I have two arrays, A and B, with dimensions (l,m,n) and (l,m,n,n), respectively. I would like to obtain an array C of dimensions (l,m,n) which is obtained by treating A and B as matrices in their fourth (A) and third and fourth indices (B). An easy way to do this is:


import numpy as np

#Define dimensions

l = 1024
m = l
n = 6

#Create some random arrays

A = np.random.rand(l,m,n)
B = np.random.rand(l,m,n,n)
C = np.zeros((l,m,n))

#Desired multiplication

for i in range(0,l):

    for j in range(0,m):

        C[i,j,:] = np.matmul(A[i,j,:],B[i,j,:,:])

It is, however, slow (about 3 seconds on my MacBook). What'd be the fastest, fully vectorial way to do this?

1 Answers
Related