I am given two 2D matrices where each of the cells is a vector with three elements.
What I'm looking to do is use 2D matrix multiplication where when any of the cells are multiplied together, it takes the dot product of the three-element vectors.
My linear algebra skills are lacking so apologies if there is an answer already, I've looked over many pages relating to tensordot and einsums but I don't understand how each of these might apply to my situation.
Here is basically what I am given:
import numpy as np
ar1 = np.array([[[1,2,3],[3,4,5]],[[5,6,7],[7,8,9]]])
ar2 = np.array([[[2,3,4],[4,5,6]],[[6,7,8],[8,9,10]]])
Here is how to make what I am looking for:
final = [[0 for x in range(2)] for y in range(2)]
final[0][0] = np.dot(ar1[0][0], ar2[0][0]) + np.dot(ar1[0][1], ar2[1][0])
final[0][1] = np.dot(ar1[0][0], ar2[0][1]) + np.dot(ar1[0][1], ar2[1][1])
final[1][0] = np.dot(ar1[1][0], ar2[0][0]) + np.dot(ar1[1][1], ar2[1][0])
final[1][1] = np.dot(ar1[1][0], ar2[0][1]) + np.dot(ar1[1][1], ar2[1][1])
final
Output: [[106, 142], [226, 310]]
In reality these matrices are going to be around 3000x40000x3 and 40000x40x3, so taking speed into account is greatly appreciated. Thanks!
tensordot? this is basically n-D matrices multiplication function. - smerllo