SymPy库关于矩阵的基本操作和运算 您所在的位置:网站首页 python生成一个单位矩阵 SymPy库关于矩阵的基本操作和运算

SymPy库关于矩阵的基本操作和运算

2023-03-13 02:20| 来源: 网络整理| 查看: 265

目录1、矩阵创建2、创建列向量3、截取矩阵的某一行或列,使用row()和col()4、删除行向量、列向量5、插入行向量、列向量使用row_insert或col_insert。6、像加法、乘法...

目录1、矩阵创建2、创建列向量3、截取矩阵的某一行或列, 使用 row()和 col()4、删除行向量、列向量5、插入行向量、列向量使用row_insert 或 col_insert。6、像加法、乘法这样的简单运算使用+,*, **就可以了7、特殊矩阵的创建

网上有很多关于科学计算包sympy的介绍,这里我把官方文档的英文表述贴过来。简单翻译就是syhttp://www.cppcns.commpy是个代数系统,底层完全使用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.

正好最近在研究线性代数,顺手把SymPy中关于矩阵的基本用法记录一下。

1、矩阵创建

矩阵的创建过程被理解为提供了一组行向量。 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、创建列向量

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)) # 打印倒数第一列 print(matrix_b.row(-1)) # 打印倒数第一行

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)产生新的矩阵对象 (2)第一参数指的是插入位置,第二个参数是需要插入的内容 (3)注意区分插入行向量和列向量,Matrix()括号内略有不同,具体看下面的代码:

matrix_d = sympy.Matrix([[1, 0, -1], [-5, 3, 4]])matrix_d_insert_col = matrix_d.col_insert(0, sjavascriptympy.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设置为-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) """矩阵转置""" matrix_transpose = matrix_a.T # print(matrix_transpose)

7、特殊矩阵的创建 """生成单位矩阵,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(),参数可以是数字,也可以是矩阵 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)

到此这篇关于SymPy库关于矩阵的基本操作和运算的文章就介绍到这了,更多相关SymPy矩阵运算内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有