NumPy: Evaluate Einstein’s summation convention of two given multidimensional arrays
5. Einstein Summation Convention
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 :
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]]
For more Practice: Solve these Related Problems:
- Use np.einsum to perform a tensor contraction that simulates matrix multiplication and compare it with np.dot.
- Implement a function that sums over specific axes of two multidimensional arrays using Einstein summation.
- Apply np.einsum to compute the trace of a matrix by summing over its diagonal indices.
- Evaluate a complex summation expression with np.einsum and verify the result against iterative summing.
Python-Numpy Code Editor:
Previous: Write a NumPy program to compute the determinant of a given square array.Next: Write a NumPy program to compute the inner product of vectors for 1-D arrays (without complex conjugation) and in higher dimension.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics