Name of matrix operation of [ A[0] dot B[0], A[1] dot B[1] ] from 2x2 matrices A, B

34 Views Asked by At

Please advise how this matrix operation is called, and what is the numpy operation for it. np.inner, np.dot does not create dot products from 2x2 matrices.

enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

This is the diagonal of the matrix product $AB$, which can be obtained in NumPy using numpy.diagonal and numpy.matmul.

For example

import numpy as np
A = np.matrix('0 1 2; 3 4 5')
B = np.matrix('0 -3; -1 -4; -2 -5');
np.diagonal(np.matmul(A,B)) # or (A*B).diagonal()

yields

array([ -5, -50])

However, this does compute the off-diagonal entries of the matrix product as well and hence isn't very efficient. Describing the entries of your resulting vector as $$ v_i = \sum_j a_{ij} b_{ji} $$ you can obtain the same result more efficiently using numpy.einsum as

np.einsum('ij,ji->i', A, B)