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]

矩陣是一個可變物件。matrices 模組還提供 ImmutableMatrix 類來獲取不可變矩陣。

基本操作

Matrix 物件的shape屬性返回其大小。

>>> 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() 方法。行列式是一個標量值,可以從方陣的元素計算得到。

>>> 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 提供許多特殊型別的矩陣類。例如,單位矩陣、全零矩陣和全一矩陣等。這些類分別命名為 eye、zeros 和 ones。單位矩陣是一個方陣,其對角線上的元素設定為 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]

廣告
© . All rights reserved.