Tensor contraction with Einstein summation convention using NumPy in Python
Last Updated :
28 Jun, 2022
In this article let's find the Tensor contraction with the Einstein summation convention in Python.
numpy.einsum() method
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)
Parameters:
- 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.
Example:
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.
Python3
# import packages
import numpy as np
# np.arange() method is used to create
# two arrays
arr1 = np.arange(12).reshape(2,2,3)
arr2 = np.arange(12).reshape(2,2,3)
# Display the arrays
print("Array of letters is :",arr1)
print("Array of numbers is :",arr2)
# Checking the dimensions
print("Array one dimension :",arr1.ndim)
print("Array two dimension",arr2.ndim)
# Checking the shape of the arrays
print("Shape of array 1 is : ",arr1.shape)
print("Shape of array 2 is : ",arr2.shape)
# Tensor contraction with Einstein summation convention
print("Tensor contraction : ",np.einsum('ijk,jil->kl', arr1, arr2))
Output:
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]]
Similar Reads
Vector outer product with Einstein summation convention using NumPy in Python In this article, we will find vector outer product with Einstein summation convention in Python. numpy.einsum() method The numpy.einsum() method from the NumPy library is used to find the vector outer product with the Einstein summation convention in Python. Many common multi-dimensional, linear al
3 min read
Evaluate Einstein's summation convention of two multidimensional NumPy arrays In Python, we can use the einsum() function of the NumPy package to compute Einstein's summation convention of two given multidimensional arrays. Syntax: numpy.einsum(subscripts, *operands, out=None)Â Parameters:Â subscripts : str Specifies the subscripts for summation as comma separated list of sub
3 min read
Evaluate Hermite series at points x when coefficients are multi-dimensional using NumPy in Python In this article, we will cover how to evaluate a Hermite series at points x with a multidimensional coefficient array in Python using NumPy. Example Input: [[11 12][13 14]] Output: [[37. 63.][40. 68.]] Explanation: Hermite series at points x.NumPy.polynomial.hermite.hermval method To evaluate a Herm
3 min read
Evaluate a Hermite_e series at points x in using NumPy Python In this article, we will cover how to evaluate a Hermite_e series at points x using NumPy in Python. numpy.polynomial.hermite.hermval The numpy.polynomial.hermite.hermval() method from the NumPy library is used to evaluate a Hermite series at points x. If the parameter x is a tuple or a list, it is
2 min read
Converting a List of Tensors to a Single Tensor in PyTorch PyTorch, a popular deep learning framework, provides powerful tools for tensor manipulation. One common task in PyTorch is converting a list of tensors into a single tensor. This operation is crucial for various applications, including data preprocessing, model input preparation, and tensor operatio
4 min read
Evaluate a Hermite_e series at list of points x using NumPy in Python In this article, we will cover how to evaluate a Hermite_e series at the list of points x using NumPy in Python. numpy.polynomial.hermite.hermval To evaluate a Hermite series at points x with a multidimensional coefficient array, NumPy provides a function called hermite.hermval(). It takes two param
3 min read