Einsum¶

If we repeated letters we want those axes multiplied together

If we omit a letter from the output it means that values along that axis will be summed over For 1D arrays

Call signature NumPy equivalent Description
('i', A) A returns a view of A
('i->', A) sum(A) sums the values of A
('i,i->i', A, B) A * B element-wise multiplication of A and B
('i,i', A, B) inner(A, B) inner product of A and B
('i,j->ij', A, B) outer(A, B) outer product of A and B

For matrices

Call signature NumPy equivalent Description
('ij', A) A returns a view of A
('ji', A) A.T view transpose of A
('ii->i', A) diag(A) view main diagonal of A
('ii', A) trace(A) sums main diagonal of A
('ij->', A) sum(A) sums the values of A
('ij->j', A) sum(A, axis=0) sum down the columns of A (across rows)
('ij->i', A) sum(A, axis=1) sum horizontally along the rows of A
('ij,ij->ij', A, B) A * B element-wise multiplication of A and B
('ij,ji->ij', A, B) A * B.T element-wise multiplication of A and B.T
('ij,jk', A, B) dot(A, B) matrix multiplication of A and B
('ij,kj->ik', A, B) inner(A, B) inner product of A and B
('ij,kj->ikj', A, B) A[:, None] * B each row of A multiplied by B
('ij,kl->ijkl', A, B) A[:, :, None, None] * B each value of A multiplied by B

Ellipse syntax .... This allows to nod index dimensions:

np.einsum('...ij,ji->...', a, b )

This multiplies the last wo axes of a with an 2d array b.