In NumPy:
A = np.array([[1,2,3],[4,5,6]])
array([[1, 3, 5],
[2, 4, 6]])
B = np.array([[1,2],[3,4],[5,6]])
array([[1, 2],
[3, 4],
[5, 6]])
A.dot(B)
array([[35, 44],
[44, 56]])
I only care about getting A.dot(B).diagonal() = array([35, 56])
Is there a way I can get array([35, 56]) without having to compute the inner products of all the rows and columns? I.e. the inner product of the ith row with ith column?
I ask because the performance difference becomes more significant for larger matrices.
This is just matrix multiplication for 2D arrays:
So since you just want the diagonal elements, looks like you’re after
So you could just use list comprehension:
OR, (and this only works because you want a
diagonalso this assumes that if A’s dimensions aren x m, B’s dimensions will bem x n):(no fancy numpy tricks going on here, just playing around with the maths).