SymPy庫關(guān)于矩陣的基本操作和運算
網(wǎng)上有很多關(guān)于科學(xué)計算包sympy的介紹,這里我把官方文檔的英文表述貼過來。簡單翻譯就是sympy是個代數(shù)系統(tǒng),底層完全使用python語言寫的,使用簡單、好理解、易擴展。
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、像加法、乘法這樣的簡單運算使用+,*, **就可以了
求逆矩陣,只需把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)于矩陣的基本操作和運算的文章就介紹到這了,更多相關(guān)SymPy矩陣運算內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
django Layui界面點擊彈出對話框并請求邏輯生成分頁的動態(tài)表格實例
這篇文章主要介紹了django Layui界面點擊彈出對話框并請求邏輯生成分頁的動態(tài)表格實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python基于文本內(nèi)容實現(xiàn)隱私信息提取與評估
這篇文章主要為大家介紹了Python如何實現(xiàn)基于文本內(nèi)容的用戶隱私泄露風(fēng)險評估系統(tǒng),文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2025-03-03
手把手帶你了解Python數(shù)據(jù)分析--matplotlib
這篇文章主要介紹了Python實現(xiàn)matplotlib顯示中文的方法,結(jié)合實例形式詳細總結(jié)分析了Python使用matplotlib庫繪圖時顯示中文的相關(guān)操作技巧與注意事項,需要的朋友可以參考下2021-08-08
使用C語言來擴展Python程序和Zope服務(wù)器的教程
這篇文章主要介紹了使用C語言來擴展Python程序和Zope服務(wù)器的教程,本文來自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04
python實現(xiàn)同一局域網(wǎng)下傳輸圖片
這篇文章主要為大家詳細介紹了python實現(xiàn)同一局域網(wǎng)下傳輸圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03

