SymPy庫關(guān)于矩陣的基本操作和運(yùn)算
網(wǎng)上有很多關(guān)于科學(xué)計算包sympy的介紹,這里我把官方文檔的英文表述貼過來。簡單翻譯就是sympy是個代數(shù)系統(tǒng),底層完全使用python語言寫的,使用簡單、好理解、易擴(kuò)展。
SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python.
正好最近在研究線性代數(shù),順手把SymPy中關(guān)于矩陣的基本用法記錄一下。
1、矩陣創(chuàng)建
矩陣的創(chuàng)建過程被理解為提供了一組行向量。 A matrix is constructed by providing a list of row vectors that make up the matrix.
import sympy matrix_a = sympy.Matrix([[1, 3], [-2, 3]]) # 生成2*2的方陣 matrix_b = sympy.Matrix([[1, 0, 2], [2, 3, 4]]) # 生成2*3的矩形矩陣
2、創(chuàng)建列向量
list對象被理解為一個列向量。 a list of elements is considered to be a column vector.
column_vector_a = sympy.Matrix([1, 2, 1])
3、截取矩陣的某一行或列, 使用 row()和 col()
print(matrix_b.col(0)) # 打印第一列 print(matrix_b.row(0)) # 打印第一行 print(matrix_b.col(-1)) # 打印倒數(shù)第一列 print(matrix_b.row(-1)) # 打印倒數(shù)第一行
4、刪除行向量、列向量
使用 row_del 或 col_del. 注意下:這些操作直接修改原來的矩陣對象,不會生成新的對象。
matrix_c = sympy.Matrix([[1, 0, 2, 4], [2, 3, 4, 0], [0, 1, 1, 1]]) matrix_c.row_del(0) # 刪除第一行 matrix_c.col_del(1) # 刪除第二列
5、插入行向量、列向量使用row_insert 或 col_insert。
insert注意三個點: (1)產(chǎn)生新的矩陣對象 (2)第一參數(shù)指的是插入位置,第二個參數(shù)是需要插入的內(nèi)容 (3)注意區(qū)分插入行向量和列向量,Matrix()括號內(nèi)略有不同,具體看下面的代碼:
matrix_d = sympy.Matrix([[1, 0, -1], [-5, 3, 4]])
matrix_d_insert_col = matrix_d.col_insert(0, sympy.Matrix([8, 9])) # 在第一列插入列向量[8,9]
# print(matrix_d_insert_col)
matrix_d_insert_row = matrix_d.row_insert(0, sympy.Matrix([[1, 1, 1]])) # 在第一行插入行向量
# print(matrix_d_insert_row)
6、像加法、乘法這樣的簡單運(yùn)算使用+,*, **就可以了
求逆矩陣,只需把power設(shè)置為-1即可。 simple operations like addition and multiplication are done just by using +, *, and **. To find the inverse of a matrix, just raise it to the -1 power.
"""矩陣乘法""" matrix_multiplication = matrix_a * matrix_b # print(matrix_multiplication) """矩陣求逆,如果不可逆,報錯NonInvertibleMatrixError: Matrix det == 0; not invertible.""" matrix_inverse = matrix_a ** -1 # print(matrix_inverse) """矩陣轉(zhuǎn)置""" matrix_transpose = matrix_a.T # print(matrix_transpose)
7、特殊矩陣的創(chuàng)建
"""生成單位矩陣,eye(n) will create an identity matrix""" matrix_identity = sympy.eye(2) # 生成二階單位矩陣 # print(matrix_identity) """生成n*m的零矩陣,zeros(m,n)""" matrix_zero = sympy.zeros(2, 3) # 生成2*3的零矩陣 # print(matrix_zero) """生成元素都是1的矩陣,ones(m,n)""" matrix_one = sympy.ones(2, 2) # 生成2*2的元素都為1的方陣 # print(matrix_one) """生成對角矩陣,diag(),參數(shù)可以是數(shù)字,也可以是矩陣 The arguments to diag can be either numbers or matrices. A number is interpreted as a matrix. The matrices are stacked diagonally. """ matrix_diag_1 = sympy.diag(1, 2, 3) # 生成對角線元素為1,2,3的三階方陣 # print(matrix_diag_1) matrix_diag_2 = sympy.diag(1, sympy.ones(2, 2), sympy.Matrix([[1, 2], [1, 3]])) # print(matrix_diag_2)
到此這篇關(guān)于SymPy庫關(guān)于矩陣的基本操作和運(yùn)算的文章就介紹到這了,更多相關(guān)SymPy矩陣運(yùn)算內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章

基于python實現(xiàn)數(shù)組格式參數(shù)加密計算

Python 實現(xiàn)Serial 與STM32J進(jìn)行串口通訊

Python 實現(xiàn)任意區(qū)域文字識別(OCR)操作

10分鐘教你用python動畫演示深度優(yōu)先算法搜尋逃出迷宮的路徑

淺談插入排序算法在Python程序中的實現(xiàn)及簡單改進(jìn)