Tensor contraction with Einstein summation convention using NumPy in Python
In this article let’s find the Tensor contraction with the Einstein summation convention in Python.
Einstein summation is a notational convention for simplifying formulas, such as vector, matrix, and general tensor summations. The numpy.einsum() method from the NumPy library is used to find the Tensor contraction with the Einstein summation convention in Python. Many common multi-dimensional, linear algebraic array operations can be described in a simple way using the Einstein summation method which computes these values in implicit mode. By suppressing or forcing summation over a defined subscript label in explicit mode, it allows us additional freedom to compute alternative array operations that may not be considered standard Einstein summation operations.
Syntax: numpy.einsum(subscripts, *operands, out=None)
- subscripts: str type. As a comma separated list of subscript labels, specifies the subscripts for summation. Unless the explicit sign ‘->’ and subscript labels of the precise output form are given, an implicit (classical Einstein summation) computation is done.
- operands: list of array_like. The arrays for the operation are as follows.
- out: ndarray, optional parameter. The calculation is done into this array if it is provided.
Returns: The calculation based on the Einstein summation convention is the output.
Here, we will create two NumPy arrays using np.arange() method. After that numpy.einsum() method is used to find the Tensor contraction with the Einstein summation convention in Python. The string ‘ijk, jil ->kl’ will be passed in the subscripts parameter of the np.einsum() method representing the Tensor contraction, it is carried out on the two arrays, and the result is returned.
Array of letters is : [[[ 0 1 2] [ 3 4 5]] [[ 6 7 8] [ 9 10 11]]] Array of numbers is : [[[ 0 1 2] [ 3 4 5]] [[ 6 7 8] [ 9 10 11]]] Array one dimension : 3 Array two dimension 3 Shape of array 1 is : (2, 2, 3) Shape of array 2 is : (2, 2, 3) Tensor contraction : [[117 135 153] [135 157 179] [153 179 205]]