SymPy 矩阵
在数学中,矩阵是数字、符号或表达式的二维数组。矩阵操作理论涉及对矩阵对象执行算术运算,但须遵守某些规则。
线性变换是矩阵的重要应用之一。许多科学领域,特别是与物理相关的领域,都使用矩阵相关的应用程序。
SymPy 包具有处理矩阵处理的矩阵模块。它包括 Matrix 类,其对象表示一个矩阵。
注意:如果要单独执行本章所有的代码片段,需要导入矩阵模块,如下图:
>>> from sympy.matrices import Matrix
例子
>>> from sympy.matrices import Matrix >>> m=Matrix([[1,2,3],[2,3,1]]) >>> m $\displaystyle \left[\begin{matrix}1 & 2 & 3\\2 & 3 & 1\end{matrix}\right]$
在 python shell 中执行上述命令,会产生如下输出:
[1 2 3 2 3 1]
矩阵是从适当大小的列表对象创建的。你还可以通过将列表项分布在指定的行数和列数中来获得矩阵。
>>> M=Matrix(2,3,[10,40,30,2,6,9]) >>> M $\displaystyle \left[\begin{matrix}10 & 40 & 30\\2 & 6 & 9\end{matrix}\right]$
在 python shell 中执行上述命令,会产生如下输出:
[10 40 30 2 6 9]
矩阵是一个可变对象。矩阵模块还提供了 ImmutableMatrix 类来获取不可变矩阵。
基本操作
The shape Matrix 对象的属性返回其大小。
>>> M.shape
上述代码的输出如下:
(2,3)
row() 和 col() 方法分别返回指定数量的行或列。
>>> M.row(0) $\displaystyle \left[\begin{matrix}10 & 40 & 30\end{matrix}\right]$
上述代码的输出如下:
[10 40 30]
>>> M.col(1) $\displaystyle \left[\begin{matrix}40\\6\end{matrix}\right]$
上述代码的输出如下:
[40 6]
使用 Python 的切片运算符来获取属于行或列的一个或多个项目。
>>> M.row(1)[1:3] [6, 9]
Matrix 类有 row_del() 和 col_del() 方法,可以从给定的矩阵中删除指定的行/列:
>>> M=Matrix(2,3,[10,40,30,2,6,9]) >>> M.col_del(1) >>> M
在 python shell 中执行上述命令,会产生如下输出:
Matrix([[10, 30],[ 2, 9]])
你可以使用以下命令将样式应用于输出:
$\displaystyle \left[\begin{matrix}10 & 30\\2 & 9\end{matrix}\right]$
执行上述代码段后,你会得到以下输出:
[10 30 2 9]
>>> M.row_del(0) >>> M $\displaystyle \left[\begin{matrix}2 & 9\end{matrix}\right]$
执行上述代码段后,你会得到以下输出:
[2 9]
同样,row_insert() 和 col_insert() 方法在指定的行或列索引处添加行或列
>>> M1=Matrix([[10,30]]) >>> M=M.row_insert(0,M1) >>> M $\displaystyle \left[\begin{matrix}10 & 30\\2 & 9\end{matrix}\right]$
执行上述代码段后,你会得到以下输出:
[10 40 30 2 9]
>>> M2=Matrix([40,6]) >>> M=M.col_insert(1,M2) >>> M $\displaystyle \left[\begin{matrix}10 & 40 & 30\\2 & 6 & 9\end{matrix}\right]$
执行上述代码段后,你会得到以下输出:
[10 40 30 6 9]
算术运算
通常的运算符 +、- 和 * 被定义用于执行加法、减法和乘法。
>>> M1=Matrix([[1,2,3],[3,2,1]]) >>> M2=Matrix([[4,5,6],[6,5,4]]) >>> M1+M2 $\displaystyle \left[\begin{matrix}5 & 7 & 9\\9 & 7 & 5\end{matrix}\right]$
执行上述代码段后,你会得到以下输出:
[5 7 9 9 7 5]
>>> M1-M2 $\displaystyle \left[\begin{matrix}-3 & -3 & -3\\-3 & -3 & -3\end{matrix}\right]$
执行上述代码段后,你会得到以下输出:
[- 3 -3 -3 -3 -3 -3]
矩阵乘法仅在以下情况下可行 - 第一个矩阵的列数必须等于第二个矩阵的行数。 - 结果将具有与第一个矩阵相同的行数,以及与第二个矩阵相同的列数。
>>> M1=Matrix([[1,2,3],[3,2,1]]) >>> M2=Matrix([[4,5],[6,6],[5,4]]) >>> M1*M2 $\displaystyle \left[\begin{matrix}31 & 29\\29 & 31\end{matrix}\right]$
上述代码的输出如下:
[31 29 29 31]
>>> M1.T $\displaystyle \left[\begin{matrix}1 & 3\\2 & 2\\3 & 1\end{matrix}\right]$
执行代码后得到如下输出:
[1 3 2 2 3 1]
要计算矩阵的行列式,请使用 det() 方法。行列式是一个标量值,可以从方阵的元素中计算出来。0
>>> M=Matrix(3,3,[10,20,30,5,8,12,9,6,15]) >>> M $\displaystyle \left[\begin{matrix}10 & 20 & 30\\5 & 8 & 12\\9 & 6 & 15\end{matrix}\right]$
上述代码的输出如下:
[10 20 30 5 8 12 9 6 15]
>>> M.det()
上述代码的输出如下:
-120
矩阵构造函数
SymPy 提供了许多特殊类型的矩阵类。例如,单位矩阵、全零和一矩阵等。这些类分别命名为眼睛、零和一。单位矩阵是一个方阵,对角线上的元素设置为 1,其余元素为 0。
例子
from sympy.matrices import eye eye(3)
Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
$\displaystyle \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$
上述代码的输出如下:
[1 0 0 0 1 0 0 0 1]
在 diag 矩阵中,对角线上的元素根据提供的参数进行初始化。
>>> from sympy.matrices import diag >>> diag(1,2,3) $\displaystyle \left[\begin{matrix}1 & 0 & 0\\0 & 2 & 0\\0 & 0 & 3\end{matrix}\right]$
上述代码的输出如下:
[1 0 0 0 2 0 0 0 3]
zeros 矩阵中的所有元素都初始化为 0。
>>> from sympy.matrices import zeros >>> zeros(2,3) $\displaystyle \left[\begin{matrix}0 & 0 & 0\\0 & 0 & 0\end{matrix}\right]$
上述代码的输出如下:
[0 0 0 0 0 0]
同样,ones 是所有元素都设置为 1 的矩阵。
>>> from sympy.matrices import ones >>> ones(2,3) $\displaystyle \left[\begin{matrix}1 & 1 & 1\\1 & 1 & 1\end{matrix}\right]$
上述代码的输出如下:
[1 1 1 1 1 1]