SymPy库关于矩阵的基本操作和运算
作者:jeffery18 发布时间:2022-10-30 12:23:55
网上有很多关于科学计算包sympy的介绍,这里我把官方文档的英文表述贴过来。简单翻译就是sympy是个代数系统,底层完全使用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, 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设置为-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)
来源:https://blog.csdn.net/weixin_43088960/article/details/123958892


猜你喜欢
- 前言小程序支持webview以后,我们开发的好多h5页面,就可以直接在小程序里使用了,比如我们开发的微信商城,文章详情页,商品详情页,就可以
- 代码如下:Create trigger tri_wk_CSVHead_History on wk_CSVHead_History --声明
- git checkout . #本地所有修改的。没有的提交的,都返回到原来的状态git stash #把所有没有提交的修改暂存到stash里
- 在网上down了个web项目,在 IntelliJ IDEA 这个编辑器里面跑起来,但是发现domain文件夹下的xml文件都报如下的红色提
- 常在读写文件之前,需要判断文件或目录是否存在,不然某些处理方法可能会使程序出错。所以最好在做任何操作之前,先判断文件是否存在。这里将介绍三种
- Django默认支持Session,并且默认是将Session数据存储在数据库中的修改session存取放在数据库中SESSION_ENGI
- Request.ServerVariables("Url") 返回服务器地址Request.ServerVariable
- /* 小弟刚刚接触ORACLE存储过程,有一个问题向各位同行求教,小弟写了一个存储过程,其目的是接收一个参数作为表名,然后查询该表中的全部记
- 概述Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案。Redis从它的许多竞争继承
- 在查询语句中使用 NOLOCK 和 READPAST 处理一个数据库死锁的异常时候,其中一个建议就是使用 NOLOCK 或者 READPAS
- 在前面已经学习了gin框架如何处理请求,解析请求,返回数据。在实际的项目当中,项目往往是以模块化来进行划分和开发的,所谓的模块化就是按照功能
- 本文实例讲述了python简单程序读取串口信息的方法。分享给大家供大家参考。具体分析如下:这段代码需要调用serial模块,通过while循
- python 2.7.11django 1.8.4错误内容:related Field has invalid lookup: iconta
- 前言之前写过一篇关于阿里的低代码工具LowCodeEngine 的文章,发现大家还是挺感兴趣的。最近又发现了一款很有意思的低代码工具Yao,
- __str__函数如果定义了该函数,当print当前实例化对象的时候,会返回该函数的return信息可用于定义当前类的描述信息用法:def
- 经常看到有人误删数据,或者误操作,特别是update和
- 如果程序中没有设置session的过期时间,那么session过期时间就会按照IIS设置的过期时间来执行,IIS中session默认过期时间
- 1.实例方法Python 的实例方法用得最多,也最常见。我们先来看 Python 的实例方法。class Kls(object): &nbs
- 本文实例讲述了python访问系统环境变量的方法。分享给大家供大家参考。具体如下:#----------------------------
- 1. 获取系统当前时间MySQL 版本为 5.7,详细的时间函数可以参考 MySQL 官方文档 在这里1.1. 获取 YYYY-MM-DD