在 Python 中求解张量方程
要解决张量方程,请在 Python 中使用 numpy.linalg.tensorsolve() 方法。假设 x 的所有索引都在乘积中累加,以及 a 最右边的索引,就像在例如 tensordot(a, x, axes=b.ndim) 中一样。
第一个参数 a 是一个系数张量,形状为 b.shape + Q。Q 是一个元组,等于由 a 的适当数量的最右边索引组成的子张量的形状,并且必须满足 prod(Q) == prod(b.shape)。第二个参数 b 是一个右手张量,可以是任何形状。第三个参数 axis 是 a 中要重新排序到右侧的轴,在求逆之前。如果为 None(默认值),则不执行重新排序。
步骤
首先,导入所需的库 -
import numpy as np
使用 array() 方法创建两个 NumPy 数组
arr1 = np.eye(2*3*4) arr1.shape = (2*3, 4, 2, 3, 4) arr2 = np.random.randn(2*3, 4)
显示数组 -
print("Array1...\n",arr1)
print("\nArray2...\n",arr2)检查两个数组的维度 -
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)检查两个数组的形状 -
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)要解决张量方程,请在 Python 中使用 numpy.linalg.tensorsolve() 方法。假设 x 的所有索引都在乘积中累加,以及 a 最右边的索引,就像在例如 tensordot(a, x, axes=b.ndim) 中一样 -
print("\nResult...\n",np.linalg.tensorsolve(arr1, arr2))示例
import numpy as np
# Creating two numpy arrays using the array() method
arr1 = np.eye(2*3*4)
arr1.shape = (2*3, 4, 2, 3, 4)
arr2 = np.random.randn(2*3, 4)
# Display the arrays
print("Array1...\n",arr1)
print("\nArray2...\n",arr2)
# Check the Dimensions of both the arrays
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)
# Check the Shape of both the arrays
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)
# To solve the tensor equation, use the numpy.linalg.tensorsolve() method in Python.
print("\nResult...\n",np.linalg.tensorsolve(arr1, arr2))输出
Array1... [[[[[1. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0.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0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]] [[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 1. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]] [[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 1. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]] [[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 1.] [0. 0. 0. 0.] [0. 0. 0. 0.]]]] [[[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [1. 0. 0. 0.] [0. 0. 0. 0.]]] [[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 0. 0.]]] [[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 0.]]] [[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 1.] [0. 0. 0. 0.]]]] [[[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.] [1. 0. 0. 0.]]] [[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 1. 0. 0.]]] [[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 1. 0.]]] [[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 1.]]]]] Array2... [[ 0.31376716 0.63443741 0.58628101 0.62313096] [ 1.12528958 -1.18403238 -0.64663325 -0.24241201] [ 0.55598965 -2.00059925 -1.97946414 -1.72478953] [ 0.18976226 0.60572953 1.50157692 -2.4491463 ] [ 0.42461806 -2.17872016 0.49677904 -1.11634625] [-1.09074462 0.35475618 0.42474987 -1.34391368]] Dimensions of Array1... 5 Dimensions of Array2... 2 Shape of Array1... (6, 4, 2, 3, 4) Shape of Array2... (6, 4) Result... [[[ 0.31376716 0.63443741 0.58628101 0.62313096] [ 1.12528958 -1.18403238 -0.64663325 -0.24241201] [ 0.55598965 -2.00059925 -1.97946414 -1.72478953]] [[ 0.18976226 0.60572953 1.50157692 -2.4491463 ] [ 0.42461806 -2.17872016 0.49677904 -1.11634625] [-1.09074462 0.35475618 0.42474987 -1.34391368]]]
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP