Related Articles

# Evaluate Einstein’s summation convention of two multidimensional NumPy arrays

• Last Updated : 29 Aug, 2020

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 subscript labels. An implicit (classical Einstein summation) calculation is performed unless the explicit indicator ‘->’ is included as well as subscript labels of the precise output form.

operands : list of array_like

These are the arrays for the operation.

out : ndarray, optional

If provided, the calculation is done into this array.

Returns: The calculation based on the Einstein summation convention.

Example 1: Einstein’s summation convention of two 2X2 matrices

## Python3

 `# Importing library``import` `numpy as np`` ` `# Creating two 2X2 matrix``matrix1 ``=` `np.array([[``1``, ``2``], [``0``, ``2``]])``matrix2 ``=` `np.array([[``0``, ``1``], [``3``, ``4``]])`` ` `print``(``"Original matrix:"``)``print``(matrix1)``print``(matrix2)`` ` `# Output``result ``=` `np.einsum(``"mk,kn"``, matrix1, matrix2)`` ` `print``(``"Einstein’s summation convention of the two matrix:"``)``print``(result)`

Output:

```Original matrix:
[[1 2]
[0 2]]
[[0 1]
[3 4]]
Einstein’s summation convention of the two matrix:
[[6 9]
[6 8]]
```

Example 2: Einstein’s summation convention of two 3X3 matrices

## Python3

 `# Importing library``import` `numpy as np`` ` `# Creating two 3X3 matrix``matrix1 ``=` `np.array([[``2``, ``3``, ``5``], [``4``, ``0``, ``2``], [``0``, ``6``, ``8``]])``matrix2 ``=` `np.array([[``0``, ``1``, ``5``], [``3``, ``4``, ``4``], [``8``, ``3``, ``0``]])`` ` `print``(``"Original matrix:"``)``print``(matrix1)``print``(matrix2)`` ` `# Output``result ``=` `np.einsum(``"mk,kn"``, matrix1, matrix2)`` ` `print``(``"Einstein’s summation convention of the two matrix:"``)``print``(result)`

Output:

```Original matrix:
[[2 3 5]
[4 0 2]
[0 6 8]]
[[0 1 5]
[3 4 4]
[8 3 0]]
Einstein’s summation convention of the two matrix:
[[49 29 22]
[16 10 20]
[82 48 24]]
```

Example 3: Einstein’s summation convention of two 4X4 matrices

## Python3

 `# Importing library``import` `numpy as np`` ` `# Creating two 4X4 matrix``matrix1 ``=` `np.array([[``1``, ``2``, ``3``, ``5``], [``4``, ``4``, ``0``, ``2``], ``                    ``[``0``, ``1``, ``6``, ``8``], [``0``, ``5``, ``6``, ``9``]])`` ` `matrix2 ``=` `np.array([[``0``, ``1``, ``9``, ``2``], [``3``, ``3``, ``4``, ``4``], ``                    ``[``1``, ``8``, ``3``, ``0``], [``5``, ``2``, ``1``, ``6``]])`` ` `print``(``"Original matrix:"``)``print``(matrix1)``print``(matrix2)`` ` `# Output``result ``=` `np.einsum(``"mk,kn"``, matrix1, matrix2)`` ` `print``(``"Einstein’s summation convention of the two matrix:"``)``print``(result)`

Output:

```Original matrix:
[[1 2 3 5]
[4 4 0 2]
[0 1 6 8]
[0 5 6 9]]
[[0 1 9 2]
[3 3 4 4]
[1 8 3 0]
[5 2 1 6]]
Einstein’s summation convention of the two matrix:
[[34 41 31 40]
[22 20 54 36]
[49 67 30 52]
[66 81 47 74]]
```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up