矩阵(线性代数)#
创建矩阵#
线性代数模块的设计尽可能简单。首先,我们进口并申报第一批 Matrix
对象:
>>> from sympy.interactive.printing import init_printing
>>> init_printing(use_unicode=False, wrap_line=False)
>>> from sympy.matrices import Matrix, eye, zeros, ones, diag, GramSchmidt
>>> M = Matrix([[1,0,0], [0,0,0]]); M
[1 0 0]
[ ]
[0 0 0]
>>> Matrix([M, (0, 0, -1)])
[1 0 0 ]
[ ]
[0 0 0 ]
[ ]
[0 0 -1]
>>> Matrix([[1, 2, 3]])
[1 2 3]
>>> Matrix([1, 2, 3])
[1]
[ ]
[2]
[ ]
[3]
除了从适当大小的列表和/或矩阵列表中创建矩阵外,SymPy还支持更高级的矩阵创建方法,包括单个值列表和维度输入:
>>> Matrix(2, 3, [1, 2, 3, 4, 5, 6])
[1 2 3]
[ ]
[4 5 6]
更有趣(也有用)的是使用2变量函数(或 lambda
)创建矩阵。在这里,我们创建一个在对角线上为1的指标函数,然后使用它来生成单位矩阵:
>>> def f(i,j):
... if i == j:
... return 1
... else:
... return 0
...
>>> Matrix(4, 4, f)
[1 0 0 0]
[ ]
[0 1 0 0]
[ ]
[0 0 1 0]
[ ]
[0 0 0 1]
最后让我们使用 lambda
要创建偶数排列项中有1的单线矩阵:
>>> Matrix(3, 4, lambda i,j: 1 - (i+j) % 2)
[1 0 1 0]
[ ]
[0 1 0 1]
[ ]
[1 0 1 0]
对于快速矩阵构造,还有一些特殊的构造函数: eye
是单位矩阵, zeros
和 ones
对于全零和全一的矩阵,以及 diag
把矩阵或元素放在对角线上:
>>> eye(4)
[1 0 0 0]
[ ]
[0 1 0 0]
[ ]
[0 0 1 0]
[ ]
[0 0 0 1]
>>> zeros(2)
[0 0]
[ ]
[0 0]
>>> zeros(2, 5)
[0 0 0 0 0]
[ ]
[0 0 0 0 0]
>>> ones(3)
[1 1 1]
[ ]
[1 1 1]
[ ]
[1 1 1]
>>> ones(1, 3)
[1 1 1]
>>> diag(1, Matrix([[1, 2], [3, 4]]))
[1 0 0]
[ ]
[0 1 2]
[ ]
[0 3 4]
基本操作#
在学习使用矩阵的同时,让我们选择一个条目易于识别的矩阵。要知道的一件有用的事情是,虽然矩阵是二维的,但是存储却不是二维的,因此允许(尽管应该小心)像访问一维列表一样访问条目。
>>> M = Matrix(2, 3, [1, 2, 3, 4, 5, 6])
>>> M[4]
5
现在,更标准的入口访问是一对索引,它们总是返回矩阵相应行和列的值:
>>> M[1, 2]
6
>>> M[0, 0]
1
>>> M[1, 1]
5
因为这是Python,所以我们也可以对子矩阵进行切片;切片总是返回一个矩阵,即使维度是1 x 1::
>>> M[0:2, 0:2]
[1 2]
[ ]
[4 5]
>>> M[2:2, 2]
[]
>>> M[:, 2]
[3]
[ ]
[6]
>>> M[:1, 2]
[3]
在上面的第二个例子中,注意切片2:2给出了一个空范围。还要注意(与Python基于0的索引保持一致),第一行/列是0。
不能访问不存在的行或列,除非它们位于切片中:
>>> M[:, 10] # the 10-th column (not there)
Traceback (most recent call last):
...
IndexError: Index out of range: a[[0, 10]]
>>> M[:, 10:11] # the 10-th column (if there)
[]
>>> M[:, :10] # all columns up to the 10-th
[1 2 3]
[ ]
[4 5 6]
只要对没有大小的坐标使用切片,对空矩阵进行切片即可工作:
>>> Matrix(0, 3, [])[:, 1]
[]
切片提供切片的副本,因此对一个对象的修改不会影响另一个对象:
>>> M2 = M[:, :]
>>> M2[0, 0] = 100
>>> M[0, 0] == 100
False
注意到 M2
没有改变 M
. 既然我们可以切片,我们也可以分配条目:
>>> M = Matrix(([1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]))
>>> M
[1 2 3 4 ]
[ ]
[5 6 7 8 ]
[ ]
[9 10 11 12]
[ ]
[13 14 15 16]
>>> M[2,2] = M[0,3] = 0
>>> M
[1 2 3 0 ]
[ ]
[5 6 7 8 ]
[ ]
[9 10 0 12]
[ ]
[13 14 15 16]
以及指定切片:
>>> M = Matrix(([1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]))
>>> M[2:,2:] = Matrix(2,2,lambda i,j: 0)
>>> M
[1 2 3 4]
[ ]
[5 6 7 8]
[ ]
[9 10 0 0]
[ ]
[13 14 0 0]
支持所有标准算术运算:
>>> M = Matrix(([1,2,3],[4,5,6],[7,8,9]))
>>> M - M
[0 0 0]
[ ]
[0 0 0]
[ ]
[0 0 0]
>>> M + M
[2 4 6 ]
[ ]
[8 10 12]
[ ]
[14 16 18]
>>> M * M
[30 36 42 ]
[ ]
[66 81 96 ]
[ ]
[102 126 150]
>>> M2 = Matrix(3,1,[1,5,0])
>>> M*M2
[11]
[ ]
[29]
[ ]
[47]
>>> M**2
[30 36 42 ]
[ ]
[66 81 96 ]
[ ]
[102 126 150]
以及一些有用的向量运算:
>>> M.row_del(0)
>>> M
[4 5 6]
[ ]
[7 8 9]
>>> M.col_del(1)
>>> M
[4 6]
[ ]
[7 9]
>>> v1 = Matrix([1,2,3])
>>> v2 = Matrix([4,5,6])
>>> v3 = v1.cross(v2)
>>> v1.dot(v2)
32
>>> v2.dot(v3)
0
>>> v1.dot(v3)
0
回想一下 row_del()
和 col_del()
操作不返回值-它们只是更改矩阵对象。我们还可以将适当大小的矩阵粘合在一起:
>>> M1 = eye(3)
>>> M2 = zeros(3, 4)
>>> M1.row_join(M2)
[1 0 0 0 0 0 0]
[ ]
[0 1 0 0 0 0 0]
[ ]
[0 0 1 0 0 0 0]
>>> M3 = zeros(4, 3)
>>> M1.col_join(M3)
[1 0 0]
[ ]
[0 1 0]
[ ]
[0 0 1]
[ ]
[0 0 0]
[ ]
[0 0 0]
[ ]
[0 0 0]
[ ]
[0 0 0]
分录操作#
我们不局限于两个矩阵之间的乘法:
>>> M = eye(3)
>>> 2*M
[2 0 0]
[ ]
[0 2 0]
[ ]
[0 0 2]
>>> 3*M
[3 0 0]
[ ]
[0 3 0]
[ ]
[0 0 3]
但是我们也可以使用 applyfunc()
. 在这里,我们将声明一个函数,它可以使任何输入数加倍。然后我们将其应用于3x3单位矩阵:
>>> f = lambda x: 2*x
>>> eye(3).applyfunc(f)
[2 0 0]
[ ]
[0 2 0]
[ ]
[0 0 2]
如果您想从矩阵中提取公因子,可以通过应用 gcd
对于矩阵的数据:
>>> from sympy.abc import x, y
>>> from sympy import gcd
>>> m = Matrix([[x, y], [1, x*y]]).inv('ADJ'); m
[ x*y -y ]
[-------- --------]
[ 2 2 ]
[x *y - y x *y - y]
[ ]
[ -1 x ]
[-------- --------]
[ 2 2 ]
[x *y - y x *y - y]
>>> gcd(tuple(_))
1
--------
2
x *y - y
>>> m/_
[x*y -y]
[ ]
[-1 x ]
另一个更有用的矩阵宽入口应用函数是替换函数。让我们用符号项声明一个矩阵,然后替换一个值。记住,我们可以用任何东西代替任何东西,甚至是另一个符号!:
>>> from sympy import Symbol
>>> x = Symbol('x')
>>> M = eye(3) * x
>>> M
[x 0 0]
[ ]
[0 x 0]
[ ]
[0 0 x]
>>> M.subs(x, 4)
[4 0 0]
[ ]
[0 4 0]
[ ]
[0 0 4]
>>> y = Symbol('y')
>>> M.subs(x, y)
[y 0 0]
[ ]
[0 y 0]
[ ]
[0 0 y]
线性代数#
现在我们已经有了基本的方法,让我们看看我们可以如何处理实际的矩阵。当然,首先想到的是决定因素:
>>> M = Matrix(( [1, 2, 3], [3, 6, 2], [2, 0, 1] ))
>>> M.det()
-28
>>> M2 = eye(3)
>>> M2.det()
1
>>> M3 = Matrix(( [1, 0, 0], [1, 0, 0], [1, 0, 0] ))
>>> M3.det()
0
另一个常见的操作是逆运算:在SymPy中,默认情况下,这是通过高斯消去计算的(对于稠密矩阵),但是我们可以指定它通过 \(LU\) 分解也:
>>> M2.inv()
[1 0 0]
[ ]
[0 1 0]
[ ]
[0 0 1]
>>> M2.inv(method="LU")
[1 0 0]
[ ]
[0 1 0]
[ ]
[0 0 1]
>>> M.inv(method="LU")
[-3/14 1/14 1/2 ]
[ ]
[-1/28 5/28 -1/4]
[ ]
[ 3/7 -1/7 0 ]
>>> M * M.inv(method="LU")
[1 0 0]
[ ]
[0 1 0]
[ ]
[0 0 1]
我们可以表演 \(QR\) 因式分解,便于求解系统:
>>> A = Matrix([[1,1,1],[1,1,3],[2,3,4]])
>>> Q, R = A.QRdecomposition()
>>> Q
[ ___ ___ ___ ]
[\/ 6 -\/ 3 -\/ 2 ]
[----- ------- -------]
[ 6 3 2 ]
[ ]
[ ___ ___ ___ ]
[\/ 6 -\/ 3 \/ 2 ]
[----- ------- ----- ]
[ 6 3 2 ]
[ ]
[ ___ ___ ]
[\/ 6 \/ 3 ]
[----- ----- 0 ]
[ 3 3 ]
>>> R
[ ___ ]
[ ___ 4*\/ 6 ___]
[\/ 6 ------- 2*\/ 6 ]
[ 3 ]
[ ]
[ ___ ]
[ \/ 3 ]
[ 0 ----- 0 ]
[ 3 ]
[ ]
[ ___ ]
[ 0 0 \/ 2 ]
>>> Q*R
[1 1 1]
[ ]
[1 1 3]
[ ]
[2 3 4]
除了 solver.py
文件中,我们可以通过将b向量传递给矩阵A的LUsolve函数来求解系统Ax=b。在这里我们要作弊,选择a和x,然后乘以b。然后我们可以求解x并检查它是否正确:
>>> A = Matrix([ [2, 3, 5], [3, 6, 2], [8, 3, 6] ])
>>> x = Matrix(3,1,[3,7,5])
>>> b = A*x
>>> soln = A.LUsolve(b)
>>> soln
[3]
[ ]
[7]
[ ]
[5]
还有一个很好的Gram-Schmidt正交化器,它将一组向量与另一组向量进行正交化。有一个可选参数指定输出是否也应该规范化,它默认为 False
. 让我们取一些向量并将它们正交化-一个是标准化的,另一个不是:
>>> L = [Matrix([2,3,5]), Matrix([3,6,2]), Matrix([8,3,6])]
>>> out1 = GramSchmidt(L)
>>> out2 = GramSchmidt(L, True)
让我们看看向量:
>>> for i in out1:
... print(i)
...
Matrix([[2], [3], [5]])
Matrix([[23/19], [63/19], [-47/19]])
Matrix([[1692/353], [-1551/706], [-423/706]])
>>> for i in out2:
... print(i)
...
Matrix([[sqrt(38)/19], [3*sqrt(38)/38], [5*sqrt(38)/38]])
Matrix([[23*sqrt(6707)/6707], [63*sqrt(6707)/6707], [-47*sqrt(6707)/6707]])
Matrix([[12*sqrt(706)/353], [-11*sqrt(706)/706], [-3*sqrt(706)/706]])
我们可以用dot()抽查它们的正交性,用norm()抽查它们的正规性:
>>> out1[0].dot(out1[1])
0
>>> out1[0].dot(out1[2])
0
>>> out1[1].dot(out1[2])
0
>>> out2[0].norm()
1
>>> out2[1].norm()
1
>>> out2[2].norm()
1
所以这个模块可以做很多工作,包括特征值、特征向量、零空间计算、余因子展开工具等等。从这里可以看到 matrices.py
所有功能的文件。
参考文献#
Matrix Base Classes#
The Matrix classes are built from functionality in various base classes. Every
methods and attribute of Matrix
is implemented on one of these base
classes. See also 稠密矩阵, and
稀疏矩阵.
- class sympy.matrices.matrixbase.MatrixBase[源代码]#
所有常见的矩阵运算,包括基本算术、成形和特殊矩阵,如 \(zeros\) 和 \(eye\) .
- property C#
元素共轭
- property D#
返回Dirac共轭(如果
self.rows == 4
)实例
>>> from sympy import Matrix, I, eye >>> m = Matrix((0, 1 + I, 2, 3)) >>> m.D Matrix([[0, 1 - I, -2, -3]]) >>> m = (eye(4) + I*eye(4)) >>> m[0, 3] = 2 >>> m.D Matrix([ [1 - I, 0, 0, 0], [ 0, 1 - I, 0, 0], [ 0, 0, -1 + I, 0], [ 2, 0, 0, -1 + I]])
如果矩阵没有4行,则将引发AttributeError,因为此属性仅为具有4行的矩阵定义。
>>> Matrix(eye(2)).D Traceback (most recent call last): ... AttributeError: Matrix has no attribute D.
- property H#
返回Hermite共轭。
实例
>>> from sympy import Matrix, I >>> m = Matrix((0, 1 + I, 2, 3)) >>> m Matrix([ [ 0], [1 + I], [ 2], [ 3]]) >>> m.H Matrix([[0, 1 - I, 2, 3]])
参见
- LDLdecomposition(hermitian=True)[源代码]#
返回矩阵A的LDL分解(L,D),使L * D * 五十、 如果hermitian标志为真,则H==A,或L * D * 五十、 如果hermitian为假,则T==A。这种方法消除了平方根的使用。此外,这确保了L的所有对角线条目都是1。如果Hermitian为真,A必须是Hermitian正定矩阵,否则必须是对称矩阵。
实例
>>> from sympy import Matrix, eye >>> A = Matrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11))) >>> L, D = A.LDLdecomposition() >>> L Matrix([ [ 1, 0, 0], [ 3/5, 1, 0], [-1/5, 1/3, 1]]) >>> D Matrix([ [25, 0, 0], [ 0, 9, 0], [ 0, 0, 9]]) >>> L * D * L.T * A.inv() == eye(A.rows) True
矩阵可以有复杂的条目:
>>> from sympy import I >>> A = Matrix(((9, 3*I), (-3*I, 5))) >>> L, D = A.LDLdecomposition() >>> L Matrix([ [ 1, 0], [-I/3, 1]]) >>> D Matrix([ [9, 0], [0, 4]]) >>> L*D*L.H == A True
- LDLsolve(rhs)[源代码]#
解决方案
Ax = B
利用LDL分解,对于一般的平方和非奇异矩阵。对于rows>cols的非平方矩阵,返回最小二乘解。
实例
>>> from sympy import Matrix, eye >>> A = eye(2)*2 >>> B = Matrix([[1, 2], [3, 4]]) >>> A.LDLsolve(B) == B/2 True
- LUdecomposition(iszerofunc=<function _iszero>, simpfunc=None, rankcheck=False)[源代码]#
Returns (L, U, perm) where L is a lower triangular matrix with unit diagonal, U is an upper triangular matrix, and perm is a list of row swap index pairs. If A is the original matrix, then
A = (L*U).permuteBkwd(perm)
, and the row permutation matrix P such that \(P A = L U\) can be computed byP = eye(A.rows).permuteFwd(perm)
.有关关键字参数rankcheck、iszerofunc和simpfunc的详细信息,请参阅LUCombined的文档。
- 参数:
rankcheck公司 :bool,可选
确定此函数是否应检测矩阵的秩不足并应引发
ValueError
.iszerofunc公司 :功能,可选
一种确定给定表达式是否为零的函数。
The function should be a callable that takes a single SymPy expression and returns a 3-valued boolean value
True
,False
, orNone
.它由轴搜索算法内部使用。有关pivot算法的更多信息,请参见“pivot”部分的注释。
辛普菲纳 :功能或无,可选
简化输入的函数。
If this is specified as a function, this function should be a callable that takes a single SymPy expression and returns an another SymPy expression that is algebraically equivalent.
如果
None
,它指示透视搜索算法不应尝试简化任何候选的轴。它由轴搜索算法内部使用。有关pivot算法的更多信息,请参见“pivot”部分的注释。
实例
>>> from sympy import Matrix >>> a = Matrix([[4, 3], [6, 3]]) >>> L, U, _ = a.LUdecomposition() >>> L Matrix([ [ 1, 0], [3/2, 1]]) >>> U Matrix([ [4, 3], [0, -3/2]])
- LUdecompositionFF()[源代码]#
计算无分数LU分解。
返回4个矩阵P,L,D,U,使PA=L D**-1u。如果矩阵的元素属于某个积分域I,那么L,D和U的所有元素都保证属于I。
工具书类
[R609]W、 Zhou和D.J.Jeffrey,“无分数矩阵因子:LU和QR因子的新形式”。《中国计算机科学前沿》,第二卷第一期,第67-80页,2008年。
- LUdecomposition_Simple(iszerofunc=<function _iszero>, simpfunc=None, rankcheck=False)[源代码]#
计算矩阵的PLU分解。
- 参数:
rankcheck公司 :bool,可选
确定此函数是否应检测矩阵的秩不足并应引发
ValueError
.iszerofunc公司 :功能,可选
一种确定给定表达式是否为零的函数。
The function should be a callable that takes a single SymPy expression and returns a 3-valued boolean value
True
,False
, orNone
.它由轴搜索算法内部使用。有关pivot算法的更多信息,请参见“pivot”部分的注释。
辛普菲纳 :功能或无,可选
简化输入的函数。
If this is specified as a function, this function should be a callable that takes a single SymPy expression and returns an another SymPy expression that is algebraically equivalent.
如果
None
,它指示透视搜索算法不应尝试简化任何候选的轴。它由轴搜索算法内部使用。有关pivot算法的更多信息,请参见“pivot”部分的注释。
- 返回:
(lu, row_swaps) :(矩阵,列表)
如果原始矩阵是\(m,n\)矩阵:
lu is a \(m, n\) matrix, which contains result of the decomposition in a compressed form. See the notes section to see how the matrix is compressed.
row_swaps 是一个\(m\)-element列表,其中每个元素是一对行交换索引。
A = (L*U).permute_backward(perm)
,而公式\(P A=L U\)中的行置换矩阵\(P\)可以通过P=eye(A.row).permute_forward(perm)
.- 加薪:
ValueError
如果
rankcheck=True
在计算过程中发现了矩阵的秩亏。
笔记
关于PLU分解:
PLU分解是LU分解的推广,它可以推广到秩亏矩阵。
它可以进一步推广到非平方矩阵,这是SymPy使用的符号。
PLU分解是a\(m,n\)矩阵\(a\)的分解,形式为\(P a=L U\),其中
- \(L\)是具有单位对角线的\(m,m\)下三角矩阵
条目。
\(U\)是一个\(m,n\)的上三角矩阵。
\(P\)是一个\(m,m\)排列矩阵。
因此,对于正方形矩阵,分解如下:
\[L=\开始{bmatrix}\]\[U=\begin{bmatrix}\]对于行多于列的矩阵,分解如下:
\[L=\开始{bmatrix}\]\[U=\begin{bmatrix}\]最后,对于列多于行的矩阵,分解如下所示:
\[L=\开始{bmatrix}\]\[U=\begin{bmatrix}\]关于压缩LU存储:
分解的结果通常以压缩形式存储,而不是分别返回\(L\)和\(U\)矩阵。
它可能不太具有启发性,但由于其效率,它通常用于许多数字库。
此特定方法的存储矩阵定义如下:
- \(L\)的子区域元素存储在子区域中
\(LU\)的一部分,即每当\(i>j\)时\(LU{i,j}=L{i,j}\)。
- \(L\)对角线上的元素都是1,而不是
显式存储。
- \(U\)存储在\(LU\)的上三角部分,即
每当\(i<=j\)时,\(LU{i,j}=U{i,j}\)。
- 对于\(m>n\)的情况,\(L\)矩阵的右侧为
不需要存储。
- 对于\(m<n\)的情况,\(U\)矩阵的下侧为
不需要存储。
因此,对于正方形矩阵,压缩输出矩阵为:
\[LU=\begin{bmatrix}\]对于行数多于列的矩阵,压缩输出矩阵为:
\[LU=\begin{bmatrix}\]对于列数多于行数的矩阵,压缩输出矩阵为:
\[LU=\begin{bmatrix}\]关于轴搜索算法:
当矩阵包含符号项时,轴搜索算法不同于每个项都可以归为零或非零的情况。该算法通过左上角与轴心位置重合的子矩阵逐列搜索。如果pivot存在,则它是当前搜索列中iszerofunc保证为非零的第一个条目。如果不存在这样的候选者,那么如果simpfunc不是None,则每个候选枢轴都将被简化。搜索是重复的,不同的是,如果
iszerofunc()
不能保证非零。在第二次搜索中,pivot是iszerofunc可以保证为非零的第一个候选对象。如果不存在这样的候选者,那么pivot就是iszerofunc返回None的第一个候选者。如果不存在这样的候选项,则在右侧的下一列中重复搜索。轴搜索算法与中的不同rref()
,它依赖于_find_reasonable_pivot()
. 的未来版本LUdecomposition_simple()
可以使用_find_reasonable_pivot()
.
- LUsolve(rhs, iszerofunc=<function _iszero>)[源代码]#
解线性系统
Ax = rhs
对于x
在哪里?A = M
.这是用于符号矩阵,用于实矩阵或复杂矩阵mpmath.lu嫒解算或者mpmath.qr_求解.
- QRdecomposition()[源代码]#
返回QR分解。
解释
QR分解是形式为\(A=QR\)的分解,其中
\(Q\)是列正交矩阵。
\(R\)是上三角(梯形)矩阵。
列正交矩阵满足\(\mathbb{I}=Q^H Q\),而全正交矩阵满足关系\(\mathbb{I}=qq^H=Q^H Q\),其中\(I\)是具有匹配维数的单位矩阵。
对于非平方或秩亏的矩阵,返回列正交矩阵就足够了,因为增加它们可能会导致冗余计算。另一个好处是,通过计算\(Q\)的列数,可以很容易地检查矩阵的秩。
如果您想增加结果以返回完全正交分解,您应该使用以下步骤。
用与其他列正交的列来扩充\(Q\)矩阵,使其成为正方形。
Augment the \(R\) matrix with zero rows to make it have the same shape as the original matrix.
程序将在示例部分进行说明。
实例
全秩矩阵示例:
>>> from sympy import Matrix >>> A = Matrix([[12, -51, 4], [6, 167, -68], [-4, 24, -41]]) >>> Q, R = A.QRdecomposition() >>> Q Matrix([ [ 6/7, -69/175, -58/175], [ 3/7, 158/175, 6/175], [-2/7, 6/35, -33/35]]) >>> R Matrix([ [14, 21, -14], [ 0, 175, -70], [ 0, 0, 35]])
如果矩阵是平方和满秩,\(Q\)矩阵在两个方向上都是正交的,并且不需要增广。
>>> Q * Q.H Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> Q.H * Q Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]])
>>> A == Q*R True
秩亏矩阵示例:
>>> A = Matrix([[12, -51, 0], [6, 167, 0], [-4, 24, 0]]) >>> Q, R = A.QRdecomposition() >>> Q Matrix([ [ 6/7, -69/175], [ 3/7, 158/175], [-2/7, 6/35]]) >>> R Matrix([ [14, 21, 0], [ 0, 175, 0]])
QRdecomposition might return a matrix Q that is rectangular. In this case the orthogonality condition might be satisfied as \(\mathbb{I} = Q.H*Q\) but not in the reversed product \(\mathbb{I} = Q * Q.H\).
>>> Q.H * Q Matrix([ [1, 0], [0, 1]]) >>> Q * Q.H Matrix([ [27261/30625, 348/30625, -1914/6125], [ 348/30625, 30589/30625, 198/6125], [ -1914/6125, 198/6125, 136/1225]])
如果您想将结果扩充为完全正交分解,您应该使用另一个正交列来扩充\(Q\)。
You are able to append an identity matrix, and you can run the Gram-Schmidt process to make them augmented as orthogonal basis.
>>> Q_aug = Q.row_join(Matrix.eye(3)) >>> Q_aug = Q_aug.QRdecomposition()[0] >>> Q_aug Matrix([ [ 6/7, -69/175, 58/175], [ 3/7, 158/175, -6/175], [-2/7, 6/35, 33/35]]) >>> Q_aug.H * Q_aug Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> Q_aug * Q_aug.H Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]])
用零行扩充\(R\)矩阵很简单。
>>> R_aug = R.col_join(Matrix([[0, 0, 0]])) >>> R_aug Matrix([ [14, 21, 0], [ 0, 175, 0], [ 0, 0, 0]]) >>> Q_aug * R_aug == A True
零矩阵示例:
>>> from sympy import Matrix >>> A = Matrix.zeros(3, 4) >>> Q, R = A.QRdecomposition()
它们可能返回行和列为零的矩阵。
>>> Q Matrix(3, 0, []) >>> R Matrix(0, 4, []) >>> Q*R Matrix([ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
正如上面描述的相同的扩充规则,\(Q\)可以用单位矩阵的列来扩充,\(R\)可以用零矩阵的行来扩充。
>>> Q_aug = Q.row_join(Matrix.eye(3)) >>> R_aug = R.col_join(Matrix.zeros(3, 4)) >>> Q_aug * Q_aug.T Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> R_aug Matrix([ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) >>> Q_aug * R_aug == A True
- QRsolve(b)[源代码]#
解线性系统
Ax = b
.M
矩阵是A
,方法参数为向量b
. 该方法返回解向量x
. 如果b
是一个矩阵,系统对每一列进行求解b
返回值是与b
.This method is slower (approximately by a factor of 2) but more stable for floating-point arithmetic than the LUsolve method. However, LUsolve usually uses an exact arithmetic, so you do not need to use QRsolve.
这主要用于教育目的和符号矩阵,用于实际(或复杂)矩阵的使用mpmath.qr_求解.
- property T#
矩阵换位
- __getitem__(key)[源代码]#
Implementations of __getitem__ should accept ints, in which case the matrix is indexed as a flat list, tuples (i,j) in which case the (i,j) entry is returned, slices, or mixed tuples (a,b) where a and b are any combination of slices and integers.
- __len__()[源代码]#
Return the number of elements of
self
.Implemented mainly so bool(Matrix()) == False.
- __mul__(other)[源代码]#
返回self*other,其中other是标量或兼容维度的矩阵。
实例
>>> from sympy import Matrix >>> A = Matrix([[1, 2, 3], [4, 5, 6]]) >>> 2*A == A*2 == Matrix([[2, 4, 6], [8, 10, 12]]) True >>> B = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> A*B Matrix([ [30, 36, 42], [66, 81, 96]]) >>> B*A Traceback (most recent call last): ... ShapeError: Matrices size mismatch. >>>
- __weakref__#
list of weak references to the object
- adjugate(method='berkowitz')[源代码]#
返回矩阵的伴随或经典伴随。也就是说,辅因子矩阵的转置。
https://en.wikipedia.org/wiki/Adjugate
- 参数:
方法 :字符串,可选
Method to use to find the cofactors, can be "bareiss", "berkowitz", "bird", "laplace" or "lu".
实例
>>> from sympy import Matrix >>> M = Matrix([[1, 2], [3, 4]]) >>> M.adjugate() Matrix([ [ 4, -2], [-3, 1]])
- analytic_func(f, x)[源代码]#
计算f(A),其中A是方阵,f是解析函数。
- 参数:
f :表达式
解析函数
x :符号
f参数
实例
>>> from sympy import Symbol, Matrix, S, log
>>> x = Symbol('x') >>> m = Matrix([[S(5)/4, S(3)/4], [S(3)/4, S(5)/4]]) >>> f = log(x) >>> m.analytic_func(f, x) Matrix([ [ 0, log(2)], [log(2), 0]])
- applyfunc(f)[源代码]#
对矩阵的每个元素应用一个函数。
实例
>>> from sympy import Matrix >>> m = Matrix(2, 2, lambda i, j: i*2+j) >>> m Matrix([ [0, 1], [2, 3]]) >>> m.applyfunc(lambda i: 2*i) Matrix([ [0, 2], [4, 6]])
- atoms(*types)[源代码]#
返回构成当前对象的原子。
实例
>>> from sympy.abc import x, y >>> from sympy import Matrix >>> Matrix([[x]]) Matrix([[x]]) >>> _.atoms() {x} >>> Matrix([[x, y], [y, x]]) Matrix([ [x, y], [y, x]]) >>> _.atoms() {x, y}
- bidiagonal_decomposition(upper=True)[源代码]#
Returns \((U,B,V.H)\) for
\[A = UBV^{H}\]where \(A\) is the input matrix, and \(B\) is its Bidiagonalized form
注:符号矩阵的双向计算可以挂起。
- 参数:
上面的 :布尔。是做上双音化还是下双音化。
上为真,下为假。
工具书类
[R610]Algorithm 5.4.2, Matrix computations by Golub and Van Loan, 4th edition
[R611]Complex Matrix Bidiagonalization, https://github.com/vslobody/Householder-Bidiagonalization
- bidiagonalize(upper=True)[源代码]#
返回\(B\),输入矩阵的双对角化形式。
注:符号矩阵的双向计算可以挂起。
- 参数:
上面的 :布尔。是做上双音化还是下双音化。
上为真,下为假。
工具书类
[R612]Algorithm 5.4.2, Matrix computations by Golub and Van Loan, 4th edition
- charpoly(x='lambda', simplify=<function _simplify>)[源代码]#
计算特征多项式det(x*I-M),其中I是单位矩阵。
返回PurePoly,因此使用不同的变量
x
不影响比较或多项式:- 参数:
x :字符串,可选
“lambda”变量的名称,默认为“lambda”。
简化 :功能,可选
用简化函数对特征多项式进行计算。默认为
simplify
.
实例
>>> from sympy import Matrix >>> from sympy.abc import x, y >>> M = Matrix([[1, 3], [2, 0]]) >>> M.charpoly() PurePoly(lambda**2 - lambda - 6, lambda, domain='ZZ') >>> M.charpoly(x) == M.charpoly(y) True >>> M.charpoly(x) == M.charpoly(y) True
指定
x
是可选的;一个名为lambda
默认情况下使用(以unicode格式打印时看起来不错):>>> M.charpoly().as_expr() lambda**2 - lambda - 6
如果
x
如果与现有符号冲突,将在名称前面加下划线以使其唯一:>>> M = Matrix([[1, 2], [x, 0]]) >>> M.charpoly(x).as_expr() _x**2 - _x - 2*x
无论是否传递符号,都可以使用gen属性获取生成器,因为它可能与传递的符号不同:
>>> M.charpoly(x).gen _x >>> M.charpoly(x).gen == x False
笔记
利用Samuelson-Berkowitz算法,在不进行除法运算的情况下,有效地计算了特征多项式。这样就可以计算出没有零因子的交换环上的特征多项式。
如果行列式det(x*I-M)可以像上三角矩阵或下三角矩阵一样容易求出,那么就不用Samuelson-Berkowitz算法来计算特征值和特征多项式。
参见
- cholesky(hermitian=True)[源代码]#
返回矩阵a的Cholesky类型分解,使L * L.H == A if hermitian flag is True, or L * 五十、 如果hermitian为假,则T==A。
如果Hermitian为真,A必须是一个Hermitian正定矩阵,如果Hermitian为False,则必须是对称矩阵。
实例
>>> from sympy import Matrix >>> A = Matrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11))) >>> A.cholesky() Matrix([ [ 5, 0, 0], [ 3, 3, 0], [-1, 1, 3]]) >>> A.cholesky() * A.cholesky().T Matrix([ [25, 15, -5], [15, 18, 0], [-5, 0, 11]])
矩阵可以有复杂的条目:
>>> from sympy import I >>> A = Matrix(((9, 3*I), (-3*I, 5))) >>> A.cholesky() Matrix([ [ 3, 0], [-I, 2]]) >>> A.cholesky() * A.cholesky().H Matrix([ [ 9, 3*I], [-3*I, 5]])
当矩阵不是正定矩阵时,非厄米型Cholesky分解可能是有用的。
>>> A = Matrix([[1, 2], [2, 1]]) >>> L = A.cholesky(hermitian=False) >>> L Matrix([ [1, 0], [2, sqrt(3)*I]]) >>> L*L.T == A True
- cofactor(i, j, method='berkowitz')[源代码]#
计算元素的余因子。
- 参数:
方法 :字符串,可选
Method to use to find the cofactors, can be "bareiss", "berkowitz", "bird", "laplace" or "lu".
实例
>>> from sympy import Matrix >>> M = Matrix([[1, 2], [3, 4]]) >>> M.cofactor(0, 1) -3
- cofactor_matrix(method='berkowitz')[源代码]#
返回一个包含每个元素的余因子的矩阵。
- 参数:
方法 :字符串,可选
Method to use to find the cofactors, can be "bareiss", "berkowitz", "bird", "laplace" or "lu".
实例
>>> from sympy import Matrix >>> M = Matrix([[1, 2], [3, 4]]) >>> M.cofactor_matrix() Matrix([ [ 4, -3], [-2, 1]])
参见
- col_insert(pos, other)[源代码]#
在给定的列位置插入一个或多个列。
实例
>>> from sympy import zeros, ones >>> M = zeros(3) >>> V = ones(3, 1) >>> M.col_insert(1, V) Matrix([ [0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]])
参见
- col_join(other)[源代码]#
沿着self的最后一行和另一行的第一行连接两个矩阵。
实例
>>> from sympy import zeros, ones >>> M = zeros(3) >>> V = ones(1, 3) >>> M.col_join(V) Matrix([ [0, 0, 0], [0, 0, 0], [0, 0, 0], [1, 1, 1]])
- columnspace(simplify=False)[源代码]#
返回跨越列空间的向量(矩阵对象)列表
M
实例
>>> from sympy import Matrix >>> M = Matrix(3, 3, [1, 3, 0, -2, -6, 0, 3, 9, 6]) >>> M Matrix([ [ 1, 3, 0], [-2, -6, 0], [ 3, 9, 6]]) >>> M.columnspace() [Matrix([ [ 1], [-2], [ 3]]), Matrix([ [0], [0], [6]])]
- classmethod companion(poly)[源代码]#
返回多项式的伴随矩阵。
实例
>>> from sympy import Matrix, Poly, Symbol, symbols >>> x = Symbol('x') >>> c0, c1, c2, c3, c4 = symbols('c0:5') >>> p = Poly(c0 + c1*x + c2*x**2 + c3*x**3 + c4*x**4 + x**5, x) >>> Matrix.companion(p) Matrix([ [0, 0, 0, 0, -c0], [1, 0, 0, 0, -c1], [0, 1, 0, 0, -c2], [0, 0, 1, 0, -c3], [0, 0, 0, 1, -c4]])
- condition_number()[源代码]#
返回矩阵的条件数。
这是最大奇异值除以最小奇异值
实例
>>> from sympy import Matrix, S >>> A = Matrix([[1, 0, 0], [0, 10, 0], [0, 0, S.One/10]]) >>> A.condition_number() 100
- conjugate()[源代码]#
返回by元素的共轭。
实例
>>> from sympy import SparseMatrix, I >>> a = SparseMatrix(((1, 2 + I), (3, 4), (I, -I))) >>> a Matrix([ [1, 2 + I], [3, 4], [I, -I]]) >>> a.C Matrix([ [ 1, 2 - I], [ 3, 4], [-I, I]])
参见
transpose
矩阵换位
H
厄米共轭
sympy.matrices.matrixbase.MatrixBase.D
狄拉克共轭
- connected_components()[源代码]#
当平方矩阵被视为加权图时,返回图的连接顶点列表。
实例
>>> from sympy import Matrix >>> A = Matrix([ ... [66, 0, 0, 68, 0, 0, 0, 0, 67], ... [0, 55, 0, 0, 0, 0, 54, 53, 0], ... [0, 0, 0, 0, 1, 2, 0, 0, 0], ... [86, 0, 0, 88, 0, 0, 0, 0, 87], ... [0, 0, 10, 0, 11, 12, 0, 0, 0], ... [0, 0, 20, 0, 21, 22, 0, 0, 0], ... [0, 45, 0, 0, 0, 0, 44, 43, 0], ... [0, 35, 0, 0, 0, 0, 34, 33, 0], ... [76, 0, 0, 78, 0, 0, 0, 0, 77]]) >>> A.connected_components() [[0, 3, 8], [1, 6, 7], [2, 4, 5]]
笔记
即使矩阵的任何符号元素在数学上可以不确定为零,这也只考虑了矩阵的结构方面,因此它们将被视为非零。
- connected_components_decomposition()[源代码]#
仅使用置换将方阵分解成块对角形式。
- 返回:
P, B :置换矩阵、BlockDiagMatrix
P 是用于解释中相似变换的置换矩阵。以及 B 是置换结果的块对角矩阵。
如果要从BlockDiagMatrix中获取对角块,请参见
get_diag_blocks()
.
解释
The decomposition is in a form of \(A = P^{-1} B P\) where \(P\) is a permutation matrix and \(B\) is a block diagonal matrix.
实例
>>> from sympy import Matrix, pprint >>> A = Matrix([ ... [66, 0, 0, 68, 0, 0, 0, 0, 67], ... [0, 55, 0, 0, 0, 0, 54, 53, 0], ... [0, 0, 0, 0, 1, 2, 0, 0, 0], ... [86, 0, 0, 88, 0, 0, 0, 0, 87], ... [0, 0, 10, 0, 11, 12, 0, 0, 0], ... [0, 0, 20, 0, 21, 22, 0, 0, 0], ... [0, 45, 0, 0, 0, 0, 44, 43, 0], ... [0, 35, 0, 0, 0, 0, 34, 33, 0], ... [76, 0, 0, 78, 0, 0, 0, 0, 77]])
>>> P, B = A.connected_components_decomposition() >>> pprint(P) PermutationMatrix((1 3)(2 8 5 7 4 6)) >>> pprint(B) [[66 68 67] ] [[ ] ] [[86 88 87] 0 0 ] [[ ] ] [[76 78 77] ] [ ] [ [55 54 53] ] [ [ ] ] [ 0 [45 44 43] 0 ] [ [ ] ] [ [35 34 33] ] [ ] [ [0 1 2 ]] [ [ ]] [ 0 0 [10 11 12]] [ [ ]] [ [20 21 22]]
>>> P = P.as_explicit() >>> B = B.as_explicit() >>> P.T*B*P == A True
笔记
当矩阵被视为一个加权图时,这个问题对应于图的连通分量的寻找。
- copy()[源代码]#
返回矩阵的副本。
实例
>>> from sympy import Matrix >>> A = Matrix(2, 2, [1, 2, 3, 4]) >>> A.copy() Matrix([ [1, 2], [3, 4]])
- cramer_solve(rhs, det_method='laplace')[源代码]#
Solves system of linear equations using Cramer's rule.
This method is relatively inefficient compared to other methods. However it only uses a single division, assuming a division-free determinant method is provided. This is helpful to minimize the chance of divide-by-zero cases in symbolic solutions to linear systems.
- 参数:
M :矩阵
The matrix representing the left hand side of the equation.
rhs :矩阵
The matrix representing the right hand side of the equation.
det_method : str or callable
The method to use to calculate the determinant of the matrix. The default is
'laplace'
. If a callable is passed, it should take a single argument, the matrix, and return the determinant of the matrix.- 返回:
x :矩阵
矩阵将满足
Ax = B
. 矩阵A的行数和矩阵B的列数相等。
实例
>>> from sympy import Matrix >>> A = Matrix([[0, -6, 1], [0, -6, -1], [-5, -2, 3]]) >>> B = Matrix([[-30, -9], [-18, -27], [-26, 46]]) >>> x = A.cramer_solve(B) >>> x Matrix([ [ 0, -5], [ 4, 3], [-6, 9]])
工具书类
- cross(b)[源代码]#
返回的叉积
self
和b
放宽相容维数的条件:如果每个元素都有3个元素,则一个与self
将被退回。如果b
形状与self
然后是交叉积的公共恒等式(比如 \(a \times b = - b \times a\) )会坚持的。- 参数:
b :3x1或1x3矩阵
参见
- det(method='bareiss', iszerofunc=None)[源代码]#
计算矩阵的行列式
M
是一个具体的矩阵对象,否则返回一个表达式Determinant(M)
如果M
是一个MatrixSymbol
或其他表达方式。- 参数:
方法 :字符串,可选
指定用于计算矩阵行列式的算法。
如果矩阵最多为3x3,则使用硬编码公式并忽略指定的方法。否则,默认为
'bareiss'
.另外,如果矩阵是上三角矩阵或下三角矩阵,则只需简单地将对角元素相乘来计算行列式,而忽略了指定的方法。
If it is set to
'domain-ge'
, then Gaussian elimination method will be used via using DomainMatrix.如果设置为
'bareiss'
,将使用Bareiss的无分数算法。如果设置为
'berkowitz'
,将使用Berkowitz的算法。If it is set to
'bird'
, Bird's algorithm will be used [R615].If it is set to
'laplace'
, Laplace's algorithm will be used.否则,如果设置为
'lu'
,将使用LU分解。备注
为了向后兼容,仍然可以使用“bareis”和“det_lu”之类的遗留键来指示相应的方法。现在,这些键也不区分大小写。但是,建议使用精确的键来指定方法。
iszerofunc公司 :FunctionType或None,可选
如果设置为
None
,默认为_iszero
如果方法设置为'bareiss'
和_is_zero_after_expand_mul
如果方法设置为'lu'
.它还可以接受任何用户指定的零测试函数,如果它被格式化为接受单个符号参数并返回的函数
True
如果测试为零,并且False
如果它测试为非零,并且None
如果它是不可判定的。- 返回:
det :基本
行列式结果。
- 加薪:
ValueError
如果为提供了无法识别的密钥
method
或iszerofunc
.NonSquareMatrixError
如果试图从非平方矩阵计算行列式。
实例
>>> from sympy import Matrix, eye, det >>> I3 = eye(3) >>> det(I3) 1 >>> M = Matrix([[1, 2], [3, 4]]) >>> det(M) -2 >>> det(M) == M.det() True >>> M.det(method="domain-ge") -2
工具书类
- det_LU_decomposition()[源代码]#
Compute matrix determinant using LU decomposition.
Note that this method fails if the LU decomposition itself fails. In particular, if the matrix has no inverse this method will fail.
TODO: Implement algorithm for sparse matrices (SFF), http://www.eecis.udel.edu/~saunders/papers/sffge/it5.ps.
参见
- classmethod diag(*args, strict=False, unpack=True, rows=None, cols=None, **kwargs)[源代码]#
返回具有指定对角线的矩阵。如果传递矩阵,则创建块对角矩阵(即矩阵的“直和”)。
克沃斯
- 排结果矩阵的行;如果
不给。
- 科尔斯结果矩阵的列;如果
不给。
cls:生成矩阵的类
unpack:bool,当为True(默认值)时,解压单个序列,而不是将其解释为矩阵。
strict:bool,当False(默认)时,允许矩阵具有可变长度的行。
实例
>>> from sympy import Matrix >>> Matrix.diag(1, 2, 3) Matrix([ [1, 0, 0], [0, 2, 0], [0, 0, 3]])
当前的默认设置是解压单个序列。如果不需要,则设置 \(unpack=False\) 它将被解释为矩阵。
>>> Matrix.diag([1, 2, 3]) == Matrix.diag(1, 2, 3) True
当传递多个元素时,每个元素都被解释为放在对角线上的东西。列表转换为矩阵。对角线的填充始终从上一项的右下角继续:这将创建一个分块对角线矩阵,无论矩阵是否为正方形。
>>> col = [1, 2, 3] >>> row = [[4, 5]] >>> Matrix.diag(col, row) Matrix([ [1, 0, 0], [2, 0, 0], [3, 0, 0], [0, 4, 5]])
什么时候? \(unpack\) 为False,则列表中的元素不必都具有相同的长度。设置 \(strict\) 设置为True将引发以下值错误:
>>> Matrix.diag([[1, 2, 3], [4, 5], [6]], unpack=False) Matrix([ [1, 2, 3], [4, 5, 0], [6, 0, 0]])
返回矩阵的类型可以用
cls
关键字。>>> from sympy import ImmutableMatrix >>> from sympy.utilities.misc import func_name >>> func_name(Matrix.diag(1, cls=ImmutableMatrix)) 'ImmutableDenseMatrix'
零维矩阵可用于将填充的起点定位在任意行或列的开头:
>>> from sympy import ones >>> r2 = ones(0, 2) >>> Matrix.diag(r2, 1, 2) Matrix([ [0, 0, 1, 0], [0, 0, 0, 2]])
- diagonal(k=0)[源代码]#
返回self的第k个对角线。主对角线对应于 \(k=0\) ;上下对角线对应 \(k > 0\) 和 \(k < 0\) 分别是。价值观 \(self[i, j]\) 为此 \(j - i = k\) ,按递增顺序返回 \(i + j\) 开始 \(i + j = |k|\) .
实例
>>> from sympy import Matrix >>> m = Matrix(3, 3, lambda i, j: j - i); m Matrix([ [ 0, 1, 2], [-1, 0, 1], [-2, -1, 0]]) >>> _.diagonal() Matrix([[0, 0, 0]]) >>> m.diagonal(1) Matrix([[1, 1]]) >>> m.diagonal(-2) Matrix([[-2]])
即使对角线作为矩阵返回,也可以使用单个索引进行元素检索:
>>> Matrix.diag(1, 2, 3).diagonal()[1] # instead of [0, 1] 2
参见
- diagonal_solve(rhs)[源代码]#
解决方案
Ax = B
有效地,其中A是一个对角矩阵,具有非零的对角项。实例
>>> from sympy import Matrix, eye >>> A = eye(2)*2 >>> B = Matrix([[1, 2], [3, 4]]) >>> A.diagonal_solve(B) == B/2 True
- diagonalize(reals_only=False, sort=False, normalize=False)[源代码]#
Return(P,D),其中D是对角线
D=P^-1 * M * P
其中M是电流矩阵。
- 参数:
reals_only :布尔。如果需要复数,是否抛出错误
对角化。(默认值:False)
sort :布尔。沿对角线对特征值进行排序。(默认值:False)
归一化 :布尔。如果为True,则规范化P的列(默认值:False)
实例
>>> from sympy import Matrix >>> M = Matrix(3, 3, [1, 2, 0, 0, 3, 0, 2, -4, 2]) >>> M Matrix([ [1, 2, 0], [0, 3, 0], [2, -4, 2]]) >>> (P, D) = M.diagonalize() >>> D Matrix([ [1, 0, 0], [0, 2, 0], [0, 0, 3]]) >>> P Matrix([ [-1, 0, -1], [ 0, 0, -1], [ 2, 1, 2]]) >>> P.inv() * M * P Matrix([ [1, 0, 0], [0, 2, 0], [0, 0, 3]])
- diff(*args, evaluate=True, **kwargs)[源代码]#
Calculate the derivative of each element in the matrix.
实例
>>> from sympy import Matrix >>> from sympy.abc import x, y >>> M = Matrix([[x, y], [1, 0]]) >>> M.diff(x) Matrix([ [1, 0], [0, 0]])
- dot(b, hermitian=None, conjugate_convention=None)[源代码]#
返回两个等长向量的点或内积。在这里
self
必须是Matrix
尺寸为1 x n或n x 1,以及b
必须是大小为1 x n、n x 1的矩阵或长度为n的列表/元组。将返回标量。默认情况下,
dot
不共轭self
或b
,即使存在复杂的条目。设置hermitian=True
(和可选的conjugate_convention
)计算厄米内积。可能的夸克是
hermitian
和conjugate_convention
.如果
conjugate_convention
是"left"
,"math"
或"maths"
,第一个向量的共轭 (self
)被使用。如果"right"
或"physics"
第二个向量的共轭b
使用。实例
>>> from sympy import Matrix >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> v = Matrix([1, 1, 1]) >>> M.row(0).dot(v) 6 >>> M.col(0).dot(v) 12 >>> v = [3, 2, 1] >>> M.row(0).dot(v) 10
>>> from sympy import I >>> q = Matrix([1*I, 1*I, 1*I]) >>> q.dot(q, hermitian=False) -3
>>> q.dot(q, hermitian=True) 3
>>> q1 = Matrix([1, 1, 1*I]) >>> q.dot(q1, hermitian=True, conjugate_convention="maths") 1 - 2*I >>> q.dot(q1, hermitian=True, conjugate_convention="physics") 1 + 2*I
- dual()[源代码]#
Returns the dual of a matrix.
A dual of a matrix is:
(1/2)*levicivita(i, j, k, l)*M(k, l)
summed over indices \(k\) and \(l\)由于levicivita方法对于任何指数的成对交换都是反对称的,因此对称矩阵的对偶矩阵就是零矩阵。严格地说,这里定义的对偶假设“矩阵” \(M\) 是一个反变反对称二阶张量,因此对偶是协变二阶张量。
- echelon_form(iszerofunc=<function _iszero>, simplify=False, with_pivots=False)[源代码]#
返回等于的矩阵行
M
那是梯队式的。注意矩阵的梯队形式是 not 但是,唯一的属性(如行空间和null空间)将被保留。实例
>>> from sympy import Matrix >>> M = Matrix([[1, 2], [3, 4]]) >>> M.echelon_form() Matrix([ [1, 2], [0, -2]])
- eigenvals(error_when_incomplete=True, **flags)[源代码]#
Compute eigenvalues of the matrix.
- 参数:
error_when_incomplete :bool,可选
如果设置为
True
,如果不计算所有特征值,则会产生错误。这是由roots
不返回完整的特征值列表。简化 :bool或function,可选
如果设置为
True
,它尝试在每个例程中应用默认简化方法返回最简化的表达式形式。如果设置为
False
,它将跳过此特定例程中的简化,以节省计算资源。如果一个函数被传递给,它将尝试应用特定函数作为简化方法。
理性的 :bool,可选
如果设置为
True
,每一个浮点数在计算前都要用有理数代替。它可以解决roots
例行程序不能很好地与浮动。倍数 :bool,可选
如果设置为
True
,结果将以列表的形式出现。如果设置为
False
,结果将以字典的形式出现。- 返回:
eigs :列表或dict
矩阵的特征值。返回格式将由键指定
multiple
.- 加薪:
MatrixError
如果没有计算足够的根。
NonSquareMatrixError
如果试图从非方阵计算特征值。
实例
>>> from sympy import Matrix >>> M = Matrix(3, 3, [0, 1, 1, 1, 0, 0, 1, 1, 1]) >>> M.eigenvals() {-1: 1, 0: 1, 2: 1}
笔记
矩阵\(a\)的特征值可以通过求解矩阵方程\(\det(a-\lambda I)=0来计算\)
It's not always possible to return radical solutions for eigenvalues for matrices larger than \(4, 4\) shape due to Abel-Ruffini theorem.
If there is no radical solution is found for the eigenvalue, it may return eigenvalues in the form of
sympy.polys.rootoftools.ComplexRootOf
.
- eigenvects(error_when_incomplete=True, iszerofunc=<function _iszero>, **flags)[源代码]#
Compute eigenvectors of the matrix.
- 参数:
error_when_incomplete :bool,可选
未计算所有特征值时引发错误。这是由
roots
不返回完整的特征值列表。iszerofunc公司 :功能,可选
指定要在中使用的零测试函数
rref
.默认值为
_iszero
,它使用SymPy天真而快速的默认假设处理程序。它还可以接受任何用户指定的零测试函数,如果它被格式化为接受单个符号参数并返回的函数
True
如果测试为零,并且False
如果测试为非零,并且None
如果它是不可判定的。简化 :bool或function,可选
如果
True
,as_content_primitive()
将用于整理规范化工件。它也将被
nullspace
例行公事。chop :bool或正数,可选
如果矩阵包含任何浮点数,为了计算的目的,它们将被更改为有理数,但在使用evalf求值后,将返回答案。这个
chop
标志传递给evalf
. 什么时候?chop=True
将使用默认精度;数字将被解释为所需的精度级别。- 返回:
ret : [(本征值,多重性,本征空间)。。。]
包含由
eigenvals
和nullspace
.eigenspace
是一个包含eigenvector
对于每个特征值。eigenvector
向量的形式是Matrix
. e、 g.长度为3的向量返回为Matrix([a_1, a_2, a_3])
.- 加薪:
NotImplementedError
如果无法计算空空间。
实例
>>> from sympy import Matrix >>> M = Matrix(3, 3, [0, 1, 1, 1, 0, 0, 1, 1, 1]) >>> M.eigenvects() [(-1, 1, [Matrix([ [-1], [ 1], [ 0]])]), (0, 1, [Matrix([ [ 0], [-1], [ 1]])]), (2, 1, [Matrix([ [2/3], [1/3], [ 1]])])]
- elementary_col_op(op='n->kn', col=None, k=None, col1=None, col2=None)[源代码]#
执行基本列操作 \(op\) .
\(op\) 可能是其中之一
"n->kn"
(column n goes to k*n)"n<->m"
(swap column n and column m)"n->n+km"
(column n goes to column n + k*column m)
- 参数:
op :string;基本行操作
col :要应用列操作的列
k :要在列操作中应用的倍数
col1 :列交换的一列
col2 :列交换的第二列或列操作中的列“m”
“n->n+km”
- elementary_row_op(op='n->kn', row=None, k=None, row1=None, row2=None)[源代码]#
执行基本行操作 \(op\) .
\(op\) 可能是其中之一
"n->kn"
(row n goes to k*n)"n<->m"
(swap row n and row m)"n->n+km"
(row n goes to row n + k*row m)
- 参数:
op :string;基本行操作
row :要应用行的操作
k :要在行操作中应用的倍数
row1 :行交换的一行
row2 :行交换的第二行或行操作中的行“m”
“n->n+km”
- evalf(n=15, subs=None, maxn=100, chop=False, strict=False, quad=None, verbose=False)[源代码]#
对self的每个元素应用evalf()。
- exp()[源代码]#
Return the exponential of a square matrix.
实例
>>> from sympy import Symbol, Matrix
>>> t = Symbol('t') >>> m = Matrix([[0, 1], [-1, 0]]) * t >>> m.exp() Matrix([ [ exp(I*t)/2 + exp(-I*t)/2, -I*exp(I*t)/2 + I*exp(-I*t)/2], [I*exp(I*t)/2 - I*exp(-I*t)/2, exp(I*t)/2 + exp(-I*t)/2]])
- expand(deep=True, modulus=None, power_base=True, power_exp=True, mul=True, log=True, multinomial=True, basic=True, **hints)[源代码]#
应用核心功能扩展到矩阵的每个条目。
实例
>>> from sympy.abc import x >>> from sympy import Matrix >>> Matrix(1, 1, [x*(x+1)]) Matrix([[x*(x + 1)]]) >>> _.expand() Matrix([[x**2 + x]])
- extract(rowsList, colsList)[源代码]#
Return a submatrix by specifying a list of rows and columns. Negative indices can be given. All indices must be in the range \(-n \le i < n\) where \(n\) is the number of rows or columns.
实例
>>> from sympy import Matrix >>> m = Matrix(4, 3, range(12)) >>> m Matrix([ [0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]) >>> m.extract([0, 1, 3], [0, 1]) Matrix([ [0, 1], [3, 4], [9, 10]])
行或列可以重复:
>>> m.extract([0, 0, 1], [-1]) Matrix([ [2], [2], [5]])
每隔一行可以使用range提供索引:
>>> m.extract(range(0, m.rows, 2), [-1]) Matrix([ [2], [8]])
RowsList或colsList也可以是布尔值的列表,在这种情况下,将选择与真值对应的行或列:
>>> m.extract([0, 1, 2, 3], [True, False, True]) Matrix([ [0, 2], [3, 5], [6, 8], [9, 11]])
- classmethod eye(rows, cols=None, **kwargs)[源代码]#
返回单位矩阵。
- 参数:
rows : rows of the matrix
cols : cols of the matrix (if None, cols=rows)
克沃斯
cls:返回矩阵的类
- property free_symbols#
返回矩阵中的自由符号。
实例
>>> from sympy.abc import x >>> from sympy import Matrix >>> Matrix([[x], [1]]).free_symbols {x}
- gauss_jordan_solve(B, freevar=False)[源代码]#
解决方案
Ax = B
使用高斯-乔丹消除。可能有零解、一解或无穷解。如果存在一个解决方案,它将被返回。如果存在无限解,则将以参数形式返回。如果不存在解决方案,它将抛出ValueError。
- 参数:
B :矩阵
要求解的方程的右侧。必须与矩阵A具有相同的行数。
免费 :布尔值,可选
标志,设置为时 \(True\) 将返回解(列矩阵)中自由变量的索引,对于一个未确定的系统(例如,列多于行),对于该系统,可以根据自由变量的任意值获得无限解。违约 \(False\) .
- 返回:
x :矩阵
矩阵将满足
Ax = B
. 矩阵A的行数和矩阵B的列数相等。帕拉姆 :矩阵
如果系统是欠定的(例如,A的列多于行),则可以根据任意参数获得无限解。这些任意参数以params Matrix的形式返回。
free_var_index :列表,可选
如果系统是欠定的(例如,A的列多于行),就自由变量的任意值而言,无限解是可能的。然后,如果标志 \(freevar\) 设置为 \(True\) .
实例
>>> from sympy import Matrix >>> A = Matrix([[1, 2, 1, 1], [1, 2, 2, -1], [2, 4, 0, 6]]) >>> B = Matrix([7, 12, 4]) >>> sol, params = A.gauss_jordan_solve(B) >>> sol Matrix([ [-2*tau0 - 3*tau1 + 2], [ tau0], [ 2*tau1 + 5], [ tau1]]) >>> params Matrix([ [tau0], [tau1]]) >>> taus_zeroes = { tau:0 for tau in params } >>> sol_unique = sol.xreplace(taus_zeroes) >>> sol_unique Matrix([ [2], [0], [5], [0]])
>>> A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 10]]) >>> B = Matrix([3, 6, 9]) >>> sol, params = A.gauss_jordan_solve(B) >>> sol Matrix([ [-1], [ 2], [ 0]]) >>> params Matrix(0, 1, [])
>>> A = Matrix([[2, -7], [-1, 4]]) >>> B = Matrix([[-21, 3], [12, -2]]) >>> sol, params = A.gauss_jordan_solve(B) >>> sol Matrix([ [0, -2], [3, -1]]) >>> params Matrix(0, 2, [])
>>> from sympy import Matrix >>> A = Matrix([[1, 2, 1, 1], [1, 2, 2, -1], [2, 4, 0, 6]]) >>> B = Matrix([7, 12, 4]) >>> sol, params, freevars = A.gauss_jordan_solve(B, freevar=True) >>> sol Matrix([ [-2*tau0 - 3*tau1 + 2], [ tau0], [ 2*tau1 + 5], [ tau1]]) >>> params Matrix([ [tau0], [tau1]]) >>> freevars [1, 3]
参见
sympy.matrices.dense.DenseMatrix.lower_triangular_solve
,sympy.matrices.dense.DenseMatrix.upper_triangular_solve
,cholesky_solve
,diagonal_solve
,LDLsolve
,LUsolve
,QRsolve
,pinv
工具书类
- get_diag_blocks()[源代码]#
得到方阵主对角线上的方子矩阵。
用于反转符号矩阵或求解可通过块对角结构解耦的线性方程组。
实例
>>> from sympy import Matrix >>> from sympy.abc import x, y, z >>> A = Matrix([[1, 3, 0, 0], [y, z*z, 0, 0], [0, 0, x, 0], [0, 0, 0, 0]]) >>> a1, a2, a3 = A.get_diag_blocks() >>> a1 Matrix([ [1, 3], [y, z**2]]) >>> a2 Matrix([[x]]) >>> a3 Matrix([[0]])
- has(*patterns)[源代码]#
测试是否有任何子表达式匹配任何模式。
实例
>>> from sympy import Matrix, SparseMatrix, Float >>> from sympy.abc import x, y >>> A = Matrix(((1, x), (0.2, 3))) >>> B = SparseMatrix(((1, x), (0.2, 3))) >>> A.has(x) True >>> A.has(y) False >>> A.has(Float) True >>> B.has(x) True >>> B.has(y) False >>> B.has(Float) True
- hat()[源代码]#
Return the skew-symmetric matrix representing the cross product, so that
self.hat() * b
is equivalent toself.cross(b)
.实例
Calling
hat
creates a skew-symmetric 3x3 Matrix from a 3x1 Matrix:>>> from sympy import Matrix >>> a = Matrix([1, 2, 3]) >>> a.hat() Matrix([ [ 0, -3, 2], [ 3, 0, -1], [-2, 1, 0]])
Multiplying it with another 3x1 Matrix calculates the cross product:
>>> b = Matrix([3, 2, 1]) >>> a.hat() * b Matrix([ [-4], [ 8], [-4]])
Which is equivalent to calling the
cross
method:>>> a.cross(b) Matrix([ [-4], [ 8], [-4]])
参见
- classmethod hstack(*args)[源代码]#
返回通过水平连接参数(即通过重复应用row_join)形成的矩阵。
实例
>>> from sympy import Matrix, eye >>> Matrix.hstack(eye(2), 2*eye(2)) Matrix([ [1, 0, 2, 0], [0, 1, 0, 2]])
- integrate(*args, **kwargs)[源代码]#
整合矩阵的每个元素。
args
将传递给integrate
功能。实例
>>> from sympy import Matrix >>> from sympy.abc import x, y >>> M = Matrix([[x, y], [1, 0]]) >>> M.integrate((x, )) Matrix([ [x**2/2, x*y], [ x, 0]]) >>> M.integrate((x, 0, 2)) Matrix([ [2, 2*y], [2, 0]])
- inv(method=None, iszerofunc=<function _iszero>, try_block_diag=False)[源代码]#
Return the inverse of a matrix using the method indicated. The default is DM if a suitable domain is found or otherwise GE for dense matrices LDL for sparse matrices.
- 参数:
method : ('DM', 'DMNC', 'GE', 'LU', 'ADJ', 'CH', 'LDL', 'QR')
iszerofunc公司 :功能,可选
要使用的零测试函数。
try_block_diag :bool,可选
如果为True,则将尝试使用get_diag_blocks()方法形成块对角矩阵,分别反转这些矩阵,然后重建完整的逆矩阵。
- 加薪:
ValueError
如果矩阵的行列式为零。
实例
>>> from sympy import SparseMatrix, Matrix >>> A = SparseMatrix([ ... [ 2, -1, 0], ... [-1, 2, -1], ... [ 0, 0, 2]]) >>> A.inv('CH') Matrix([ [2/3, 1/3, 1/6], [1/3, 2/3, 1/3], [ 0, 0, 1/2]]) >>> A.inv(method='LDL') # use of 'method=' is optional Matrix([ [2/3, 1/3, 1/6], [1/3, 2/3, 1/3], [ 0, 0, 1/2]]) >>> A * _ Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> A = Matrix(A) >>> A.inv('CH') Matrix([ [2/3, 1/3, 1/6], [1/3, 2/3, 1/3], [ 0, 0, 1/2]]) >>> A.inv('ADJ') == A.inv('GE') == A.inv('LU') == A.inv('CH') == A.inv('LDL') == A.inv('QR') True
笔记
根据
method
关键字,它调用适当的方法:DM .... Use DomainMatrix
inv_den
method DMNC .... Use DomainMatrixinv_den
method without cancellation GE .... inverse_GE(); default for dense matrices LU .... inverse_LU() ADJ ... inverse_ADJ() CH ... inverse_CH() LDL ... inverse_LDL(); default for sparse matrices QR ... inverse_QR()注意,GE和LU方法可能要求在倒置矩阵之前对矩阵进行简化,以便在旋转过程中正确检测零点。在困难情况下,可通过设置
iszerofunc
如果参数为零,则应返回True的函数的参数。ADJ例程计算行列式并使用它来检测奇异矩阵,以及测试对角线上的零点。
- classmethod irregular(ntop, *matrices, **kwargs)[源代码]#
返回由给定矩阵填充的矩阵,这些矩阵按从左到右、从上到下的顺序列出,因为它们首先出现在矩阵中。他们必须完全填满矩阵。
实例
>>> from sympy import ones, Matrix >>> Matrix.irregular(3, ones(2,1), ones(3,3)*2, ones(2,2)*3, ... ones(1,1)*4, ones(2,2)*5, ones(1,2)*6, ones(1,2)*7) Matrix([ [1, 2, 2, 2, 3, 3], [1, 2, 2, 2, 3, 3], [4, 2, 2, 2, 5, 5], [6, 6, 7, 7, 5, 5]])
- is_anti_symmetric(simplify=True)[源代码]#
检查矩阵M是否是反对称矩阵,即M是一个全M的平方矩阵 [i, j] ==-米 [j, i] .
什么时候?
simplify=True
(默认),总和M [i, j] +米 [j, i] 在测试前进行简化,以确定它是否为零。默认情况下,使用SymPy simplify函数。若要使用自定义函数集,请简化为接受返回简化表达式的单个参数的函数。若要跳过简化,请将simplify设置为False,但请注意,尽管这会更快,但可能会导致误报。实例
>>> from sympy import Matrix, symbols >>> m = Matrix(2, 2, [0, 1, -1, 0]) >>> m Matrix([ [ 0, 1], [-1, 0]]) >>> m.is_anti_symmetric() True >>> x, y = symbols('x y') >>> m = Matrix(2, 3, [0, 0, x, -y, 0, 0]) >>> m Matrix([ [ 0, 0, x], [-y, 0, 0]]) >>> m.is_anti_symmetric() False
>>> from sympy.abc import x, y >>> m = Matrix(3, 3, [0, x**2 + 2*x + 1, y, ... -(x + 1)**2, 0, x*y, ... -y, -x*y, 0])
Simplification of matrix elements is done by default so even though two elements which should be equal and opposite would not pass an equality test, the matrix is still reported as anti-symmetric:
>>> m[0, 1] == -m[1, 0] False >>> m.is_anti_symmetric() True
If
simplify=False
is used for the case when a Matrix is already simplified, this will speed things up. Here, we see that without simplification the matrix does not appear anti-symmetric:>>> m.is_anti_symmetric(simplify=False) False
但是,如果矩阵已经展开,那么它就会显得反对称,并且不需要在is_-u-symmetric例程中进行简化:
>>> m = m.expand() >>> m.is_anti_symmetric(simplify=False) True
- is_diagonal()[源代码]#
检查矩阵是否为对角矩阵,即主对角线以外的项都为零的矩阵。
实例
>>> from sympy import Matrix, diag >>> m = Matrix(2, 2, [1, 0, 0, 2]) >>> m Matrix([ [1, 0], [0, 2]]) >>> m.is_diagonal() True
>>> m = Matrix(2, 2, [1, 1, 0, 2]) >>> m Matrix([ [1, 1], [0, 2]]) >>> m.is_diagonal() False
>>> m = diag(1, 2, 3) >>> m Matrix([ [1, 0, 0], [0, 2, 0], [0, 0, 3]]) >>> m.is_diagonal() True
- is_diagonalizable(reals_only=False, **kwargs)[源代码]#
返回
True
如果矩阵是可对角化的。- 参数:
reals_only :bool,可选
如果
True
,它测试矩阵是否可以对角化为对角线上只包含实数。如果
False
,它测试矩阵是否完全可以对角化,即使数字可能不是真的。
实例
可对角化矩阵示例:
>>> from sympy import Matrix >>> M = Matrix([[1, 2, 0], [0, 3, 0], [2, -4, 2]]) >>> M.is_diagonalizable() True
不可对角化矩阵示例:
>>> M = Matrix([[0, 1], [0, 0]]) >>> M.is_diagonalizable() False
根据非实项对角化的矩阵示例:
>>> M = Matrix([[0, 1], [-1, 0]]) >>> M.is_diagonalizable(reals_only=False) True >>> M.is_diagonalizable(reals_only=True) False
- property is_echelon#
返回 \(True\) 如果矩阵是梯形的。也就是说,所有的零行都在底部,在一行的每个前导非零的下面都是排它的零。
- property is_hermitian#
检查矩阵是否为Hermitian。
在厄米矩阵元素i中,j是元素j,i的复共轭。
实例
>>> from sympy import Matrix >>> from sympy import I >>> from sympy.abc import x >>> a = Matrix([[1, I], [-I, 1]]) >>> a Matrix([ [ 1, I], [-I, 1]]) >>> a.is_hermitian True >>> a[0, 0] = 2*I >>> a.is_hermitian False >>> a[0, 0] = x >>> a.is_hermitian >>> a[0, 1] = a[1, 0]*I >>> a.is_hermitian False
- property is_indefinite#
求矩阵的确定性。
解释
平方实矩阵\(A\)为:
如果所有非零实向量\(x^T的x>0\),则为正定矩阵。
一个半正定矩阵,如果\(x^T A x\geq 0\)对于所有非零实向量\(x\)。
如果所有非零实向量\(x^T A x<0\),则为负定矩阵。
对于所有非零实向量\(x\),如果\(x^T为x\leq 0\),则为负半定矩阵。
如果存在非零实向量\(x,y\),且\(x^T A x>0>y^T A y\),则为不定矩阵。
正方形复矩阵\(A\)为:
如果\(\text{re}(x^H A x)>0\)对于所有非零复向量\(x\),则为正定矩阵。
所有非零复向量\(x\)的半正定矩阵if\(\text{re}(x^H A x)\geq 0\)。
如果\(\text{re}(x^H A x)<0\)对于所有非零复向量\(x\),则为负定矩阵。
对于所有非零复向量\(x\),如果\(\text{re}(x^H A x)\leq 0\),则为负半定矩阵。
如果存在非零复向量\(x,y\),且\(\text{re}(x^H A x)>0>\text{re}(y^H A y)\),则为不定矩阵。
矩阵不必是对称的或厄米特的正定矩阵。
实非对称矩阵是正定的当且仅当\(\frac{A+A^T}{2}\)是正定的。
复非厄米特矩阵是正定的当且仅当\(\frac{A+A^H}{2}\)是正定的。
这个扩展可以应用于上面所有的定义。
However, for complex cases, you can restrict the definition of \(\text{re}(x^H A x) > 0\) to \(x^H A x > 0\) and require the matrix to be hermitian. But we do not present this restriction for computation because you can check
M.is_hermitian
independently with this and use the same procedure.实例
对称正定矩阵的一个例子:
>>> from sympy import Matrix, symbols >>> from sympy.plotting import plot3d >>> a, b = symbols('a b') >>> x = Matrix([a, b])
>>> A = Matrix([[1, 0], [0, 1]]) >>> A.is_positive_definite True >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称半正定矩阵的一个例子:
>>> A = Matrix([[1, -1], [-1, 1]]) >>> A.is_positive_definite False >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称负定矩阵的一个例子:
>>> A = Matrix([[-1, 0], [0, -1]]) >>> A.is_negative_definite True >>> A.is_negative_semidefinite True >>> A.is_indefinite False
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称不定矩阵的一个例子:
>>> A = Matrix([[1, 2], [2, -1]]) >>> A.is_indefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
非对称正定矩阵的一个例子。
>>> A = Matrix([[1, 2], [-2, 1]]) >>> A.is_positive_definite True >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
笔记
尽管有些人将正定矩阵的定义简化为对称矩阵或厄米矩阵,但这种限制并不正确,因为它没有将定义为\(x^T A x>0\)或\(\text{re}(x^H A x)>0\)的所有正定矩阵实例进行分类。
例如,
Matrix([[1, 2], [-2, 1]])
上面的例子是一个非对称实正定矩阵的例子。然而,由于以下公式成立;
\[\文本{re}(x^H A x)>0\iff\]通过将矩阵变换为\(\frac{A+A^T}{2}\)或\(\frac{A+A^H}{2}\)(保证总是实对称或复hermitian),我们可以对所有可能对称或不对称的正定矩阵进行分类,并且可以将大部分研究推迟到对称或厄米特正定矩阵上。
But it is a different problem for the existence of Cholesky decomposition. Because even though a non symmetric or a non hermitian matrix can be positive definite, Cholesky or LDL decomposition does not exist because the decompositions require the matrix to be symmetric or hermitian.
工具书类
[R617]https://en.wikipedia.org/wiki/Definiteness_a_矩阵#特征值
[R619]正定矩阵〉,艾默尔。数学。月刊77259-264 1970。
- property is_lower#
检查矩阵是否为下三角矩阵。即使矩阵不是正方形,也可以返回True。
实例
>>> from sympy import Matrix >>> m = Matrix(2, 2, [1, 0, 0, 1]) >>> m Matrix([ [1, 0], [0, 1]]) >>> m.is_lower True
>>> m = Matrix(4, 3, [0, 0, 0, 2, 0, 0, 1, 4, 0, 6, 6, 5]) >>> m Matrix([ [0, 0, 0], [2, 0, 0], [1, 4, 0], [6, 6, 5]]) >>> m.is_lower True
>>> from sympy.abc import x, y >>> m = Matrix(2, 2, [x**2 + y, y**2 + x, 0, x + y]) >>> m Matrix([ [x**2 + y, x + y**2], [ 0, x + y]]) >>> m.is_lower False
- property is_lower_hessenberg#
检查矩阵是否为下Hessenberg形式。
下面的hessenberg矩阵在第一个超对角线上方有零个条目。
实例
>>> from sympy import Matrix >>> a = Matrix([[1, 2, 0, 0], [5, 2, 3, 0], [3, 4, 3, 7], [5, 6, 1, 1]]) >>> a Matrix([ [1, 2, 0, 0], [5, 2, 3, 0], [3, 4, 3, 7], [5, 6, 1, 1]]) >>> a.is_lower_hessenberg True
- property is_negative_definite#
求矩阵的确定性。
解释
平方实矩阵\(A\)为:
如果所有非零实向量\(x^T的x>0\),则为正定矩阵。
一个半正定矩阵,如果\(x^T A x\geq 0\)对于所有非零实向量\(x\)。
如果所有非零实向量\(x^T A x<0\),则为负定矩阵。
对于所有非零实向量\(x\),如果\(x^T为x\leq 0\),则为负半定矩阵。
如果存在非零实向量\(x,y\),且\(x^T A x>0>y^T A y\),则为不定矩阵。
正方形复矩阵\(A\)为:
如果\(\text{re}(x^H A x)>0\)对于所有非零复向量\(x\),则为正定矩阵。
所有非零复向量\(x\)的半正定矩阵if\(\text{re}(x^H A x)\geq 0\)。
如果\(\text{re}(x^H A x)<0\)对于所有非零复向量\(x\),则为负定矩阵。
对于所有非零复向量\(x\),如果\(\text{re}(x^H A x)\leq 0\),则为负半定矩阵。
如果存在非零复向量\(x,y\),且\(\text{re}(x^H A x)>0>\text{re}(y^H A y)\),则为不定矩阵。
矩阵不必是对称的或厄米特的正定矩阵。
实非对称矩阵是正定的当且仅当\(\frac{A+A^T}{2}\)是正定的。
复非厄米特矩阵是正定的当且仅当\(\frac{A+A^H}{2}\)是正定的。
这个扩展可以应用于上面所有的定义。
However, for complex cases, you can restrict the definition of \(\text{re}(x^H A x) > 0\) to \(x^H A x > 0\) and require the matrix to be hermitian. But we do not present this restriction for computation because you can check
M.is_hermitian
independently with this and use the same procedure.实例
对称正定矩阵的一个例子:
>>> from sympy import Matrix, symbols >>> from sympy.plotting import plot3d >>> a, b = symbols('a b') >>> x = Matrix([a, b])
>>> A = Matrix([[1, 0], [0, 1]]) >>> A.is_positive_definite True >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称半正定矩阵的一个例子:
>>> A = Matrix([[1, -1], [-1, 1]]) >>> A.is_positive_definite False >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称负定矩阵的一个例子:
>>> A = Matrix([[-1, 0], [0, -1]]) >>> A.is_negative_definite True >>> A.is_negative_semidefinite True >>> A.is_indefinite False
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称不定矩阵的一个例子:
>>> A = Matrix([[1, 2], [2, -1]]) >>> A.is_indefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
非对称正定矩阵的一个例子。
>>> A = Matrix([[1, 2], [-2, 1]]) >>> A.is_positive_definite True >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
笔记
尽管有些人将正定矩阵的定义简化为对称矩阵或厄米矩阵,但这种限制并不正确,因为它没有将定义为\(x^T A x>0\)或\(\text{re}(x^H A x)>0\)的所有正定矩阵实例进行分类。
例如,
Matrix([[1, 2], [-2, 1]])
上面的例子是一个非对称实正定矩阵的例子。然而,由于以下公式成立;
\[\文本{re}(x^H A x)>0\iff\]通过将矩阵变换为\(\frac{A+A^T}{2}\)或\(\frac{A+A^H}{2}\)(保证总是实对称或复hermitian),我们可以对所有可能对称或不对称的正定矩阵进行分类,并且可以将大部分研究推迟到对称或厄米特正定矩阵上。
But it is a different problem for the existence of Cholesky decomposition. Because even though a non symmetric or a non hermitian matrix can be positive definite, Cholesky or LDL decomposition does not exist because the decompositions require the matrix to be symmetric or hermitian.
工具书类
[R620]https://en.wikipedia.org/wiki/Definiteness_a_矩阵#特征值
[R622]正定矩阵〉,艾默尔。数学。月刊77259-264 1970。
- property is_negative_semidefinite#
求矩阵的确定性。
解释
平方实矩阵\(A\)为:
如果所有非零实向量\(x^T的x>0\),则为正定矩阵。
一个半正定矩阵,如果\(x^T A x\geq 0\)对于所有非零实向量\(x\)。
如果所有非零实向量\(x^T A x<0\),则为负定矩阵。
对于所有非零实向量\(x\),如果\(x^T为x\leq 0\),则为负半定矩阵。
如果存在非零实向量\(x,y\),且\(x^T A x>0>y^T A y\),则为不定矩阵。
正方形复矩阵\(A\)为:
如果\(\text{re}(x^H A x)>0\)对于所有非零复向量\(x\),则为正定矩阵。
所有非零复向量\(x\)的半正定矩阵if\(\text{re}(x^H A x)\geq 0\)。
如果\(\text{re}(x^H A x)<0\)对于所有非零复向量\(x\),则为负定矩阵。
对于所有非零复向量\(x\),如果\(\text{re}(x^H A x)\leq 0\),则为负半定矩阵。
如果存在非零复向量\(x,y\),且\(\text{re}(x^H A x)>0>\text{re}(y^H A y)\),则为不定矩阵。
矩阵不必是对称的或厄米特的正定矩阵。
实非对称矩阵是正定的当且仅当\(\frac{A+A^T}{2}\)是正定的。
复非厄米特矩阵是正定的当且仅当\(\frac{A+A^H}{2}\)是正定的。
这个扩展可以应用于上面所有的定义。
However, for complex cases, you can restrict the definition of \(\text{re}(x^H A x) > 0\) to \(x^H A x > 0\) and require the matrix to be hermitian. But we do not present this restriction for computation because you can check
M.is_hermitian
independently with this and use the same procedure.实例
对称正定矩阵的一个例子:
>>> from sympy import Matrix, symbols >>> from sympy.plotting import plot3d >>> a, b = symbols('a b') >>> x = Matrix([a, b])
>>> A = Matrix([[1, 0], [0, 1]]) >>> A.is_positive_definite True >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称半正定矩阵的一个例子:
>>> A = Matrix([[1, -1], [-1, 1]]) >>> A.is_positive_definite False >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称负定矩阵的一个例子:
>>> A = Matrix([[-1, 0], [0, -1]]) >>> A.is_negative_definite True >>> A.is_negative_semidefinite True >>> A.is_indefinite False
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称不定矩阵的一个例子:
>>> A = Matrix([[1, 2], [2, -1]]) >>> A.is_indefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
非对称正定矩阵的一个例子。
>>> A = Matrix([[1, 2], [-2, 1]]) >>> A.is_positive_definite True >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
笔记
尽管有些人将正定矩阵的定义简化为对称矩阵或厄米矩阵,但这种限制并不正确,因为它没有将定义为\(x^T A x>0\)或\(\text{re}(x^H A x)>0\)的所有正定矩阵实例进行分类。
例如,
Matrix([[1, 2], [-2, 1]])
上面的例子是一个非对称实正定矩阵的例子。然而,由于以下公式成立;
\[\文本{re}(x^H A x)>0\iff\]通过将矩阵变换为\(\frac{A+A^T}{2}\)或\(\frac{A+A^H}{2}\)(保证总是实对称或复hermitian),我们可以对所有可能对称或不对称的正定矩阵进行分类,并且可以将大部分研究推迟到对称或厄米特正定矩阵上。
But it is a different problem for the existence of Cholesky decomposition. Because even though a non symmetric or a non hermitian matrix can be positive definite, Cholesky or LDL decomposition does not exist because the decompositions require the matrix to be symmetric or hermitian.
工具书类
[R623]https://en.wikipedia.org/wiki/Definiteness_a_矩阵#特征值
[R625]正定矩阵〉,艾默尔。数学。月刊77259-264 1970。
- is_nilpotent()[源代码]#
检查矩阵是否幂零。
如果对于某个整数k,B**k是零矩阵,则矩阵B是幂零的。
实例
>>> from sympy import Matrix >>> a = Matrix([[0, 0, 0], [1, 0, 0], [1, 1, 0]]) >>> a.is_nilpotent() True
>>> a = Matrix([[1, 0, 1], [1, 0, 0], [1, 1, 0]]) >>> a.is_nilpotent() False
- property is_positive_definite#
求矩阵的确定性。
解释
平方实矩阵\(A\)为:
如果所有非零实向量\(x^T的x>0\),则为正定矩阵。
一个半正定矩阵,如果\(x^T A x\geq 0\)对于所有非零实向量\(x\)。
如果所有非零实向量\(x^T A x<0\),则为负定矩阵。
对于所有非零实向量\(x\),如果\(x^T为x\leq 0\),则为负半定矩阵。
如果存在非零实向量\(x,y\),且\(x^T A x>0>y^T A y\),则为不定矩阵。
正方形复矩阵\(A\)为:
如果\(\text{re}(x^H A x)>0\)对于所有非零复向量\(x\),则为正定矩阵。
所有非零复向量\(x\)的半正定矩阵if\(\text{re}(x^H A x)\geq 0\)。
如果\(\text{re}(x^H A x)<0\)对于所有非零复向量\(x\),则为负定矩阵。
对于所有非零复向量\(x\),如果\(\text{re}(x^H A x)\leq 0\),则为负半定矩阵。
如果存在非零复向量\(x,y\),且\(\text{re}(x^H A x)>0>\text{re}(y^H A y)\),则为不定矩阵。
矩阵不必是对称的或厄米特的正定矩阵。
实非对称矩阵是正定的当且仅当\(\frac{A+A^T}{2}\)是正定的。
复非厄米特矩阵是正定的当且仅当\(\frac{A+A^H}{2}\)是正定的。
这个扩展可以应用于上面所有的定义。
However, for complex cases, you can restrict the definition of \(\text{re}(x^H A x) > 0\) to \(x^H A x > 0\) and require the matrix to be hermitian. But we do not present this restriction for computation because you can check
M.is_hermitian
independently with this and use the same procedure.实例
对称正定矩阵的一个例子:
>>> from sympy import Matrix, symbols >>> from sympy.plotting import plot3d >>> a, b = symbols('a b') >>> x = Matrix([a, b])
>>> A = Matrix([[1, 0], [0, 1]]) >>> A.is_positive_definite True >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称半正定矩阵的一个例子:
>>> A = Matrix([[1, -1], [-1, 1]]) >>> A.is_positive_definite False >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称负定矩阵的一个例子:
>>> A = Matrix([[-1, 0], [0, -1]]) >>> A.is_negative_definite True >>> A.is_negative_semidefinite True >>> A.is_indefinite False
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称不定矩阵的一个例子:
>>> A = Matrix([[1, 2], [2, -1]]) >>> A.is_indefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
非对称正定矩阵的一个例子。
>>> A = Matrix([[1, 2], [-2, 1]]) >>> A.is_positive_definite True >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
笔记
尽管有些人将正定矩阵的定义简化为对称矩阵或厄米矩阵,但这种限制并不正确,因为它没有将定义为\(x^T A x>0\)或\(\text{re}(x^H A x)>0\)的所有正定矩阵实例进行分类。
例如,
Matrix([[1, 2], [-2, 1]])
上面的例子是一个非对称实正定矩阵的例子。然而,由于以下公式成立;
\[\文本{re}(x^H A x)>0\iff\]通过将矩阵变换为\(\frac{A+A^T}{2}\)或\(\frac{A+A^H}{2}\)(保证总是实对称或复hermitian),我们可以对所有可能对称或不对称的正定矩阵进行分类,并且可以将大部分研究推迟到对称或厄米特正定矩阵上。
But it is a different problem for the existence of Cholesky decomposition. Because even though a non symmetric or a non hermitian matrix can be positive definite, Cholesky or LDL decomposition does not exist because the decompositions require the matrix to be symmetric or hermitian.
工具书类
[R626]https://en.wikipedia.org/wiki/Definiteness_a_矩阵#特征值
[R628]正定矩阵〉,艾默尔。数学。月刊77259-264 1970。
- property is_positive_semidefinite#
求矩阵的确定性。
解释
平方实矩阵\(A\)为:
如果所有非零实向量\(x^T的x>0\),则为正定矩阵。
一个半正定矩阵,如果\(x^T A x\geq 0\)对于所有非零实向量\(x\)。
如果所有非零实向量\(x^T A x<0\),则为负定矩阵。
对于所有非零实向量\(x\),如果\(x^T为x\leq 0\),则为负半定矩阵。
如果存在非零实向量\(x,y\),且\(x^T A x>0>y^T A y\),则为不定矩阵。
正方形复矩阵\(A\)为:
如果\(\text{re}(x^H A x)>0\)对于所有非零复向量\(x\),则为正定矩阵。
所有非零复向量\(x\)的半正定矩阵if\(\text{re}(x^H A x)\geq 0\)。
如果\(\text{re}(x^H A x)<0\)对于所有非零复向量\(x\),则为负定矩阵。
对于所有非零复向量\(x\),如果\(\text{re}(x^H A x)\leq 0\),则为负半定矩阵。
如果存在非零复向量\(x,y\),且\(\text{re}(x^H A x)>0>\text{re}(y^H A y)\),则为不定矩阵。
矩阵不必是对称的或厄米特的正定矩阵。
实非对称矩阵是正定的当且仅当\(\frac{A+A^T}{2}\)是正定的。
复非厄米特矩阵是正定的当且仅当\(\frac{A+A^H}{2}\)是正定的。
这个扩展可以应用于上面所有的定义。
However, for complex cases, you can restrict the definition of \(\text{re}(x^H A x) > 0\) to \(x^H A x > 0\) and require the matrix to be hermitian. But we do not present this restriction for computation because you can check
M.is_hermitian
independently with this and use the same procedure.实例
对称正定矩阵的一个例子:
>>> from sympy import Matrix, symbols >>> from sympy.plotting import plot3d >>> a, b = symbols('a b') >>> x = Matrix([a, b])
>>> A = Matrix([[1, 0], [0, 1]]) >>> A.is_positive_definite True >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称半正定矩阵的一个例子:
>>> A = Matrix([[1, -1], [-1, 1]]) >>> A.is_positive_definite False >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称负定矩阵的一个例子:
>>> A = Matrix([[-1, 0], [0, -1]]) >>> A.is_negative_definite True >>> A.is_negative_semidefinite True >>> A.is_indefinite False
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
对称不定矩阵的一个例子:
>>> A = Matrix([[1, 2], [2, -1]]) >>> A.is_indefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
非对称正定矩阵的一个例子。
>>> A = Matrix([[1, 2], [-2, 1]]) >>> A.is_positive_definite True >>> A.is_positive_semidefinite True
>>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
笔记
尽管有些人将正定矩阵的定义简化为对称矩阵或厄米矩阵,但这种限制并不正确,因为它没有将定义为\(x^T A x>0\)或\(\text{re}(x^H A x)>0\)的所有正定矩阵实例进行分类。
例如,
Matrix([[1, 2], [-2, 1]])
上面的例子是一个非对称实正定矩阵的例子。然而,由于以下公式成立;
\[\文本{re}(x^H A x)>0\iff\]通过将矩阵变换为\(\frac{A+A^T}{2}\)或\(\frac{A+A^H}{2}\)(保证总是实对称或复hermitian),我们可以对所有可能对称或不对称的正定矩阵进行分类,并且可以将大部分研究推迟到对称或厄米特正定矩阵上。
But it is a different problem for the existence of Cholesky decomposition. Because even though a non symmetric or a non hermitian matrix can be positive definite, Cholesky or LDL decomposition does not exist because the decompositions require the matrix to be symmetric or hermitian.
工具书类
[R629]https://en.wikipedia.org/wiki/Definiteness_a_矩阵#特征值
[R631]正定矩阵〉,艾默尔。数学。月刊77259-264 1970。
- property is_square#
检查矩阵是否为正方形。
如果行数等于列数,则矩阵为正方形。空矩阵定义为正方形,因为行数和列数都为零。
实例
>>> from sympy import Matrix >>> a = Matrix([[1, 2, 3], [4, 5, 6]]) >>> b = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> c = Matrix([]) >>> a.is_square False >>> b.is_square True >>> c.is_square True
- property is_strongly_diagonally_dominant#
检验矩阵是否行强对角占优。
解释
如果
\[\左| A{i,i}\right |>\sum{j=0,j\neq i}^{n-1}\]实例
>>> from sympy import Matrix >>> A = Matrix([[3, -2, 1], [1, -3, 2], [-1, 2, 4]]) >>> A.is_strongly_diagonally_dominant False
>>> A = Matrix([[-2, 2, 1], [1, 3, 2], [1, -2, 0]]) >>> A.is_strongly_diagonally_dominant False
>>> A = Matrix([[-4, 2, 1], [1, 6, 2], [1, -2, 5]]) >>> A.is_strongly_diagonally_dominant True
笔记
如果您想测试一个矩阵是否是列对角占优的,可以在转置矩阵之后应用该测试。
- is_symbolic()[源代码]#
检查是否有任何元素包含符号。
实例
>>> from sympy import Matrix >>> from sympy.abc import x, y >>> M = Matrix([[x, y], [1, 0]]) >>> M.is_symbolic() True
- is_symmetric(simplify=True)[源代码]#
检查矩阵是否为对称矩阵,即方阵且等于其转置。
默认情况下,在测试对称性之前会进行简化。使用“simplify=False”可以跳过它们;虽然加快了速度,但这可能会导致错误的否定。
实例
>>> from sympy import Matrix >>> m = Matrix(2, 2, [0, 1, 1, 2]) >>> m Matrix([ [0, 1], [1, 2]]) >>> m.is_symmetric() True
>>> m = Matrix(2, 2, [0, 1, 2, 0]) >>> m Matrix([ [0, 1], [2, 0]]) >>> m.is_symmetric() False
>>> m = Matrix(2, 3, [0, 0, 0, 0, 0, 0]) >>> m Matrix([ [0, 0, 0], [0, 0, 0]]) >>> m.is_symmetric() False
>>> from sympy.abc import x, y >>> m = Matrix(3, 3, [1, x**2 + 2*x + 1, y, (x + 1)**2, 2, 0, y, 0, 3]) >>> m Matrix([ [ 1, x**2 + 2*x + 1, y], [(x + 1)**2, 2, 0], [ y, 0, 3]]) >>> m.is_symmetric() True
如果矩阵已经被简化,可以使用“simplify=False”来加速is_symmetric()测试。
>>> bool(m.is_symmetric(simplify=False)) False >>> m1 = m.expand() >>> m1.is_symmetric(simplify=False) True
- property is_upper#
检查矩阵是否为上三角矩阵。即使矩阵不是正方形,也可以返回True。
实例
>>> from sympy import Matrix >>> m = Matrix(2, 2, [1, 0, 0, 1]) >>> m Matrix([ [1, 0], [0, 1]]) >>> m.is_upper True
>>> m = Matrix(4, 3, [5, 1, 9, 0, 4, 6, 0, 0, 5, 0, 0, 0]) >>> m Matrix([ [5, 1, 9], [0, 4, 6], [0, 0, 5], [0, 0, 0]]) >>> m.is_upper True
>>> m = Matrix(2, 3, [4, 2, 5, 6, 1, 1]) >>> m Matrix([ [4, 2, 5], [6, 1, 1]]) >>> m.is_upper False
- property is_upper_hessenberg#
检查矩阵是否为上Hessenberg形式。
上面的hessenberg矩阵在第一个子矩阵下面有零个条目。
实例
>>> from sympy import Matrix >>> a = Matrix([[1, 4, 2, 3], [3, 4, 1, 7], [0, 2, 3, 4], [0, 0, 1, 3]]) >>> a Matrix([ [1, 4, 2, 3], [3, 4, 1, 7], [0, 2, 3, 4], [0, 0, 1, 3]]) >>> a.is_upper_hessenberg True
- property is_weakly_diagonally_dominant#
测试矩阵是否为弱对角占优的行。
解释
如果A\(n,n\)矩阵\(A\)是弱对角占优行
\[\左| A{i,i}\右|\ge\sum{j=0,j\neq i}^{n-1}\]实例
>>> from sympy import Matrix >>> A = Matrix([[3, -2, 1], [1, -3, 2], [-1, 2, 4]]) >>> A.is_weakly_diagonally_dominant True
>>> A = Matrix([[-2, 2, 1], [1, 3, 2], [1, -2, 0]]) >>> A.is_weakly_diagonally_dominant False
>>> A = Matrix([[-4, 2, 1], [1, 6, 2], [1, -2, 5]]) >>> A.is_weakly_diagonally_dominant True
笔记
如果您想测试一个矩阵是否是列对角占优的,可以在转置矩阵之后应用该测试。
- property is_zero_matrix#
检查矩阵是否为零矩阵。
如果每个元素都是零,那么矩阵就是零。矩阵不必是平方的就可以认为是零。根据虚真原理,空矩阵为零。对于可能为零也可能不为零的矩阵(例如,包含符号),则为无
实例
>>> from sympy import Matrix, zeros >>> from sympy.abc import x >>> a = Matrix([[0, 0], [0, 0]]) >>> b = zeros(3, 4) >>> c = Matrix([[0, 1], [0, 0]]) >>> d = Matrix([]) >>> e = Matrix([[x, 0], [0, 0]]) >>> a.is_zero_matrix True >>> b.is_zero_matrix True >>> c.is_zero_matrix False >>> d.is_zero_matrix True >>> e.is_zero_matrix
- jacobian(X)[源代码]#
计算雅可比矩阵(向量值函数的导数)。
- 参数:
``self`` :表示函数f_i(x_1,…,x n)的表达式向量。
X :按顺序设置x_i,可以是列表或矩阵
Both ``self`` and X can be a row or a column matrix in any order
(即jacobian()应该始终有效)。
实例
>>> from sympy import sin, cos, Matrix >>> from sympy.abc import rho, phi >>> X = Matrix([rho*cos(phi), rho*sin(phi), rho**2]) >>> Y = Matrix([rho, phi]) >>> X.jacobian(Y) Matrix([ [cos(phi), -rho*sin(phi)], [sin(phi), rho*cos(phi)], [ 2*rho, 0]]) >>> X = Matrix([rho*cos(phi), rho*sin(phi)]) >>> X.jacobian(Y) Matrix([ [cos(phi), -rho*sin(phi)], [sin(phi), rho*cos(phi)]])
- classmethod jordan_block(size=None, eigenvalue=None, *, band='upper', **kwargs)[源代码]#
返回一个约旦方块
- 参数:
size :整数,可选
指定约旦块矩阵的形状。
特征值 :数字或符号
指定矩阵主对角线的值。
备注
关键词
eigenval
也被指定为此关键字的别名,但不建议使用。在以后的版本中,我们可能不赞成使用别名。
band :“上”或“下”,可选
指定要放置的非对角线的位置 \(1\) 开了。
cls :矩阵,可选
指定输出窗体的矩阵类。
如果未指定,则将返回执行方法的类类型。
- 返回:
矩阵
乔丹块矩阵。
- 加薪:
ValueError
如果没有为矩阵大小指定足够的参数,或者没有给出特征值。
实例
创建默认的Jordan块:
>>> from sympy import Matrix >>> from sympy.abc import x >>> Matrix.jordan_block(4, x) Matrix([ [x, 1, 0, 0], [0, x, 1, 0], [0, 0, x, 1], [0, 0, 0, x]])
创建一个替代的Jordan块矩阵,其中 \(1\) 在下对角线上:
>>> Matrix.jordan_block(4, x, band='lower') Matrix([ [x, 0, 0, 0], [1, x, 0, 0], [0, 1, x, 0], [0, 0, 1, x]])
使用关键字参数创建Jordan块
>>> Matrix.jordan_block(size=4, eigenvalue=x) Matrix([ [x, 1, 0, 0], [0, x, 1, 0], [0, 0, x, 1], [0, 0, 0, x]])
工具书类
- jordan_form(calc_transform=True, **kwargs)[源代码]#
Return\((P,J)\),其中\(J\)是一个Jordan块矩阵,\(P\)是这样的矩阵:\(M=P jp^{-1}\)
- 参数:
calc_transform 布尔
如果
False
,则只返回\(J\)。chop 布尔
在计算特征值和特征向量时,所有的矩阵都被转换成精确的类型。因此,可能存在近似误差。如果
chop==True
这些错误将被截断。
实例
>>> from sympy import Matrix >>> M = Matrix([[ 6, 5, -2, -3], [-3, -1, 3, 3], [ 2, 1, -2, -3], [-1, 1, 5, 5]]) >>> P, J = M.jordan_form() >>> J Matrix([ [2, 1, 0, 0], [0, 2, 0, 0], [0, 0, 2, 1], [0, 0, 0, 2]])
参见
- left_eigenvects(**flags)[源代码]#
返回左特征向量和特征值。
此函数返回左特征向量的三元组(特征值、重数、基)列表。选项与eigenvects()相同,即
**flags
参数直接传递给eigenvects()。实例
>>> from sympy import Matrix >>> M = Matrix([[0, 1, 1], [1, 0, 0], [1, 1, 1]]) >>> M.eigenvects() [(-1, 1, [Matrix([ [-1], [ 1], [ 0]])]), (0, 1, [Matrix([ [ 0], [-1], [ 1]])]), (2, 1, [Matrix([ [2/3], [1/3], [ 1]])])] >>> M.left_eigenvects() [(-1, 1, [Matrix([[-2, 1, 1]])]), (0, 1, [Matrix([[-1, -1, 1]])]), (2, 1, [Matrix([[1, 1, 1]])])]
- limit(*args)[源代码]#
计算矩阵中每个元素的极限。
args
将传递给limit
功能。实例
>>> from sympy import Matrix >>> from sympy.abc import x, y >>> M = Matrix([[x, y], [1, 0]]) >>> M.limit(x, 2) Matrix([ [2, y], [1, 0]])
- log(simplify=<function cancel>)[源代码]#
Return the logarithm of a square matrix.
- 参数:
简化 :函数,bool
用于简化结果的函数。
缺省值为
cancel
有效地减少了符号矩阵取倒数和求逆所带来的表达式增长。
实例
>>> from sympy import S, Matrix
正定矩阵示例:
>>> m = Matrix([[1, 1], [0, 1]]) >>> m.log() Matrix([ [0, 1], [0, 0]])
>>> m = Matrix([[S(5)/4, S(3)/4], [S(3)/4, S(5)/4]]) >>> m.log() Matrix([ [ 0, log(2)], [log(2), 0]])
非正定矩阵示例:
>>> m = Matrix([[S(3)/4, S(5)/4], [S(5)/4, S(3)/4]]) >>> m.log() Matrix([ [ I*pi/2, log(2) - I*pi/2], [log(2) - I*pi/2, I*pi/2]])
>>> m = Matrix( ... [[0, 0, 0, 1], ... [0, 0, 1, 0], ... [0, 1, 0, 0], ... [1, 0, 0, 0]]) >>> m.log() Matrix([ [ I*pi/2, 0, 0, -I*pi/2], [ 0, I*pi/2, -I*pi/2, 0], [ 0, -I*pi/2, I*pi/2, 0], [-I*pi/2, 0, 0, I*pi/2]])
- lower_triangular(k=0)[源代码]#
Return the elements on and below the kth diagonal of a matrix. If k is not specified then simply returns lower-triangular portion of a matrix
实例
>>> from sympy import ones >>> A = ones(4) >>> A.lower_triangular() Matrix([ [1, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0], [1, 1, 1, 1]])
>>> A.lower_triangular(-2) Matrix([ [0, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0], [1, 1, 0, 0]])
>>> A.lower_triangular(1) Matrix([ [1, 1, 0, 0], [1, 1, 1, 0], [1, 1, 1, 1], [1, 1, 1, 1]])
- minor(i, j, method='berkowitz')[源代码]#
返回的(i,j)小调
M
. 也就是说,返回通过删除 \(i\) 第行和 \(j\) 第列来自M
.- 参数:
i, j :内景
要排除以获取子矩阵的行和列。
方法 :字符串,可选
Method to use to find the determinant of the submatrix, can be "bareiss", "berkowitz", "bird", "laplace" or "lu".
实例
>>> from sympy import Matrix >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M.minor(1, 1) -12
参见
- minor_submatrix(i, j)[源代码]#
返回通过移除 \(i\) 第行和 \(j\) 第列来自
M
(与 Python 负指数一起工作)。- 参数:
i, j :内景
要排除以获取子矩阵的行和列。
实例
>>> from sympy import Matrix >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M.minor_submatrix(1, 1) Matrix([ [1, 3], [7, 9]])
- multiply(other, dotprodsimp=None)[源代码]#
与uuUmul_Uu9()相同,但具有可选的简化功能。
- 参数:
dotprodsimp公司 :bool,可选
指定在矩阵乘法期间是否使用中间项代数简化来控制表达式放大,从而加快计算速度。默认设置为禁用。
- multiply_elementwise(other)[源代码]#
返回A和B的Hadamard积(elementwise积)
实例
>>> from sympy import Matrix >>> A = Matrix([[0, 1, 2], [3, 4, 5]]) >>> B = Matrix([[1, 10, 100], [100, 10, 1]]) >>> A.multiply_elementwise(B) Matrix([ [ 0, 10, 200], [300, 40, 5]])
- norm(ord=None)[源代码]#
Return the Norm of a Matrix or Vector.
In the simplest case this is the geometric size of the vector Other norms can be specified by the ord parameter
奥德
矩阵范数
向量范数
没有
弗罗宾纽斯范数
2-范数
“弗罗”
弗罗宾纽斯范数
不存在
因弗
最大行和
最大值(ABS(x))
-INF
——
最小(ABS(x))
1
最大列和
如下
-1
——
如下
2
2-norm(最大单曲价值)
如下
-2
最小奇异值
如下
其他
不存在
和(ABS(x)) ord) (1。/ORD)
实例
>>> from sympy import Matrix, Symbol, trigsimp, cos, sin, oo >>> x = Symbol('x', real=True) >>> v = Matrix([cos(x), sin(x)]) >>> trigsimp( v.norm() ) 1 >>> v.norm(10) (sin(x)**10 + cos(x)**10)**(1/10) >>> A = Matrix([[1, 1], [1, 1]]) >>> A.norm(1) # maximum sum of absolute values of A is 2 2 >>> A.norm(2) # Spectral norm (max of |Ax|/|x| under 2-vector-norm) 2 >>> A.norm(-2) # Inverse spectral norm (smallest singular value) 0 >>> A.norm() # Frobenius Norm 2 >>> A.norm(oo) # Infinity Norm 2 >>> Matrix([1, -2]).norm(oo) 2 >>> Matrix([-1, 2]).norm(-oo) 1
参见
- normalized(iszerofunc=<function _iszero>)[源代码]#
返回的规范化版本
self
.- 参数:
iszerofunc公司 :功能,可选
确定是否
self
是零向量。违约_iszero
测试每个元素是否精确为零。- 返回:
矩阵
规范化向量形式
self
. 它与单位向量的长度相同。但是,对于范数为0的向量,将返回零向量。- 加薪:
ShapeError
如果矩阵不在矩阵的形式中。
参见
- nullspace(simplify=False, iszerofunc=<function _iszero>)[源代码]#
返回跨越零空间的向量(矩阵对象)列表
M
实例
>>> from sympy import Matrix >>> M = Matrix(3, 3, [1, 3, 0, -2, -6, 0, 3, 9, 6]) >>> M Matrix([ [ 1, 3, 0], [-2, -6, 0], [ 3, 9, 6]]) >>> M.nullspace() [Matrix([ [-3], [ 1], [ 0]])]
参见
- classmethod ones(rows, cols=None, **kwargs)[源代码]#
返回1的矩阵。
- 参数:
rows : rows of the matrix
cols : cols of the matrix (if None, cols=rows)
克沃斯
cls:返回矩阵的类
- classmethod orthogonalize(*vecs, **kwargs)[源代码]#
对中提供的向量应用Gram-Schmidt正交化过程
vecs
.- 参数:
vecs
使向量正交
归一化 布尔
如果
True
,返回正交基。rankcheck公司 布尔
如果
True
,当遇到线性相关向量时,计算不会停止。如果
False
,它会升高ValueError
当找到任何零或线性相关向量时。- 返回:
列表
正交(或正交)基向量的列表。
实例
>>> from sympy import I, Matrix >>> v = [Matrix([1, I]), Matrix([1, -I])] >>> Matrix.orthogonalize(*v) [Matrix([ [1], [I]]), Matrix([ [ 1], [-I]])]
工具书类
- per()[源代码]#
Returns the permanent of a matrix. Unlike determinant, permanent is defined for both square and non-square matrices.
For an m x n matrix, with m less than or equal to n, it is given as the sum over the permutations s of size less than or equal to m on [1, 2, . . . n] of the product from i = 1 to m of M[i, s[i]]. Taking the transpose will not affect the value of the permanent.
In the case of a square matrix, this is the same as the permutation definition of the determinant, but it does not take the sign of the permutation into account. Computing the permanent with this definition is quite inefficient, so here the Ryser formula is used.
实例
>>> from sympy import Matrix >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M.per() 450 >>> M = Matrix([1, 5, 7]) >>> M.per() 13
工具书类
[R634]Prof. Frank Ben's notes: https://math.berkeley.edu/~bernd/ban275.pdf
[R635]Wikipedia article on Permanent: https://en.wikipedia.org/wiki/Permanent_%28mathematics%29
[R637]Permanent of a rectangular matrix : https://arxiv.org/pdf/0904.3251.pdf
- permute(perm, orientation='rows', direction='forward')[源代码]#
按给定的交换列表置换矩阵的行或列。
- 参数:
perm :排列、列表或列表
排列的一种表示。
如果是
Permutation
,它直接用于根据矩阵大小调整大小。如果它被指定为列表列表(例如。,
[[0, 1], [0, 2]]
),然后应用循环乘积形成置换。循环产品的应用方向如下所述。如果将其指定为列表,则该列表应表示置换的数组形式。(例如。,
[1, 2, 0]
)会形成交换功能 \(0 \mapsto 1, 1 \mapsto 2, 2\mapsto 0\) .方向 :“行”、“列”
用于控制是对行还是列进行置换的标志
方向 :“向前”、“向后”
一种标志,用于控制是先从列表的开头应用排列,还是先从列表的后面应用排列。
例如,如果置换规范是
[[0, 1], [0, 2]]
,如果标志设置为
'forward'
,循环将形成如下 \(0 \mapsto 2, 2 \mapsto 1, 1 \mapsto 0\) .如果标志设置为
'backward'
,循环将形成如下 \(0 \mapsto 1, 1 \mapsto 2, 2 \mapsto 0\) .如果论点
perm
不是列表的形式,此标志不起作用。
实例
>>> from sympy import eye >>> M = eye(3) >>> M.permute([[0, 1], [0, 2]], orientation='rows', direction='forward') Matrix([ [0, 0, 1], [1, 0, 0], [0, 1, 0]])
>>> from sympy import eye >>> M = eye(3) >>> M.permute([[0, 1], [0, 2]], orientation='rows', direction='backward') Matrix([ [0, 1, 0], [0, 0, 1], [1, 0, 0]])
笔记
如果是双射函数 \(\sigma : \mathbb{{N}}_0 \rightarrow \mathbb{{N}}_0\) 表示排列。
如果矩阵 \(A\) 是要置换的矩阵,表示为水平或垂直向量堆栈:
\[A=\]如果矩阵 \(B\) 作为结果,矩阵行的排列定义为:
\[{\B开始:}\]矩阵列的排列定义为:
\[{\B开始:}\]
- permute_cols(swaps, direction='forward')[源代码]#
Alias
self.permute(swaps, orientation='cols', direction=direction)
参见
- permute_rows(swaps, direction='forward')[源代码]#
Alias
self.permute(swaps, orientation='rows', direction=direction)
参见
- pinv(method='RD')[源代码]#
计算矩阵的Moore-Penrose伪逆。
Moore-Penrose伪逆存在且对任何矩阵都是唯一的。如果矩阵可逆,则伪逆与逆矩阵相同。
- 参数:
方法 :字符串,可选
指定反向计算的伪方法。
如果
'RD'
,将使用秩分解。如果
'ED'
,将使用对角化。
实例
通过秩分解计算伪逆:
>>> from sympy import Matrix >>> A = Matrix([[1, 2, 3], [4, 5, 6]]) >>> A.pinv() Matrix([ [-17/18, 4/9], [ -1/9, 1/9], [ 13/18, -2/9]])
通过对角化计算伪逆:
>>> B = A.pinv(method='ED') >>> B.simplify() >>> B Matrix([ [-17/18, 4/9], [ -1/9, 1/9], [ 13/18, -2/9]])
参见
工具书类
- pinv_solve(B, arbitrary_matrix=None)[源代码]#
解决
Ax = B
使用Moore-Penrose伪逆。可能有零解、一解或无穷解。如果存在一个解决方案,它将被返回。如果存在无穷多个解,则根据任意_矩阵的值返回一个。如果不存在解,则返回最小二乘解。
- 参数:
B :矩阵
要求解的方程的右侧。必须与矩阵A具有相同的行数。
arbitrary_matrix :矩阵
如果系统是欠定的(例如,A的列多于行),则可以用任意矩阵表示无限解。此参数可以设置为用于该目的的特定矩阵;如果是这样,它的形状必须与x相同,其中的行数与矩阵a的列数相同,列数与矩阵B的行数相同。如果保留为“无”,则适当的矩阵包含以下形式的伪符号:
wn_m
每行和每列的符号都是n。- 返回:
x :矩阵
矩阵将满足
Ax = B
. 矩阵A的行数和矩阵B的列数相等。
实例
>>> from sympy import Matrix >>> A = Matrix([[1, 2, 3], [4, 5, 6]]) >>> B = Matrix([7, 8]) >>> A.pinv_solve(B) Matrix([ [ _w0_0/6 - _w1_0/3 + _w2_0/6 - 55/18], [-_w0_0/3 + 2*_w1_0/3 - _w2_0/3 + 1/9], [ _w0_0/6 - _w1_0/3 + _w2_0/6 + 59/18]]) >>> A.pinv_solve(B, arbitrary_matrix=Matrix([0, 0, 0])) Matrix([ [-55/18], [ 1/9], [ 59/18]])
笔记
这可能返回精确解或最小二乘解。要确定哪个,请检查
A * A.pinv() * B == B
. 如果存在精确解,则为真;如果仅存在最小二乘解,则为假。请注意,该等式的左侧可能需要简化,以便与右侧进行正确比较。参见
sympy.matrices.dense.DenseMatrix.lower_triangular_solve
,sympy.matrices.dense.DenseMatrix.upper_triangular_solve
,gauss_jordan_solve
,cholesky_solve
,diagonal_solve
,LDLsolve
,LUsolve
,QRsolve
,pinv
工具书类
[R639]https://en.wikipedia.org/wiki/Moore-Penrose_pseudo inverse#获取线性系统的所有解
- pow(exp, method=None)[源代码]#
返回self**exp标量或符号。
- 参数:
方法 :倍增、乳状液、约旦、凯利
如果乘,则使用递归返回求幂。如果是jordan则使用jordan形式求幂。如果是cayley,则用cayley-Hamilton定理进行求幂。如果是ULIMP,则使用dotprodsimp递归进行求幂。这指定在朴素矩阵幂次期间是否使用中间项代数简化来控制表达式放大,从而加快计算速度。如果没有,那么它将试探性地决定使用哪种方法。
- print_nonzero(symb='X')[源代码]#
显示用于快速形状查找的非零项的位置。
实例
>>> from sympy import Matrix, eye >>> m = Matrix(2, 3, lambda i, j: i*3+j) >>> m Matrix([ [0, 1, 2], [3, 4, 5]]) >>> m.print_nonzero() [ XX] [XXX] >>> m = eye(4) >>> m.print_nonzero("x") [x ] [ x ] [ x ] [ x]
- project(v)[源代码]#
返回的投影
self
在包含v
.实例
>>> from sympy import Matrix, S, sqrt >>> V = Matrix([sqrt(3)/2, S.Half]) >>> x = Matrix([[1, 0]]) >>> V.project(x) Matrix([[sqrt(3)/2, 0]]) >>> V.project(-x) Matrix([[sqrt(3)/2, 0]])
- rank(iszerofunc=<function _iszero>, simplify=False)[源代码]#
返回矩阵的秩。
实例
>>> from sympy import Matrix >>> from sympy.abc import x >>> m = Matrix([[1, 2], [x, 1 - 1/x]]) >>> m.rank() 2 >>> n = Matrix(3, 3, range(1, 10)) >>> n.rank() 2
- rank_decomposition(iszerofunc=<function _iszero>, simplify=False)[源代码]#
返回一对矩阵 (\(C\) , \(F\) )具有匹配的等级使得 \(A = C F\) .
- 参数:
iszerofunc公司 :功能,可选
一种用于检测元素是否可以作为轴心的函数。
lambda x: x.is_zero
默认情况下使用。简化 :Bool或Function,可选
查找用于简化函数的元素。默认为SymPy的
simplify
使用。- 返回:
(C、F) :矩阵
\(C\) 和 \(F\) 全秩矩阵的秩与 \(A\) ,其产品提供 \(A\) .
有关其他数学细节,请参见注释。
实例
>>> from sympy import Matrix >>> A = Matrix([ ... [1, 3, 1, 4], ... [2, 7, 3, 9], ... [1, 5, 3, 1], ... [1, 2, 0, 8] ... ]) >>> C, F = A.rank_decomposition() >>> C Matrix([ [1, 3, 4], [2, 7, 9], [1, 5, 1], [1, 2, 8]]) >>> F Matrix([ [1, 0, -2, 0], [0, 1, 1, 0], [0, 0, 0, 1]]) >>> C * F == A True
笔记
获取 \(F\) ,一个 \(A\) ,相当于创建产品
\[恩-恩-恩{n-1}。。。E_1 A=F\]where \(E_n, E_{n-1}, \dots, E_1\) are the elimination matrices or permutation matrices equivalent to each row-reduction step.
消去矩阵同积的逆矩阵给出 \(C\) :
\[C = \left(E_n E_{n-1} \dots E_1\right)^{-1}\]然而,实际上并不需要计算逆:的列 \(C\) 是来自原始矩阵的列索引与 \(F\) .
工具书类
[R641]Piziak,R.;Odell,P.L.(1999年6月1日)。“矩阵的全秩因式分解”。数学杂志。72(3):193。内政部:10.2307/2690882
- refine(assumptions=True)[源代码]#
将细化应用于矩阵的每个元素。
实例
>>> from sympy import Symbol, Matrix, Abs, sqrt, Q >>> x = Symbol('x') >>> Matrix([[Abs(x)**2, sqrt(x**2)],[sqrt(x**2), Abs(x)**2]]) Matrix([ [ Abs(x)**2, sqrt(x**2)], [sqrt(x**2), Abs(x)**2]]) >>> _.refine(Q.real(x)) Matrix([ [ x**2, Abs(x)], [Abs(x), x**2]])
- replace(F, G, map=False, simultaneous=True, exact=None)[源代码]#
将矩阵项中的函数F替换为函数G。
实例
>>> from sympy import symbols, Function, Matrix >>> F, G = symbols('F, G', cls=Function) >>> M = Matrix(2, 2, lambda i, j: F(i+j)) ; M Matrix([ [F(0), F(1)], [F(1), F(2)]]) >>> N = M.replace(F,G) >>> N Matrix([ [G(0), G(1)], [G(1), G(2)]])
- reshape(rows, cols)[源代码]#
重塑矩阵。元素总数必须保持不变。
实例
>>> from sympy import Matrix >>> m = Matrix(2, 3, lambda i, j: 1) >>> m Matrix([ [1, 1, 1], [1, 1, 1]]) >>> m.reshape(1, 6) Matrix([[1, 1, 1, 1, 1, 1]]) >>> m.reshape(3, 2) Matrix([ [1, 1], [1, 1], [1, 1]])
- rmultiply(other, dotprodsimp=None)[源代码]#
与uu rmul_uUu()相同,但有可选的简化。
- 参数:
dotprodsimp公司 :bool,可选
指定在矩阵乘法期间是否使用中间项代数简化来控制表达式放大,从而加快计算速度。默认设置为禁用。
- rot90(k=1)[源代码]#
将矩阵旋转90度
- 参数:
k :内景
指定矩阵逆时针旋转90度(当矩阵逆时针旋转90度时)。
实例
>>> from sympy import Matrix, symbols >>> A = Matrix(2, 2, symbols('a:d')) >>> A Matrix([ [a, b], [c, d]])
顺时针旋转矩阵一次:
>>> A.rot90(1) Matrix([ [c, a], [d, b]])
逆时针旋转矩阵两次:
>>> A.rot90(-2) Matrix([ [d, c], [b, a]])
- row_insert(pos, other)[源代码]#
在给定的行位置插入一行或多行。
实例
>>> from sympy import zeros, ones >>> M = zeros(3) >>> V = ones(1, 3) >>> M.row_insert(1, V) Matrix([ [0, 0, 0], [1, 1, 1], [0, 0, 0], [0, 0, 0]])
参见
- row_join(other)[源代码]#
沿着self的最后一列和rhs的第一列连接两个矩阵
实例
>>> from sympy import zeros, ones >>> M = zeros(3) >>> V = ones(3, 1) >>> M.row_join(V) Matrix([ [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1]])
- rowspace(simplify=False)[源代码]#
返回跨行空间的向量列表
M
.实例
>>> from sympy import Matrix >>> M = Matrix(3, 3, [1, 3, 0, -2, -6, 0, 3, 9, 6]) >>> M Matrix([ [ 1, 3, 0], [-2, -6, 0], [ 3, 9, 6]]) >>> M.rowspace() [Matrix([[1, 3, 0]]), Matrix([[0, 0, 6]])]
- rref(iszerofunc=<function _iszero>, simplify=False, pivots=True, normalize_last=True)[源代码]#
返回矩阵的降行梯队形式和轴变量的指标。
- 参数:
iszerofunc公司 :功能
一种用于检测元素是否可以作为轴心的函数。
lambda x: x.is_zero
默认情况下使用。简化 :功能
查找用于简化函数的元素。默认为SymPy的
simplify
使用。枢轴 :正确或错误
如果
True
,则返回一个包含行缩减矩阵的元组和一个pivot列的元组。如果False
只返回行缩减矩阵。normalize_last :正确或错误
如果
True
,没有轴规范化为 \(1\) 直到每个轴上下的所有条目都归零。这意味着在最后一步之前,行缩减算法是无分数的。如果False
,在每个轴被规范化为 \(1\) 在行操作用于在轴的上方和下方归零之前。
实例
>>> from sympy import Matrix >>> from sympy.abc import x >>> m = Matrix([[1, 2], [x, 1 - 1/x]]) >>> m.rref() (Matrix([ [1, 0], [0, 1]]), (0, 1)) >>> rref_matrix, rref_pivots = m.rref() >>> rref_matrix Matrix([ [1, 0], [0, 1]]) >>> rref_pivots (0, 1)
iszerofunc
can correct rounding errors in matrices with float values. In the following example, callingrref()
leads to floating point errors, incorrectly row reducing the matrix.iszerofunc= lambda x: abs(x) < 1e-9
sets sufficiently small numbers to zero, avoiding this error.>>> m = Matrix([[0.9, -0.1, -0.2, 0], [-0.8, 0.9, -0.4, 0], [-0.1, -0.8, 0.6, 0]]) >>> m.rref() (Matrix([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]), (0, 1, 2)) >>> m.rref(iszerofunc=lambda x:abs(x)<1e-9) (Matrix([ [1, 0, -0.301369863013699, 0], [0, 1, -0.712328767123288, 0], [0, 0, 0, 0]]), (0, 1))
笔记
The default value of
normalize_last=True
can provide significant speedup to row reduction, especially on matrices with symbols. However, if you depend on the form row reduction algorithm leaves entries of the matrix, setnormalize_last=False
- rref_rhs(rhs)[源代码]#
Return reduced row-echelon form of matrix, matrix showing rhs after reduction steps.
rhs
must have the same number of rows asself
.实例
>>> from sympy import Matrix, symbols >>> r1, r2 = symbols('r1 r2') >>> Matrix([[1, 1], [2, 1]]).rref_rhs(Matrix([r1, r2])) (Matrix([ [1, 0], [0, 1]]), Matrix([ [ -r1 + r2], [2*r1 - r2]]))
- property shape#
矩阵的形状(维数)为2元组(行、列)。
实例
>>> from sympy import zeros >>> M = zeros(2, 3) >>> M.shape (2, 3) >>> M.rows 2 >>> M.cols 3
- simplify(**kwargs)[源代码]#
对矩阵的每个元素应用“简化”。
实例
>>> from sympy.abc import x, y >>> from sympy import SparseMatrix, sin, cos >>> SparseMatrix(1, 1, [x*sin(y)**2 + x*cos(y)**2]) Matrix([[x*sin(y)**2 + x*cos(y)**2]]) >>> _.simplify() Matrix([[x]])
- singular_value_decomposition()[源代码]#
Returns a Condensed Singular Value decomposition.
解释
A Singular Value decomposition is a decomposition in the form \(A = U \Sigma V^H\) where
\(U, V\) are column orthogonal matrix.
\(\Sigma\) is a diagonal matrix, where the main diagonal contains singular values of matrix A.
A column orthogonal matrix satisfies \(\mathbb{I} = U^H U\) while a full orthogonal matrix satisfies relation \(\mathbb{I} = U U^H = U^H U\) where \(\mathbb{I}\) is an identity matrix with matching dimensions.
For matrices which are not square or are rank-deficient, it is sufficient to return a column orthogonal matrix because augmenting them may introduce redundant computations. In condensed Singular Value Decomposition we only return column orthogonal matrices because of this reason
如果您想增加结果以返回完全正交分解,您应该使用以下步骤。
Augment the \(U, V\) matrices with columns that are orthogonal to every other columns and make it square.
Augment the \(\Sigma\) matrix with zero rows to make it have the same shape as the original matrix.
程序将在示例部分进行说明。
实例
we take a full rank matrix first:
>>> from sympy import Matrix >>> A = Matrix([[1, 2],[2,1]]) >>> U, S, V = A.singular_value_decomposition() >>> U Matrix([ [ sqrt(2)/2, sqrt(2)/2], [-sqrt(2)/2, sqrt(2)/2]]) >>> S Matrix([ [1, 0], [0, 3]]) >>> V Matrix([ [-sqrt(2)/2, sqrt(2)/2], [ sqrt(2)/2, sqrt(2)/2]])
If a matrix if square and full rank both U, V are orthogonal in both directions
>>> U * U.H Matrix([ [1, 0], [0, 1]]) >>> U.H * U Matrix([ [1, 0], [0, 1]])
>>> V * V.H Matrix([ [1, 0], [0, 1]]) >>> V.H * V Matrix([ [1, 0], [0, 1]]) >>> A == U * S * V.H True
>>> C = Matrix([ ... [1, 0, 0, 0, 2], ... [0, 0, 3, 0, 0], ... [0, 0, 0, 0, 0], ... [0, 2, 0, 0, 0], ... ]) >>> U, S, V = C.singular_value_decomposition()
>>> V.H * V Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> V * V.H Matrix([ [1/5, 0, 0, 0, 2/5], [ 0, 1, 0, 0, 0], [ 0, 0, 1, 0, 0], [ 0, 0, 0, 0, 0], [2/5, 0, 0, 0, 4/5]])
If you want to augment the results to be a full orthogonal decomposition, you should augment \(V\) with an another orthogonal column.
你可以附加一个任意的标准基,这个基与其他列线性无关,你可以运行Gram-Schmidt过程,使它们作为正交基进行扩充。
>>> V_aug = V.row_join(Matrix([[0,0,0,0,1], ... [0,0,0,1,0]]).H) >>> V_aug = V_aug.QRdecomposition()[0] >>> V_aug Matrix([ [0, sqrt(5)/5, 0, -2*sqrt(5)/5, 0], [1, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 1], [0, 2*sqrt(5)/5, 0, sqrt(5)/5, 0]]) >>> V_aug.H * V_aug Matrix([ [1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]) >>> V_aug * V_aug.H Matrix([ [1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]])
Similarly we augment U
>>> U_aug = U.row_join(Matrix([0,0,1,0])) >>> U_aug = U_aug.QRdecomposition()[0] >>> U_aug Matrix([ [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0]])
>>> U_aug.H * U_aug Matrix([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) >>> U_aug * U_aug.H Matrix([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
We add 2 zero columns and one row to S
>>> S_aug = S.col_join(Matrix([[0,0,0]])) >>> S_aug = S_aug.row_join(Matrix([[0,0,0,0], ... [0,0,0,0]]).H) >>> S_aug Matrix([ [2, 0, 0, 0, 0], [0, sqrt(5), 0, 0, 0], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0]])
>>> U_aug * S_aug * V_aug.H == C True
- singular_values()[源代码]#
计算矩阵的奇异值
实例
>>> from sympy import Matrix, Symbol >>> x = Symbol('x', real=True) >>> M = Matrix([[0, 1, 0], [0, x, 0], [-1, 0, 0]]) >>> M.singular_values() [sqrt(x**2 + 1), 1, 0]
- solve(rhs, method='GJ')[源代码]#
解存在唯一解的线性方程。
- 参数:
rhs :矩阵
表示线性方程右边的向量。
方法 :字符串,可选
如果设置为
'GJ'
或'GE'
,将使用Gauss-Jordan消去法,并在例程中实现gauss_jordan_solve
.如果设置为
'LU'
,LUsolve
将使用例程。如果设置为
'QR'
,QRsolve
将使用例程。如果设置为
'PINV'
,pinv_solve
将使用例程。If set to
'CRAMER'
,cramer_solve
routine will be used.它还支持适用于特殊线性系统的方法
正定系统:
如果设置为
'CH'
,cholesky_solve
将使用例程。如果设置为
'LDL'
,LDLsolve
将使用例程。要使用不同的方法并通过逆运算计算解,请使用.inv()docstring中定义的方法。
- 返回:
解决 :矩阵
表示解的向量。
- 加薪:
ValueError
如果没有唯一的解决方案,则
ValueError
将被提升。如果
M
不是正方形,aValueError
另外,我们还将提出解决这个系统的不同程序。
- solve_least_squares(rhs, method='CH')[源代码]#
返回数据的最小二乘拟合。
- 参数:
rhs :矩阵
表示线性方程右边的向量。
方法 :字符串或布尔值,可选
如果设置为
'CH'
,cholesky_solve
将使用例程。如果设置为
'LDL'
,LDLsolve
将使用例程。如果设置为
'QR'
,QRsolve
将使用例程。如果设置为
'PINV'
,pinv_solve
将使用例程。否则
M
将用于创建传递给solve
以及由定义的提示method
.- 返回:
解决 :矩阵
表示解的向量。
实例
>>> from sympy import Matrix, ones >>> A = Matrix([1, 2, 3]) >>> B = Matrix([2, 3, 4]) >>> S = Matrix(A.row_join(B)) >>> S Matrix([ [1, 2], [2, 3], [3, 4]])
如果每行S代表Ax+By的系数,x和y是 [2, 3] 则S*xy为:
>>> r = S*Matrix([2, 3]); r Matrix([ [ 8], [13], [18]])
但是让我们在中间值上加1,然后求出xy的最小二乘值:
>>> xy = S.solve_least_squares(Matrix([8, 14, 18])); xy Matrix([ [ 5/3], [10/3]])
误差由S*xy-r给出:
>>> S*xy - r Matrix([ [1/3], [1/3], [1/3]]) >>> _.norm().n(2) 0.58
如果使用不同的xy,则标准值将更高:
>>> xy += ones(2, 1)/10 >>> (S*xy - r).norm().n(2) 1.5
- strongly_connected_components()[源代码]#
Returns the list of strongly connected vertices of the graph when a square matrix is viewed as a weighted graph.
实例
>>> from sympy import Matrix >>> A = Matrix([ ... [44, 0, 0, 0, 43, 0, 45, 0, 0], ... [0, 66, 62, 61, 0, 68, 0, 60, 67], ... [0, 0, 22, 21, 0, 0, 0, 20, 0], ... [0, 0, 12, 11, 0, 0, 0, 10, 0], ... [34, 0, 0, 0, 33, 0, 35, 0, 0], ... [0, 86, 82, 81, 0, 88, 0, 80, 87], ... [54, 0, 0, 0, 53, 0, 55, 0, 0], ... [0, 0, 2, 1, 0, 0, 0, 0, 0], ... [0, 76, 72, 71, 0, 78, 0, 70, 77]]) >>> A.strongly_connected_components() [[0, 4, 6], [2, 3, 7], [1, 5, 8]]
- strongly_connected_components_decomposition(lower=True)[源代码]#
Decomposes a square matrix into block triangular form only using the permutations.
- 参数:
lower : bool
Makes \(B\) lower block triangular when
True
. Otherwise, makes \(B\) upper block triangular.- 返回:
P, B : PermutationMatrix, BlockMatrix
P is a permutation matrix for the similarity transform as in the explanation. And B is the block triangular matrix of the result of the permutation.
解释
The decomposition is in a form of \(A = P^{-1} B P\) where \(P\) is a permutation matrix and \(B\) is a block diagonal matrix.
实例
>>> from sympy import Matrix, pprint >>> A = Matrix([ ... [44, 0, 0, 0, 43, 0, 45, 0, 0], ... [0, 66, 62, 61, 0, 68, 0, 60, 67], ... [0, 0, 22, 21, 0, 0, 0, 20, 0], ... [0, 0, 12, 11, 0, 0, 0, 10, 0], ... [34, 0, 0, 0, 33, 0, 35, 0, 0], ... [0, 86, 82, 81, 0, 88, 0, 80, 87], ... [54, 0, 0, 0, 53, 0, 55, 0, 0], ... [0, 0, 2, 1, 0, 0, 0, 0, 0], ... [0, 76, 72, 71, 0, 78, 0, 70, 77]])
A lower block triangular decomposition:
>>> P, B = A.strongly_connected_components_decomposition() >>> pprint(P) PermutationMatrix((8)(1 4 3 2 6)(5 7)) >>> pprint(B) [[44 43 45] [0 0 0] [0 0 0] ] [[ ] [ ] [ ] ] [[34 33 35] [0 0 0] [0 0 0] ] [[ ] [ ] [ ] ] [[54 53 55] [0 0 0] [0 0 0] ] [ ] [ [0 0 0] [22 21 20] [0 0 0] ] [ [ ] [ ] [ ] ] [ [0 0 0] [12 11 10] [0 0 0] ] [ [ ] [ ] [ ] ] [ [0 0 0] [2 1 0 ] [0 0 0] ] [ ] [ [0 0 0] [62 61 60] [66 68 67]] [ [ ] [ ] [ ]] [ [0 0 0] [82 81 80] [86 88 87]] [ [ ] [ ] [ ]] [ [0 0 0] [72 71 70] [76 78 77]]
>>> P = P.as_explicit() >>> B = B.as_explicit() >>> P.T * B * P == A True
An upper block triangular decomposition:
>>> P, B = A.strongly_connected_components_decomposition(lower=False) >>> pprint(P) PermutationMatrix((0 1 5 7 4 3 2 8 6)) >>> pprint(B) [[66 68 67] [62 61 60] [0 0 0] ] [[ ] [ ] [ ] ] [[86 88 87] [82 81 80] [0 0 0] ] [[ ] [ ] [ ] ] [[76 78 77] [72 71 70] [0 0 0] ] [ ] [ [0 0 0] [22 21 20] [0 0 0] ] [ [ ] [ ] [ ] ] [ [0 0 0] [12 11 10] [0 0 0] ] [ [ ] [ ] [ ] ] [ [0 0 0] [2 1 0 ] [0 0 0] ] [ ] [ [0 0 0] [0 0 0] [44 43 45]] [ [ ] [ ] [ ]] [ [0 0 0] [0 0 0] [34 33 35]] [ [ ] [ ] [ ]] [ [0 0 0] [0 0 0] [54 53 55]]
>>> P = P.as_explicit() >>> B = B.as_explicit() >>> P.T * B * P == A True
- subs(*args, **kwargs)[源代码]#
返回一个新的矩阵,其中sub应用于每个条目。
实例
>>> from sympy.abc import x, y >>> from sympy import SparseMatrix, Matrix >>> SparseMatrix(1, 1, [x]) Matrix([[x]]) >>> _.subs(x, y) Matrix([[y]]) >>> Matrix(_).subs(y, x) Matrix([[x]])
- table(printer, rowstart='[', rowend=']', rowsep='\n', colsep=', ', align='right')[源代码]#
矩阵的字符串形式。
printer
在元素上使用的打印机(通常类似StrPrinter())rowstart
用于开始每行的字符串(默认为“[”)。rowend
用于结束每行的字符串(默认为“]”)。rowsep
用于分隔行的字符串(默认为换行)。colsep
用于分隔列的字符串(默认为“,”)。align
定义元素的对齐方式。必须是“left”、“right”或“center”之一。你也可以用“<”、“>”和“^”分别表示同一件事。这是用于矩阵的字符串打印机。
实例
>>> from sympy import Matrix, StrPrinter >>> M = Matrix([[1, 2], [-33, 4]]) >>> printer = StrPrinter() >>> M.table(printer) '[ 1, 2]\n[-33, 4]' >>> print(M.table(printer)) [ 1, 2] [-33, 4] >>> print(M.table(printer, rowsep=',\n')) [ 1, 2], [-33, 4] >>> print('[%s]' % M.table(printer, rowsep=',\n')) [[ 1, 2], [-33, 4]] >>> print(M.table(printer, colsep=' ')) [ 1 2] [-33 4] >>> print(M.table(printer, align='center')) [ 1 , 2] [-33, 4] >>> print(M.table(printer, rowstart='{', rowend='}')) { 1, 2} {-33, 4}
- todod()[源代码]#
Returns matrix as dict of dicts containing non-zero elements of the Matrix
实例
>>> from sympy import Matrix >>> A = Matrix([[0, 1],[0, 3]]) >>> A Matrix([ [0, 1], [0, 3]]) >>> A.todod() {0: {1: 1}, 1: {1: 3}}
- todok()[源代码]#
将矩阵作为键字典返回。
实例
>>> from sympy import Matrix >>> M = Matrix.eye(3) >>> M.todok() {(0, 0): 1, (1, 1): 1, (2, 2): 1}
- tolist()[源代码]#
以嵌套Python列表的形式返回矩阵。
实例
>>> from sympy import Matrix, ones >>> m = Matrix(3, 3, range(9)) >>> m Matrix([ [0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> m.tolist() [[0, 1, 2], [3, 4, 5], [6, 7, 8]] >>> ones(3, 0).tolist() [[], [], []]
如果没有行,则无法判断原始矩阵中有多少列:
>>> ones(0, 3).tolist() []
- trace()[源代码]#
返回方阵的轨迹,即对角线元素的和。
实例
>>> from sympy import Matrix >>> A = Matrix(2, 2, [1, 2, 3, 4]) >>> A.trace() 5
- transpose()[源代码]#
返回矩阵的转置。
实例
>>> from sympy import Matrix >>> A = Matrix(2, 2, [1, 2, 3, 4]) >>> A.transpose() Matrix([ [1, 3], [2, 4]])
>>> from sympy import Matrix, I >>> m=Matrix(((1, 2+I), (3, 4))) >>> m Matrix([ [1, 2 + I], [3, 4]]) >>> m.transpose() Matrix([ [ 1, 3], [2 + I, 4]]) >>> m.T == m.transpose() True
参见
conjugate
元素共轭
- upper_hessenberg_decomposition()[源代码]#
Converts a matrix into Hessenberg matrix H.
Returns 2 matrices H, P s.t. \(P H P^{T} = A\), where H is an upper hessenberg matrix and P is an orthogonal matrix
实例
>>> from sympy import Matrix >>> A = Matrix([ ... [1,2,3], ... [-3,5,6], ... [4,-8,9], ... ]) >>> H, P = A.upper_hessenberg_decomposition() >>> H Matrix([ [1, 6/5, 17/5], [5, 213/25, -134/25], [0, 216/25, 137/25]]) >>> P Matrix([ [1, 0, 0], [0, -3/5, 4/5], [0, 4/5, 3/5]]) >>> P * H * P.H == A True
工具书类
- upper_triangular(k=0)[源代码]#
Return the elements on and above the kth diagonal of a matrix. If k is not specified then simply returns upper-triangular portion of a matrix
实例
>>> from sympy import ones >>> A = ones(4) >>> A.upper_triangular() Matrix([ [1, 1, 1, 1], [0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 0, 1]])
>>> A.upper_triangular(2) Matrix([ [0, 0, 1, 1], [0, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0]])
>>> A.upper_triangular(-1) Matrix([ [1, 1, 1, 1], [1, 1, 1, 1], [0, 1, 1, 1], [0, 0, 1, 1]])
- vec()[源代码]#
返回通过堆叠列转换为单列矩阵的矩阵
实例
>>> from sympy import Matrix >>> m=Matrix([[1, 3], [2, 4]]) >>> m Matrix([ [1, 3], [2, 4]]) >>> m.vec() Matrix([ [1], [2], [3], [4]])
参见
- vech(diagonal=True, check_symmetry=True)[源代码]#
通过堆叠下三角中的元素,将矩阵重塑为列向量。
- 参数:
对角线的 :bool,可选
如果
True
,它包括对角线元素。check_symmetry :bool,可选
如果
True
,检查矩阵是否对称。
实例
>>> from sympy import Matrix >>> m=Matrix([[1, 2], [2, 3]]) >>> m Matrix([ [1, 2], [2, 3]]) >>> m.vech() Matrix([ [1], [2], [3]]) >>> m.vech(diagonal=False) Matrix([[2]])
笔记
这对对称矩阵和
vech
可以用向量形式表示对称矩阵,其大小小于vec
.参见
- vee()[源代码]#
Return a 3x1 vector from a skew-symmetric matrix representing the cross product, so that
self * b
is equivalent toself.vee().cross(b)
.实例
Calling
vee
creates a vector from a skew-symmetric Matrix:>>> from sympy import Matrix >>> A = Matrix([[0, -3, 2], [3, 0, -1], [-2, 1, 0]]) >>> a = A.vee() >>> a Matrix([ [1], [2], [3]])
Calculating the matrix product of the original matrix with a vector is equivalent to a cross product:
>>> b = Matrix([3, 2, 1]) >>> A * b Matrix([ [-4], [ 8], [-4]])
>>> a.cross(b) Matrix([ [-4], [ 8], [-4]])
vee
can also be used to retrieve angular velocity expressions. Defining a rotation matrix:>>> from sympy import rot_ccw_axis3, trigsimp >>> from sympy.physics.mechanics import dynamicsymbols >>> theta = dynamicsymbols('theta') >>> R = rot_ccw_axis3(theta) >>> R Matrix([ [cos(theta(t)), -sin(theta(t)), 0], [sin(theta(t)), cos(theta(t)), 0], [ 0, 0, 1]])
We can retrive the angular velocity:
>>> Omega = R.T * R.diff() >>> Omega = trigsimp(Omega) >>> Omega.vee() Matrix([ [ 0], [ 0], [Derivative(theta(t), t)]])
参见
- classmethod vstack(*args)[源代码]#
返回一个通过垂直连接参数(即通过重复应用col峎join)形成的矩阵。
实例
>>> from sympy import Matrix, eye >>> Matrix.vstack(eye(2), 2*eye(2)) Matrix([ [1, 0], [0, 1], [2, 0], [0, 2]])
- classmethod wilkinson(n, **kwargs)[源代码]#
Returns two square Wilkinson Matrix of size 2*n + 1 \(W_{2n + 1}^-, W_{2n + 1}^+ =\) Wilkinson(n)
实例
>>> from sympy import Matrix >>> wminus, wplus = Matrix.wilkinson(3) >>> wminus Matrix([ [-3, 1, 0, 0, 0, 0, 0], [ 1, -2, 1, 0, 0, 0, 0], [ 0, 1, -1, 1, 0, 0, 0], [ 0, 0, 1, 0, 1, 0, 0], [ 0, 0, 0, 1, 1, 1, 0], [ 0, 0, 0, 0, 1, 2, 1], [ 0, 0, 0, 0, 0, 1, 3]]) >>> wplus Matrix([ [3, 1, 0, 0, 0, 0, 0], [1, 2, 1, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 0, 0, 1, 1, 1, 0], [0, 0, 0, 0, 1, 2, 1], [0, 0, 0, 0, 0, 1, 3]])
工具书类
[R643]Wilkinson, The Algebraic Eigenvalue Problem, Claredon Press, Oxford, 1965, 662 pp.
Matrix Exceptions#
Matrix Functions#
- sympy.matrices.dense.matrix_multiply_elementwise(A, B)[源代码]#
返回A和B的Hadamard积(elementwise积)
>>> from sympy import Matrix, matrix_multiply_elementwise >>> A = Matrix([[0, 1, 2], [3, 4, 5]]) >>> B = Matrix([[1, 10, 100], [100, 10, 1]]) >>> matrix_multiply_elementwise(A, B) Matrix([ [ 0, 10, 200], [300, 40, 5]])
- sympy.matrices.dense.diag(*values, strict=True, unpack=False, **kwargs)[源代码]#
返回一个矩阵,其中提供的值放在对角线上。如果包含非方矩阵,它们将生成块对角矩阵。
实例
此版本的diag是矩阵.diag它的不同之处在于它将所有列表都视为矩阵——即使给出一个列表也是如此。如果不希望这样,请将 \(*\) 在列表或集合之前 \(unpack=True\) .
>>> from sympy import diag
>>> diag([1, 2, 3], unpack=True) # = diag(1,2,3) or diag(*[1,2,3]) Matrix([ [1, 0, 0], [0, 2, 0], [0, 0, 3]])
>>> diag([1, 2, 3]) # a column vector Matrix([ [1], [2], [3]])
- sympy.matrices.dense.jordan_cell(eigenval, n)[源代码]#
创建Jordan block:
实例
>>> from sympy import jordan_cell >>> from sympy.abc import x >>> jordan_cell(x, 4) Matrix([ [x, 1, 0, 0], [0, x, 1, 0], [0, 0, x, 1], [0, 0, 0, x]])
- sympy.matrices.dense.hessian(f, varlist, constraints=())[源代码]#
计算varlist中函数fwrt的Hessian矩阵,该函数可以作为序列或行/列向量给出。可以选择给出约束列表。
实例
>>> from sympy import Function, hessian, pprint >>> from sympy.abc import x, y >>> f = Function('f')(x, y) >>> g1 = Function('g')(x, y) >>> g2 = x**2 + 3*y >>> pprint(hessian(f, (x, y), [g1, g2])) [ d d ] [ 0 0 --(g(x, y)) --(g(x, y)) ] [ dx dy ] [ ] [ 0 0 2*x 3 ] [ ] [ 2 2 ] [d d d ] [--(g(x, y)) 2*x ---(f(x, y)) -----(f(x, y))] [dx 2 dy dx ] [ dx ] [ ] [ 2 2 ] [d d d ] [--(g(x, y)) 3 -----(f(x, y)) ---(f(x, y)) ] [dy dy dx 2 ] [ dy ]
工具书类
- sympy.matrices.dense.GramSchmidt(vlist, orthonormal=False)[源代码]#
对一组向量应用Gram-Schmidt过程。
- 参数:
列表 :矩阵列表
要正交化的向量。
正交 :Bool,可选
如果为true,则返回正交基。
- 返回:
列表 :矩阵列表
正交向量
笔记
这个程序主要是从
Matrix.orthogonalize
,但有一点不同,即当找到线性相关向量时,这总是会引发错误normalize
被命名为orthonormal
在这个函数中。工具书类
- sympy.matrices.dense.wronskian(functions, var, method='bareiss')[源代码]#
计算函数[]的Wronskian
| f1 f2 ... fn | | f1' f2' ... fn' | | . . . . | W(f1, ..., fn) = | . . . . | | . . . . | | (n) (n) (n) | | D (f1) D (f2) ... D (fn) |
- sympy.matrices.dense.casoratian(seqs, n, zero=True)[源代码]#
在给定‘k’阶线性差分算子L和齐次方程Ly=0的情况下,我们要计算L的核,它是一组‘k’序列:a(n),b(n)。。。z(n)。
L的解是线性无关的,如果它们的Casoratian表示为C(a,b,…,z),当n=0时不为零。
Casoratian由k x k行列式定义:
+ a(n) b(n) . . . z(n) + | a(n+1) b(n+1) . . . z(n+1) | | . . . . | | . . . . | | . . . . | + a(n+k-1) b(n+k-1) . . . z(n+k-1) +
用超olve()线性地证明了它是递归集的一个非常有用的基:
>>> from sympy import Symbol, casoratian, factorial >>> n = Symbol('n', integer=True)
指数和阶乘线性无关:
>>> casoratian([2**n, factorial(n)], n) != 0 True
- sympy.matrices.dense.randMatrix(r, c=None, min=0, max=99, seed=None, symmetric=False, percent=100, prng=None)[源代码]#
用维数创建随机矩阵
r
Xc
.如果c
如果省略,矩阵将为正方形。如果symmetric
矩阵必须是正方形。如果percent
如果小于100,则只有大约给定的元素百分比为非零。用于生成矩阵的伪随机数生成器按以下方式选择。
如果
prng
它将用作随机数生成器。它应该是random.Random
或者至少有randint
和shuffle
具有相同签名的方法。如果
prng
未提供,但seed
是供应的,然后是新的random.Random
给予seed
将被创建;否则,一个新的
random.Random
将使用默认种子。
实例
>>> from sympy import randMatrix >>> randMatrix(3) [25, 45, 27] [44, 54, 9] [23, 96, 46] >>> randMatrix(3, 2) [87, 29] [23, 37] [90, 26] >>> randMatrix(3, 3, 0, 2) [0, 2, 0] [2, 0, 1] [0, 0, 1] >>> randMatrix(3, symmetric=True) [85, 26, 29] [26, 71, 43] [29, 43, 57] >>> A = randMatrix(3, seed=1) >>> B = randMatrix(3, seed=2) >>> A == B False >>> A == randMatrix(3, seed=1) True >>> randMatrix(3, symmetric=True, percent=50) [77, 70, 0], [70, 0, 0], [ 0, 0, 88]
Rotation matrices#
- sympy.matrices.dense.rot_givens(i, j, theta, dim=3)[源代码]#
Returns a a Givens rotation matrix, a a rotation in the plane spanned by two coordinates axes.
- 参数:
i : int between
0
anddim - 1
Represents first axis
j : int between
0
anddim - 1
Represents second axis
dim : int bigger than 1
Number of dimentions. Defaults to 3.
解释
The Givens rotation corresponds to a generalization of rotation matrices to any number of dimensions, given by:
\[\begin{split}G(i, j, \theta) = \begin{bmatrix} 1 & \cdots & 0 & \cdots & 0 & \cdots & 0 \\ \vdots & \ddots & \vdots & & \vdots & & \vdots \\ 0 & \cdots & c & \cdots & -s & \cdots & 0 \\ \vdots & & \vdots & \ddots & \vdots & & \vdots \\ 0 & \cdots & s & \cdots & c & \cdots & 0 \\ \vdots & & \vdots & & \vdots & \ddots & \vdots \\ 0 & \cdots & 0 & \cdots & 0 & \cdots & 1 \end{bmatrix}\end{split}\]Where \(c = \cos(\theta)\) and \(s = \sin(\theta)\) appear at the intersections
i
th andj
th rows and columns.For fixed
i > j
, the non-zero elements of a Givens matrix are given by:\(g_{kk} = 1\) for \(k \ne i,\,j\)
\(g_{kk} = c\) for \(k = i,\,j\)
\(g_{ji} = -g_{ij} = -s\)
实例
>>> from sympy import pi, rot_givens
A counterclockwise rotation of pi/3 (60 degrees) around the third axis (z-axis):
>>> rot_givens(1, 0, pi/3) Matrix([ [ 1/2, -sqrt(3)/2, 0], [sqrt(3)/2, 1/2, 0], [ 0, 0, 1]])
如果我们旋转π/2(90度):
>>> rot_givens(1, 0, pi/2) Matrix([ [0, -1, 0], [1, 0, 0], [0, 0, 1]])
This can be generalized to any number of dimensions:
>>> rot_givens(1, 0, pi/2, dim=4) Matrix([ [0, -1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
参见
rot_axis1
Returns a rotation matrix for a rotation of theta (in radians) about the 1-axis (clockwise around the x axis)
rot_axis2
Returns a rotation matrix for a rotation of theta (in radians) about the 2-axis (clockwise around the y axis)
rot_axis3
Returns a rotation matrix for a rotation of theta (in radians) about the 3-axis (clockwise around the z axis)
rot_ccw_axis1
Returns a rotation matrix for a rotation of theta (in radians) about the 1-axis (counterclockwise around the x axis)
rot_ccw_axis2
Returns a rotation matrix for a rotation of theta (in radians) about the 2-axis (counterclockwise around the y axis)
rot_ccw_axis3
Returns a rotation matrix for a rotation of theta (in radians) about the 3-axis (counterclockwise around the z axis)
工具书类
- sympy.matrices.dense.rot_axis1(theta)[源代码]#
返回θ绕1轴旋转的旋转矩阵(以弧度为单位)。
解释
For a right-handed coordinate system, this corresponds to a clockwise rotation around the \(x\)-axis, given by:
\[\begin{split}R = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos(\theta) & \sin(\theta) \\ 0 & -\sin(\theta) & \cos(\theta) \end{bmatrix}\end{split}\]实例
>>> from sympy import pi, rot_axis1
旋转pi/3(60度):
>>> theta = pi/3 >>> rot_axis1(theta) Matrix([ [1, 0, 0], [0, 1/2, sqrt(3)/2], [0, -sqrt(3)/2, 1/2]])
如果我们旋转π/2(90度):
>>> rot_axis1(pi/2) Matrix([ [1, 0, 0], [0, 0, 1], [0, -1, 0]])
参见
rot_givens
Returns a Givens rotation matrix (generalized rotation for any number of dimensions)
rot_ccw_axis1
Returns a rotation matrix for a rotation of theta (in radians) about the 1-axis (counterclockwise around the x axis)
rot_axis2
Returns a rotation matrix for a rotation of theta (in radians) about the 2-axis (clockwise around the y axis)
rot_axis3
Returns a rotation matrix for a rotation of theta (in radians) about the 3-axis (clockwise around the z axis)
- sympy.matrices.dense.rot_axis2(theta)[源代码]#
返回θ绕2轴旋转的旋转矩阵(以弧度为单位)。
解释
For a right-handed coordinate system, this corresponds to a clockwise rotation around the \(y\)-axis, given by:
\[\begin{split}R = \begin{bmatrix} \cos(\theta) & 0 & -\sin(\theta) \\ 0 & 1 & 0 \\ \sin(\theta) & 0 & \cos(\theta) \end{bmatrix}\end{split}\]实例
>>> from sympy import pi, rot_axis2
旋转pi/3(60度):
>>> theta = pi/3 >>> rot_axis2(theta) Matrix([ [ 1/2, 0, -sqrt(3)/2], [ 0, 1, 0], [sqrt(3)/2, 0, 1/2]])
如果我们旋转π/2(90度):
>>> rot_axis2(pi/2) Matrix([ [0, 0, -1], [0, 1, 0], [1, 0, 0]])
参见
rot_givens
Returns a Givens rotation matrix (generalized rotation for any number of dimensions)
rot_ccw_axis2
Returns a rotation matrix for a rotation of theta (in radians) about the 2-axis (clockwise around the y axis)
rot_axis1
Returns a rotation matrix for a rotation of theta (in radians) about the 1-axis (counterclockwise around the x axis)
rot_axis3
Returns a rotation matrix for a rotation of theta (in radians) about the 3-axis (counterclockwise around the z axis)
- sympy.matrices.dense.rot_axis3(theta)[源代码]#
返回θ绕3轴旋转的旋转矩阵(以弧度为单位)。
解释
For a right-handed coordinate system, this corresponds to a clockwise rotation around the \(z\)-axis, given by:
\[\begin{split}R = \begin{bmatrix} \cos(\theta) & \sin(\theta) & 0 \\ -\sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \end{bmatrix}\end{split}\]实例
>>> from sympy import pi, rot_axis3
旋转pi/3(60度):
>>> theta = pi/3 >>> rot_axis3(theta) Matrix([ [ 1/2, sqrt(3)/2, 0], [-sqrt(3)/2, 1/2, 0], [ 0, 0, 1]])
如果我们旋转π/2(90度):
>>> rot_axis3(pi/2) Matrix([ [ 0, 1, 0], [-1, 0, 0], [ 0, 0, 1]])
参见
rot_givens
Returns a Givens rotation matrix (generalized rotation for any number of dimensions)
rot_ccw_axis3
Returns a rotation matrix for a rotation of theta (in radians) about the 3-axis (counterclockwise around the z axis)
rot_axis1
Returns a rotation matrix for a rotation of theta (in radians) about the 1-axis (clockwise around the x axis)
rot_axis2
Returns a rotation matrix for a rotation of theta (in radians) about the 2-axis (clockwise around the y axis)
- sympy.matrices.dense.rot_ccw_axis1(theta)[源代码]#
返回θ绕1轴旋转的旋转矩阵(以弧度为单位)。
解释
For a right-handed coordinate system, this corresponds to a counterclockwise rotation around the \(x\)-axis, given by:
\[\begin{split}R = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos(\theta) & -\sin(\theta) \\ 0 & \sin(\theta) & \cos(\theta) \end{bmatrix}\end{split}\]实例
>>> from sympy import pi, rot_ccw_axis1
旋转pi/3(60度):
>>> theta = pi/3 >>> rot_ccw_axis1(theta) Matrix([ [1, 0, 0], [0, 1/2, -sqrt(3)/2], [0, sqrt(3)/2, 1/2]])
如果我们旋转π/2(90度):
>>> rot_ccw_axis1(pi/2) Matrix([ [1, 0, 0], [0, 0, -1], [0, 1, 0]])
参见
rot_givens
Returns a Givens rotation matrix (generalized rotation for any number of dimensions)
rot_axis1
Returns a rotation matrix for a rotation of theta (in radians) about the 1-axis (clockwise around the x axis)
rot_ccw_axis2
Returns a rotation matrix for a rotation of theta (in radians) about the 2-axis (counterclockwise around the y axis)
rot_ccw_axis3
Returns a rotation matrix for a rotation of theta (in radians) about the 3-axis (counterclockwise around the z axis)
- sympy.matrices.dense.rot_ccw_axis2(theta)[源代码]#
返回θ绕2轴旋转的旋转矩阵(以弧度为单位)。
解释
For a right-handed coordinate system, this corresponds to a counterclockwise rotation around the \(y\)-axis, given by:
\[\begin{split}R = \begin{bmatrix} \cos(\theta) & 0 & \sin(\theta) \\ 0 & 1 & 0 \\ -\sin(\theta) & 0 & \cos(\theta) \end{bmatrix}\end{split}\]实例
>>> from sympy import pi, rot_ccw_axis2
旋转pi/3(60度):
>>> theta = pi/3 >>> rot_ccw_axis2(theta) Matrix([ [ 1/2, 0, sqrt(3)/2], [ 0, 1, 0], [-sqrt(3)/2, 0, 1/2]])
如果我们旋转π/2(90度):
>>> rot_ccw_axis2(pi/2) Matrix([ [ 0, 0, 1], [ 0, 1, 0], [-1, 0, 0]])
参见
rot_givens
Returns a Givens rotation matrix (generalized rotation for any number of dimensions)
rot_axis2
Returns a rotation matrix for a rotation of theta (in radians) about the 2-axis (clockwise around the y axis)
rot_ccw_axis1
Returns a rotation matrix for a rotation of theta (in radians) about the 1-axis (counterclockwise around the x axis)
rot_ccw_axis3
Returns a rotation matrix for a rotation of theta (in radians) about the 3-axis (counterclockwise around the z axis)
- sympy.matrices.dense.rot_ccw_axis3(theta)[源代码]#
返回θ绕3轴旋转的旋转矩阵(以弧度为单位)。
解释
For a right-handed coordinate system, this corresponds to a counterclockwise rotation around the \(z\)-axis, given by:
\[\begin{split}R = \begin{bmatrix} \cos(\theta) & -\sin(\theta) & 0 \\ \sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \end{bmatrix}\end{split}\]实例
>>> from sympy import pi, rot_ccw_axis3
旋转pi/3(60度):
>>> theta = pi/3 >>> rot_ccw_axis3(theta) Matrix([ [ 1/2, -sqrt(3)/2, 0], [sqrt(3)/2, 1/2, 0], [ 0, 0, 1]])
如果我们旋转π/2(90度):
>>> rot_ccw_axis3(pi/2) Matrix([ [0, -1, 0], [1, 0, 0], [0, 0, 1]])
参见
rot_givens
Returns a Givens rotation matrix (generalized rotation for any number of dimensions)
rot_axis3
Returns a rotation matrix for a rotation of theta (in radians) about the 3-axis (clockwise around the z axis)
rot_ccw_axis1
Returns a rotation matrix for a rotation of theta (in radians) about the 1-axis (counterclockwise around the x axis)
rot_ccw_axis2
Returns a rotation matrix for a rotation of theta (in radians) about the 2-axis (counterclockwise around the y axis)
Numpy Utility Functions#
- sympy.matrices.dense.list2numpy(l, dtype=<class 'object'>)[源代码]#
Converts Python list of SymPy expressions to a NumPy array.
参见
- sympy.matrices.dense.symarray(prefix, shape, **kwargs)[源代码]#
创建大量的符号(作为对象数组)。
创建的符号将命名为
prefix_i1_i2_
... 因此,如果希望符号对于不同的输出数组是唯一的,那么应该提供一个非空前缀,因为具有相同名称的SymPy符号是同一个对象。- 参数:
前缀 :字符串
在每个符号的名称前加上前缀。
形状 :int或tuple
创建的数组的形状。如果是int,则数组是一维的;对于多个维度,该形状必须是元组。
**kwargs :dict命令
传递给符号的关键字参数
实例
这些医生都需要纽比。
>>> from sympy import symarray >>> symarray('', 3) [_0 _1 _2]
如果希望多个符号数组包含不同的符号,则 must 提供唯一的前缀:
>>> a = symarray('', 3) >>> b = symarray('', 3) >>> a[0] == b[0] True >>> a = symarray('a', 3) >>> b = symarray('b', 3) >>> a[0] == b[0] False
使用前缀创建符号数组:
>>> symarray('a', 3) [a_0 a_1 a_2]
对于多个维度,形状必须以元组形式给出:
>>> symarray('a', (2, 3)) [[a_0_0 a_0_1 a_0_2] [a_1_0 a_1_1 a_1_2]] >>> symarray('a', (2, 3, 2)) [[[a_0_0_0 a_0_0_1] [a_0_1_0 a_0_1_1] [a_0_2_0 a_0_2_1]] [[a_1_0_0 a_1_0_1] [a_1_1_0 a_1_1_1] [a_1_2_0 a_1_2_1]]]
对于设置基础符号的假设:
>>> [s.is_real for s in symarray('a', 2, real=True)] [True, True]