I have two matrices,
A = np.array([[1, 3, 5],
[2, 4, 6]])
b = np.array([[7, 9, 11],
[8, 10, 12]])
The operation I want to perform is multiplying
# Column 1
A_1 = np.matmul([[1],[2]], [[7, 8]])
# [1]
# [2] * [7, 8] = [7, 8]
# [14, 16]
# Column 2
A_2 = np.matmul([[3],[4]], [[9, 10]])
# [3]
# [4] * [9, 10] = [27, 30]
# [36, 40]
# A_1+A_2 = [34 38
# 50 56]
....
# Column N
For each column then sum up the resulting matrices. My matrices will always have a shape of (2, N). In words, multiply i'th column of matrix A with the transpose of the i'th column of matrix B then sum up each 2x2 matrix from each column pair.
I know I can probably loop over each entry and calculate it this way but I don't think thats the spirit of numpy and think there has to be a better way.
IIUC you can do this with
np.einsum:Prints:
EDIT: As @NickODell stated, if you want to sum the 2x2 matrices to one number on each column you can use:
Prints: