Definition
Einsum Notation
Einstein summation notation is a concise way to represent summations over repeated indices when performing tensor operations. Instead of writing out explicit summation symbols , we reuse an index letter in multiple arrays to imply a summation over that index. This allows for compact expressions of operations like matrix multiplication, tensor contraction, outer product, etc.
Understanding
Let and be two matrices:
To compute , we need to replace the repeated indices . Since occurs on the left-hand side of the arrow and not on the right-hand side, we need to sum over for each pair.
The shape of the resulting tensor is equal to the dimensionalities of the indices and , thus and respectively:
Now, we need to compute each element by summing over the index:
Thus, the resulting matrix is:
Note that this is equivalent to matrix multiplication, which is a special case of tensor contraction.
Examples
The examples are taken from 1.
Transpose
The transpose of a matrix is denoted as:
or equivalently:
einsum('ij->ji', A)
Sum
The sum of all elements of a matrix is denoted as:
or equivalently:
einsum('ij->', A)
Column Sum
The sum of all elements of the -th column of a matrix is denoted as:
or equivalently:
einsum('ij->j', A)
Row Sum
The sum of all elements of the -th row of a matrix is denoted as:
or equivalently:
einsum('ij->i', A)
Dot Product
The dot product of two vectors is denoted as:
einsum('i,i->', x, y)