网站网站二维码收钱怎么做的,怎么自己办网站,工程发布平台,英文网站注册Array
一般来说#xff0c;一维数组可对应向量#xff1b;二维数组对应矩阵#xff0c;高维数组则对应张量。故而与高维数组相关的大部分函数#xff0c;都封装在sympy.tensor中。但另一方面#xff0c;数组本身是一个非常通用的数据类型#xff0c;故而可以直接从sympy…Array
一般来说一维数组可对应向量二维数组对应矩阵高维数组则对应张量。故而与高维数组相关的大部分函数都封装在sympy.tensor中。但另一方面数组本身是一个非常通用的数据类型故而可以直接从sympy中导入
import sympy
from sympy import Array
a1 Array([[1, 2], [3, 4], [5, 6]])
a1.shape # (3,2)
a1.rank() # 2阶张量
print(sympy.latex(a1))a 1 [ 1 2 3 4 5 6 ] a_1 \left[\begin{matrix}1 2\\3 4\\5 6\end{matrix}\right] a1 135246
由于 a 1 a_1 a1是二阶张量故而可以转换为矩阵
a1.tomatrix()Matrix([
[1, 2],
[3, 4],
[5, 6]])Array类型的元素也可以是符号示例如下
from sympy.abc import x, y, z
a2 Array([[[x, y], [z, x*z]], [[1, x*y], [1/x, x/y]]])
a2.shape # (2,2,2)
a2.rank() # 3阶张量
print(sympy.latex(a2))a 2 [ [ x y z x z ] [ 1 x y 1 x x y ] ] a_2\left[\begin{matrix}\left[\begin{matrix}x y\\z x z\end{matrix}\right] \left[\begin{matrix}1 x y\\\frac{1}{x} \frac{x}{y}\end{matrix}\right]\end{matrix}\right] a2[[xzyxz][1x1xyyx]]
因为这个数组的阶数为3所以无法调用tomatrix转换为矩阵。
乘法和缩并
回顾向量之间的运算如果两个向量分别是行向量和列向量且元素个数相同那么二者相乘则有两种结局示例如下 [ c d ] [ a b ] [ c a c b d a d b ] [ a b ] [ c d ] a c b d \begin{bmatrix}c\\d\end{bmatrix}\begin{bmatrix}ab\end{bmatrix} \begin{bmatrix}cacb\\ dadb\end{bmatrix}\quad \begin{bmatrix}ab\end{bmatrix} \begin{bmatrix}c\\d\end{bmatrix}acbd [cd][ab][cadacbdb][ab][cd]acbd
若将向量看作张量那么前者是张量乘法后者则是在乘法之后进行了缩并操作。在sympy中tensorproduct即为张量乘法tensorcontraction则为缩并
from sympy import tensorproduct, tensorcontraction
from sympy.abc import a,b,c,d
A Array([a,b])
B Array([c,d])
p tensorproduct(A, B)
print(sympy.latex(p))
t tensorcontraction(p, (0,1))
sympy.latex(t) # ac bd其中tensorcontraction的第二个参数 ( 0 , 1 ) (0,1) (0,1)表示被缩并的轴。