Given an F matrix which is 3x3 and some point matrices which are both N x 3 I would like to compute E = b.T @ F @ a efficiently.
F = np.arange(9).reshape(3, 3)
>>> [[0 1 2]
[3 4 5]
[6 7 8]]
a = np.array([[1, 2, 1], [3, 4, 1], [5, 6, 1], [7, 8, 1]])
>>>[[1 2 1]
[3 4 1]
[5 6 1]
[7 8 1]]
b = np.array([[10, 20, 1],[30, 40, 1],[50, 60, 1],[70, 80, 1]])
>>>[[10 20 1]
[30 40 1]
[50 60 1]
[70 80 1]]
The expected output is:
E = [388 1434 3120 5446]
I can get it with a simple for loop but I would like to do this with all numpy. I tried reshaping the a and b matrices but that didn't quite work.
N = b.shape[0] #4
a_reshaped = a.reshape(N, 3, 1)
b_reshaped = b.reshape(1, N, 3)
F_repeated = np.repeat(F[None,:], N, axis=0)
E = b_reshaped @ F_repeated @ a_reshaped
>>>
[[[ 388]
[ 788]
[1188]
[1588]]
[[ 714]
[1434]
[2154]
[2874]]
[[1040]
[2080]
[3120]
[4160]]
[[1366]
[2726]
[4086]
[5446]]]
If I then take the diagonal values I get the expected result however, this is very inefficient.
Any suggestions?
Edit: Here is the for loop version of what I am describing:
E = []
for k in range(N):
error = b[k].T @ F @ a[k]
E.append(error)
E = np.array(E)
>>>[388 1434 3120 5446]