最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SymPy庫(kù)關(guān)于矩陣的基本操作和運(yùn)算

 更新時(shí)間:2023年03月10日 08:33:58   作者:jeffery18  
本文主要介紹了SymPy庫(kù)關(guān)于矩陣的基本操作和運(yùn)算,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

網(wǎng)上有很多關(guān)于科學(xué)計(jì)算包sympy的介紹,這里我把官方文檔的英文表述貼過(guò)來(lái)。簡(jiǎn)單翻譯就是sympy是個(gè)代數(shù)系統(tǒng),底層完全使用python語(yǔ)言寫(xiě)的,使用簡(jiǎn)單、好理解、易擴(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)建過(guò)程被理解為提供了一組行向量。 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對(duì)象被理解為一個(gè)列向量。 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. 注意下:這些操作直接修改原來(lái)的矩陣對(duì)象,不會(huì)生成新的對(duì)象。

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注意三個(gè)點(diǎn): (1)產(chǎn)生新的矩陣對(duì)象 (2)第一參數(shù)指的是插入位置,第二個(gè)參數(shù)是需要插入的內(nèi)容 (3)注意區(qū)分插入行向量和列向量,Matrix()括號(hào)內(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、像加法、乘法這樣的簡(jiǎn)單運(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)
 
"""矩陣求逆,如果不可逆,報(bào)錯(cuò)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)
 
"""生成對(duì)角矩陣,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)  # 生成對(duì)角線元素為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庫(kù)關(guān)于矩陣的基本操作和運(yùn)算的文章就介紹到這了,更多相關(guān)SymPy矩陣運(yùn)算內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

望城县| 岑溪市| 瑞昌市| 申扎县| 容城县| 宿州市| 平阳县| 湖州市| 澜沧| 赞皇县| 红桥区| 松桃| 会宁县| 赤水市| 新丰县| 仲巴县| 西和县| 青川县| 丰都县| 丹阳市| 久治县| 郸城县| 读书| 昌黎县| 理塘县| 上虞市| 博乐市| 上高县| 新疆| 武冈市| 克山县| 眉山市| 南涧| 灌云县| 蒙自县| 天等县| 南丹县| 鲁甸县| 乐山市| 大冶市| 应城市|