Sympy 简明教程
SymPy - Matrices
在数学中,矩阵是二维数字、符号或表达式的数组。矩阵操作的理论涉及对矩阵目标执行算术操作,但要遵循特定规则。
In Mathematics, a matrix is a two dimensional array of numbers, symbols or expressions. Theory of matrix manipulation deals with performing arithmetic operations on matrix objects, subject to certain rules.
线性变换是矩阵的重要应用之一。许多科学领域,特别是与物理学相关的领域,都使用与矩阵相关的应用程序。
Linear transformation is one of the important applications of matrices. Many scientific fields, specially related to Physics use matrix related applications.
SymPy 包具有处理矩阵的 matrices 模块。他包含 Matrix 类,其对象表示一个矩阵。
SymPy package has matrices module that deals with matrix handling. It includes Matrix class whose object represents a matrix.
Note: If you want to execute all the snippets in this chapter individually, you need to import the matrix module as shown below −
Note: If you want to execute all the snippets in this chapter individually, you need to import the matrix module as shown below −
>>> from sympy.matrices import Matrix
Example
>>> 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 中执行上面的命令后,将生成以下输出 -
On executing the above command in python shell, following output will be generated −
[1 2 3 2 3 1]
[1 2 3 2 3 1]
可以使用大小合适的 List 对象创建矩阵。也可以通过按指定行数和列数分配列表项来获取矩阵。
Matrix is created from appropriately sized List objects. You can also obtain a matrix by distributing list items in specified number of rows and columns.
>>> 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 中执行上面的命令后,将生成以下输出 -
On executing the above command in python shell, following output will be generated −
[10 40 30 2 6 9]
[10 40 30 2 6 9]
矩阵是一个可变对象。matrices 模块还提供 ImmutableMatrix 类来获取不可变矩阵。
Matrix is a mutable object. The matrices module also provides ImmutableMatrix class for obtaining immutable matrix.
Basic manipulation
Matrix 对象的 shape 属性返回其大小。
The shape property of Matrix object returns its size.
>>> M.shape
以上代码的输出如下 −
The output for the above code is as follows −
(2,3)
(2,3)
row() 和 col() 方法分别返回指定数量的行或列。
The row() and col() method respectively returns row or column of specified number.
>>> M.row(0)
$\displaystyle \left[\begin{matrix}10 & 40 & 30\end{matrix}\right]$
以上代码的输出如下 −
The output for the above code is as follows −
[10 40 30]
[10 40 30]
>>> M.col(1)
$\displaystyle \left[\begin{matrix}40\\6\end{matrix}\right]$
以上代码的输出如下 −
The output for the above code is as follows −
[40 6]
[40 6]
可以使用 Python 的切片操作符获取属于行或列的一个或多个项。
Use Python’s slice operator to fetch one or more items belonging to row or column.
>>> M.row(1)[1:3]
[6, 9]
Matrix 类具有 row_del() 和 col_del() 方法,用于从给定矩阵中删除指定行/列,如下所示:
Matrix class has row_del() and col_del() methods that deletes specified row/column from given matrix −
>>> M=Matrix(2,3,[10,40,30,2,6,9])
>>> M.col_del(1)
>>> M
在 Python shell 中执行上面的命令后,将生成以下输出 -
On executing the above command in python shell, following output will be generated −
Matrix([[10, 30],[ 2, 9]])
可以使用以下命令给输出应用样式:
You can apply style to the output using the following command −
$\displaystyle \left[\begin{matrix}10 & 30\\2 & 9\end{matrix}\right]$
执行以上代码片段后,可以获得以下输出:
You get the following output after executing the above code snippet −
[10 30 2 9]
[10 30 2 9]
>>> M.row_del(0)
>>> M
$\displaystyle \left[\begin{matrix}2 & 9\end{matrix}\right]$
执行以上代码片段后,可以获得以下输出:
You get the following output after executing the above code snippet −
[2 9]
[2 9]
同样,row_insert() 和 col_insert() 方法在指定行或列索引处添加行或列
Similarly, row_insert() and col_insert() methods add rows or columns at specified row or column index
>>> M1=Matrix([[10,30]])
>>> M=M.row_insert(0,M1)
>>> M
$\displaystyle \left[\begin{matrix}10 & 30\\2 & 9\end{matrix}\right]$
执行以上代码片段后,可以获得以下输出:
You get the following output after executing the above code snippet −
[10 40 30 2 9]
[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]$
执行以上代码片段后,可以获得以下输出:
You get the following output after executing the above code snippet −
[10 40 30 6 9]
[10 40 30 6 9]
Arithmetic Operations
通常的操作符 + 、 - 和 * 被定义为执行加法、减法和乘法运算。
Usual operators +, - and * are defined for performing addition, subtraction and multiplication.
>>> 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]$
执行以上代码片段后,可以获得以下输出:
You get the following output after executing the above code snippet −
[5 7 9 9 7 5]
[5 7 9 9 7 5]
>>> M1-M2
$\displaystyle \left[\begin{matrix}-3 & -3 & -3\\-3 & -3 & -3\end{matrix}\right]$
执行以上代码片段后,可以获得以下输出:
You get the following output after executing the above code snippet −
[- 3 -3 -3 -3 -3 -3]
[- 3 -3 -3 -3 -3 -3]
矩阵乘法只有在以下情况下才可能:- 第一个矩阵的列数必须等于第二个矩阵的行数。- 结果的行数将和第一个矩阵相同,列数将和第二个矩阵相同。
Matrix multiplication is possible only if - The number of columns of the 1st matrix must equal the number of rows of the 2nd matrix. - And the result will have the same number of rows as the 1st matrix, and the same number of columns as the 2nd matrix.
>>> 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]$
以上代码的输出如下 −
The output for the above code is as follows −
[31 29 29 31]
[31 29 29 31]
>>> M1.T
$\displaystyle \left[\begin{matrix}1 & 3\\2 & 2\\3 & 1\end{matrix}\right]$
在执行代码 − 之后,将得到以下输出:
The following output is obtained after executing the code −
[1 3 2 2 3 1]
[1 3 2 2 3 1]
要计算矩阵的行列式,请使用 det() 方法。行列式是一个标量值,可以从方阵的元素计算得出。0
To calculate a determinant of matrix, use det() method. A determinant is a scalar value that can be computed from the elements of a square matrix.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]$
以上代码的输出如下 −
The output for the above code is as follows −
[10 20 30 5 8 12 9 6 15]
[10 20 30 5 8 12 9 6 15]
>>> M.det()
以上代码的输出如下 −
The output for the above code is as follows −
-120
-120
Matrix Constructors
SymPy 提供了许多特殊类型的矩阵类。例如,单位矩阵、由所有零和一组成的矩阵等。这些类分别命名为 eye、zeros 和 ones。单位矩阵是一个方阵,对角线上的元素设置为 1,其余元素均为 0。
SymPy provides many special type of matrix classes. For example, Identity matrix, matrix of all zeroes and ones, etc. These classes are named as eye, zeros and ones respectively. Identity matrix is a square matrix with elements falling on diagonal are set to 1, rest of the elements are 0.
Example
from sympy.matrices import eye eye(3)
Output
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]$
以上代码的输出如下 −
The output for the above code is as follows −
[1 0 0 0 1 0 0 0 1]
[1 0 0 0 1 0 0 0 1]
在 diag 矩阵中,对角线上的元素根据提供参数来初始化。
In diag matrix, elements on diagonal are initialized as per arguments provided.
>>> 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]$
以上代码的输出如下 −
The output for the above code is as follows −
[1 0 0 0 2 0 0 0 3]
[1 0 0 0 2 0 0 0 3]
在 zeros 矩阵中,所有元素都初始化为 0。
All elements in zeros matrix are initialized to 0.
>>> from sympy.matrices import zeros
>>> zeros(2,3)
$\displaystyle \left[\begin{matrix}0 & 0 & 0\\0 & 0 & 0\end{matrix}\right]$
以上代码的输出如下 −
The output for the above code is as follows −
[0 0 0 0 0 0]
[0 0 0 0 0 0]
类似地,ones 是一个矩阵,其中的所有元素都设置为 1。
Similarly, ones is matrix with all elements set to 1.
>>> from sympy.matrices import ones
>>> ones(2,3)
$\displaystyle \left[\begin{matrix}1 & 1 & 1\\1 & 1 & 1\end{matrix}\right]$
以上代码的输出如下 −
The output for the above code is as follows −
[1 1 1 1 1 1]
[1 1 1 1 1 1]