评估爱因斯坦对两个多维 NumPy 数组的求和约定
在Python中,我们可以使用 NumPy 包的 einsum()函数来计算爱因斯坦对两个给定多维数组的求和约定。
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.
示例 1:爱因斯坦对两个 2X2 矩阵的求和约定
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)
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)
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)
输出:
Original matrix:
[[1 2]
[0 2]]
[[0 1]
[3 4]]
Einstein’s summation convention of the two matrix:
[[6 9]
[6 8]]
示例 2:爱因斯坦对两个 3X3 矩阵的求和约定
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)
输出:
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]]
示例 3:爱因斯坦对两个 4X4 矩阵的求和约定
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)
输出:
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]]