Saturday, October 25, 2025
HomeLanguagesEvaluate Einstein’s summation convention of two multidimensional NumPy arrays

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 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]]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS