
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Tensor contraction with Einstein summation convention in Python
For Tensor contraction with Einstein summation convention, use the numpy.einsum() method in Python. The 1st parameter is the subscript. It specifies the subscripts for summation as comma separated list of subscript labels. The 2nd parameter is the operands. These are the arrays for the operation.
The einsum() method evaluates the Einstein summation convention on the operands. Using the Einstein summation convention, many common multi-dimensional, linear algebraic array operations can be represented in a simple fashion. In implicit mode einsum computes these values.
In explicit mode, einsum provides further flexibility to compute other array operations that might not be considered classical Einstein summation operations, by disabling, or forcing summation over specified subscript labels.
Steps
At first, import the required libraries −
import numpy as np
Creating two numpy One-Dimensional array using the array() method −
arr1 = np.arange(60.).reshape(3,4,5) arr2 = np.arange(24.).reshape(4,3,2)
Display the arrays −
print("Array1...\n",arr1) print("\nArray2...\n",arr2)
Check the Dimensions of both the arrays −
print("\nDimensions of Array1...\n",arr1.ndim) print("\nDimensions of Array2...\n",arr2.ndim)
Check the Shape of both the arrays −
print("\nShape of Array1...\n",arr1.shape) print("\nShape of Array2...\n",arr2.shape)
For Tensor contraction with Einstein summation convention, use the numpy.einsum() method in Python −
print("\nResult (Tensor contraction)...\n",np.einsum('ijk,jil->kl', arr1, arr2))
Example
import numpy as np # Creating two numpy One-Dimensional array using the array() method arr1 = np.arange(60.).reshape(3,4,5) arr2 = np.arange(24.).reshape(4,3,2) # Display the arrays print("Array1...\n",arr1) print("\nArray2...\n",arr2) # Check the Dimensions of both the arrays print("\nDimensions of Array1...\n",arr1.ndim) print("\nDimensions of Array2...\n",arr2.ndim) # Check the Shape of both the arrays print("\nShape of Array1...\n",arr1.shape) print("\nShape of Array2...\n",arr2.shape) # For Tensor contraction with Einstein summation convention, use the numpy.einsum() method in Python. print("\nResult (Tensor contraction)...\n",np.einsum('ijk,jil->kl', arr1, arr2))
Output
Array1... [[[ 0. 1. 2. 3. 4.] [ 5. 6. 7. 8. 9.] [10. 11. 12. 13. 14.] [15. 16. 17. 18. 19.]] [[20. 21. 22. 23. 24.] [25. 26. 27. 28. 29.] [30. 31. 32. 33. 34.] [35. 36. 37. 38. 39.]] [[40. 41. 42. 43. 44.] [45. 46. 47. 48. 49.] [50. 51. 52. 53. 54.] [55. 56. 57. 58. 59.]]] Array2... [[[ 0. 1.] [ 2. 3.] [ 4. 5.]] [[ 6. 7.] [ 8. 9.] [10. 11.]] [[12. 13.] [14. 15.] [16. 17.]] [[18. 19.] [20. 21.] [22. 23.]]] Dimensions of Array1... 3 Dimensions of Array2... 3 Shape of Array1... (3, 4, 5) Shape of Array2... (4, 3, 2) Result (Tensor contraction)... [[4400. 4730.] [4532. 4874.] [4664. 5018.] [4796. 5162.] [4928. 5306.]]
- Related Articles
- Scalar multiplication with Einstein summation convention in Python
- Vector inner product with Einstein summation convention in Python
- Matrix Vector multiplication with Einstein summation convention in Python
- Vector outer product with Einstein summation convention in Python
- Array axis summations with Einstein summation convention in Python
- Compute a matrix transpose with Einstein summation convention in Python
- Get the trace of a matrix with Einstein summation convention in Python
- Extract the diagonal of a matrix with Einstein summation convention in Python
- Compute the tensor dot product for arrays with different dimensions with double contraction in Python
- Alternate element summation in list (Python)
- Python Program to get K length groups with given summation
- Summation of tuples in list in Python
- Python - Column summation of tuples
- Python – Dual Tuple Alternate summation
- Mutual Inductance with Dot Convention
