w3resource

NumPy: Evaluate Einstein’s summation convention of two given multidimensional arrays


Write a NumPy program to evaluate Einstein’s summation convention of two given multidimensional arrays.

Note: In mathematics, especially in applications of linear algebra to physics, the Einstein notation or Einstein summation convention is a notational convention that implies summation over a set of indexed terms in a formula, thus achieving notational brevity.

Sample Solution :

Python Code :

import numpy as np

# Create two 1-D arrays 'a' and 'b'
a = np.array([1,2,3])
b = np.array([0,1,0])

# Display the original 1-D arrays 'a' and 'b'
print("Original 1-d arrays:")
print(a)
print(b)

# Compute the Einstein’s summation convention of the 1-D arrays 'a' and 'b' using np.einsum()
result =  np.einsum("n,n", a, b)
print("Einstein’s summation convention of the said arrays:")
print(result)

# Create two 3x3 arrays 'x' and 'y'
x = np.arange(9).reshape(3, 3)
y = np.arange(3, 12).reshape(3, 3)

# Display the original higher-dimensional arrays 'x' and 'y'
print("Original Higher dimension:")
print(x)
print(y)

# Compute the Einstein’s summation convention of the 2-D arrays 'x' and 'y' using np.einsum()
result = np.einsum("mk,kn", x, y)
print("Einstein’s summation convention of the said arrays:")
print(result) 

Sample Output:

Original 1-d arrays:
[1 2 3]
[0 1 0]
Einstein’s summation convention of the said arrays:
2
Original Higher dimension:
[[0 1 2]
 [3 4 5]
 [6 7 8]]
[[ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
Einstein’s summation convention of the said arrays:
[[ 24  27  30]
 [ 78  90 102]
 [132 153 174]]

Explanation:

In the above code –

a = np.array([1,2,3]): This line creates a NumPy array a containing the elements [1, 2, 3].

b = np.array([0,1,0]): This line creates a NumPy array b containing the elements [0, 1, 0].

result = np.einsum("n,n", a, b): This line computes the element-wise product of arrays a and b and sums the result. The einsum notation "n,n" indicates that the same index n is used for both arrays, meaning the product is taken element-wise, and the resulting array is summed. In this case, the result is (1 * 0) + (2 * 1) + (3 * 0) = 0 + 2 + 0 = 2

x = np.arange(9).reshape(3, 3): This line creates a 3x3 NumPy array x containing elements from 0 to 8.

[[0, 1, 2],

[3, 4, 5],

[6, 7, 8]]

y = np.arange(3, 12).reshape(3, 3): This line creates a 3x3 NumPy array y containing elements from 3 to 11.

[[ 3, 4, 5],

[ 6, 7, 8],

[ 9, 10, 11]]

result = np.einsum("mk,kn", x, y): This line computes the matrix product of arrays x and y using the Einstein summation notation "mk,kn". The indices m, k, and n represent the row index of the first matrix, the column index of the first matrix (and row index of the second matrix), and the column index of the second matrix, respectively. The k index appears in both input arrays, so it is summed over, resulting in a matrix multiplication.

The final result is the matrix product of x and y:

[[ 24, 27, 30],

[ 78, 90, 102],

[132, 153, 174]]

Python-Numpy Code Editor: