Skip to content
Related Articles

Related Articles

Tensor contraction with Einstein summation convention using NumPy in Python

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Jun, 2022
View Discussion
Improve Article
Save Article

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]]
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!