公共矩阵

MatrixCommon类引用

class sympy.matrices.common.MatrixCommon[源代码]

所有常见的矩阵运算,包括基本算术、成形和特殊矩阵,如 \(zeros\)\(eye\) .

property C

元素共轭

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]])

参见

conjugate

元素共轭

sympy.matrices.matrices.MatrixBase.D

狄拉克共轭

property T

矩阵换位

__abs__()[源代码]

返回一个新的矩阵,其中包含按条目排序的绝对值。

__add__(other)[源代码]

返回self+other,如果形状不匹配,则引发ShapeError。

__getitem__(key)[源代码]

的实现 __getitem__ 应该接受int,在这种情况下,矩阵被索引为一个平面列表,元组(i,j),在这种情况下,返回(i,j)项,切片或混合元组(a,b),其中a和b是切片和整数的任何组合。

__len__()[源代码]

矩阵中的条目总数。

__mul__(other)[源代码]

返回self*other,其中other是标量或兼容维度的矩阵。

实例

>>> from sympy.matrices 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.
>>>
__pow__(exp)[源代码]

返回self**exp标量或符号。

__weakref__

对象的弱引用列表(如果已定义)

adjoint()[源代码]

共轭转置或厄米共轭。

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]])
as_real_imag(deep=True, **hints)[源代码]

返回一个包含矩阵(实、虚)部分的元组。

atoms(*types)[源代码]

返回构成当前对象的原子。

实例

>>> from sympy.abc import x, y
>>> from sympy.matrices import Matrix
>>> Matrix([[x]])
Matrix([[x]])
>>> _.atoms()
{x}
>>> Matrix([[x, y], [y, x]])
Matrix([
[x, y],
[y, x]])
>>> _.atoms()
{x, y}
col(j)[源代码]

基本列选择器。

实例

>>> from sympy import eye
>>> eye(2).col(0)
Matrix([
[1],
[0]])
col_del(col)[源代码]

删除指定的列。

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, row_insert

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]])

参见

col, row_join

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]])
conjugate()[源代码]

返回by元素的共轭。

实例

>>> from sympy.matrices import SparseMatrix
>>> from sympy import 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.matrices.MatrixBase.D

狄拉克共轭

classmethod diag(*args, strict=False, unpack=True, rows=None, cols=None, **kwargs)[源代码]

返回具有指定对角线的矩阵。如果传递矩阵,则创建块对角矩阵(即矩阵的“直和”)。

克沃斯

结果矩阵的行;如果

不给。

科尔斯结果矩阵的列;如果

不给。

cls:生成矩阵的类

unpack:bool,当为True(默认值)时,解压单个序列,而不是将其解释为矩阵。

strict:bool,当False(默认)时,允许矩阵具有可变长度的行。

实例

>>> from sympy.matrices 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.matrices 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

参见

diag

evalf(n=15, subs=None, maxn=100, chop=False, strict=False, quad=None, verbose=False)[源代码]

对self的每个元素应用evalf()。

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.matrices import Matrix
>>> Matrix(1, 1, [x*(x+1)])
Matrix([[x*(x + 1)]])
>>> _.expand()
Matrix([[x**2 + x]])
extract(rowsList, colsList)[源代码]

通过指定行和列的列表返回子矩阵。可以给出负指数。所有索引必须在-n<=i<n的范围内,其中n是行数或列数。

实例

>>> 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:矩阵的行cols:矩阵的cols(如果没有,cols=rows)

克沃斯

cls:返回矩阵的类

property free_symbols

返回矩阵中的自由符号。

实例

>>> from sympy.abc import x
>>> from sympy.matrices import Matrix
>>> Matrix([[x], [1]]).free_symbols
{x}
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
classmethod hstack(*args)[源代码]

返回通过水平连接参数(即通过重复应用row_join)形成的矩阵。

实例

>>> from sympy.matrices import Matrix, eye
>>> Matrix.hstack(eye(2), 2*eye(2))
Matrix([
[1, 0, 2, 0],
[0, 1, 0, 2]])
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])

矩阵元素的简化是默认的,因此即使两个应该相等和相反的元素不能通过相等测试,矩阵仍然被报告为反对称:

>>> m[0, 1] == -m[1, 0]
False
>>> m.is_anti_symmetric()
True

如果在矩阵已经被简化的情况下使用“simplify=False”,这将加快速度。在这里,我们可以看到,如果不进行简化,矩阵就不会是反对称的:

>>> 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
property is_hermitian

检查矩阵是否为Hermitian。

在厄米矩阵元素i中,j是元素j,i的复共轭。

实例

>>> from sympy.matrices 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_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.matrices 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_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.matrices 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.matrices 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.matrices 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.matrices 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
classmethod jordan_block(size=None, eigenvalue=None, *, band='upper', **kwargs)[源代码]

返回一个约旦方块

参数

size :整数,可选

指定约旦块矩阵的形状。

特征值 :数字或符号

指定矩阵主对角线的值。

注解

关键词 eigenval 也被指定为此关键字的别名,但不建议使用。

在以后的版本中,我们可能不赞成使用别名。

band :“上”或“下”,可选

指定要放置的非对角线的位置 \(1\) 开了。

cls :矩阵,可选

指定输出窗体的矩阵类。

如果未指定,则将返回执行方法的类类型。

行,酷 :整数,可选

指定Jordan块矩阵的形状。有关这些关键工作原理的详细信息,请参阅Notes部分。

注解

此功能将来将被弃用。

返回

矩阵

乔丹块矩阵。

加薪

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]])

笔记

注解

此功能将来将被弃用。

关键字参数 sizerowscols 与约旦块尺寸规格有关。

如果要创建方形Jordan块,请指定三个参数之一。

如果要创建矩形Jordan块,请指定 rowscols 个别地。

给出的参数

矩阵形状

大小

科尔斯

科尔斯

大小

任何

大小

大小

没有

没有

ValueError

没有

没有

科尔斯

科尔斯

科尔斯

科尔斯

科尔斯

工具书类

R517

https://en.wikipedia.org/wiki/Jordan_matrix

multiply(other, dotprodsimp=None)[源代码]

与uuUmul_Uu9()相同,但具有可选的简化功能。

参数

dotprodsimp公司 :bool,可选

指定在矩阵乘法期间是否使用中间项代数简化来控制表达式放大,从而加快计算速度。默认设置为禁用。

multiply_elementwise(other)[源代码]

返回A和B的Hadamard积(elementwise积)

实例

>>> from sympy.matrices 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]])
n(*args, **kwargs)[源代码]

对self的每个元素应用evalf()。

classmethod ones(rows, cols=None, **kwargs)[源代码]

返回1的矩阵。

阿格斯

rows:矩阵的行cols:矩阵的cols(如果没有,cols=rows)

克沃斯

cls:返回矩阵的类

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.matrices 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.matrices 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

permute_rows(swaps, direction='forward')[源代码]

Alias self.permute(swaps, orientation='rows', direction=direction)

参见

permute

pow(exp, method=None)[源代码]

返回self**exp标量或符号。

参数

方法 :倍增、乳状液、约旦、凯利

如果乘,则使用递归返回求幂。如果是jordan则使用jordan形式求幂。如果是cayley,则用cayley-Hamilton定理进行求幂。如果是ULIMP,则使用dotprodsimp递归进行求幂。这指定在朴素矩阵幂次期间是否使用中间项代数简化来控制表达式放大,从而加快计算速度。如果没有,那么它将试探性地决定使用哪种方法。

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(i)[源代码]

基本行选择器。

实例

>>> from sympy import eye
>>> eye(2).row(0)
Matrix([[1, 0]])
row_del(row)[源代码]

删除指定的行。

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, col_insert

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]])

参见

row, col_join

property shape

矩阵的形状(维数)为2元组(行、列)。

实例

>>> from sympy.matrices 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 sin, cos
>>> from sympy.matrices import SparseMatrix
>>> SparseMatrix(1, 1, [x*sin(y)**2 + x*cos(y)**2])
Matrix([[x*sin(y)**2 + x*cos(y)**2]])
>>> _.simplify()
Matrix([[x]])
subs(*args, **kwargs)[源代码]

返回一个新的矩阵,其中sub应用于每个条目。

实例

>>> from sympy.abc import x, y
>>> from sympy.matrices import SparseMatrix, Matrix
>>> SparseMatrix(1, 1, [x])
Matrix([[x]])
>>> _.subs(x, y)
Matrix([[y]])
>>> Matrix(_).subs(y, x)
Matrix([[x]])
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

元素共轭

values()[源代码]

返回self的非零值。

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

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 .

参见

vec

classmethod vstack(*args)[源代码]

返回一个通过垂直连接参数(即通过重复应用col峎join)形成的矩阵。

实例

>>> from sympy.matrices import Matrix, eye
>>> Matrix.vstack(eye(2), 2*eye(2))
Matrix([
[1, 0],
[0, 1],
[2, 0],
[0, 2]])
xreplace(rule)[源代码]

返回一个新矩阵,并对每个条目应用xreplace。

实例

>>> from sympy.abc import x, y
>>> from sympy.matrices import SparseMatrix, Matrix
>>> SparseMatrix(1, 1, [x])
Matrix([[x]])
>>> _.xreplace({x: y})
Matrix([[y]])
>>> Matrix(_).xreplace({y: x})
Matrix([[x]])
classmethod zeros(rows, cols=None, **kwargs)[源代码]

返回零矩阵。

阿格斯

rows:矩阵的行cols:矩阵的cols(如果没有,cols=rows)

克沃斯

cls:返回矩阵的类