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
To compute
The shape of the resulting tensor is equal to the dimensionalities of the indices
Now, we need to compute each
Thus, the resulting matrix
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
or equivalently:
einsum('ij->ji', A)Sum
The sum of all elements of a matrix
or equivalently:
einsum('ij->', A)Column Sum
The sum of all elements of the
or equivalently:
einsum('ij->j', A)Row Sum
The sum of all elements of the
or equivalently:
einsum('ij->i', A)Dot Product
The dot product of two vectors
einsum('i,i->', x, y)