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

标签:SymPy,矩阵,运算
0
投稿

猜你喜欢

  • python读取并绘制nc数据的保姆级教程

    2023-11-23 02:19:24
  • SQL Server2019数据库备份与还原脚本(批量备份)

    2024-01-27 06:46:47
  • 用玩票的心态瞎猜豆瓣的思路

    2008-08-18 21:14:00
  • 用非动态SQL Server SQL语句来对动态查询进行执行

    2024-01-19 08:55:05
  • 非集成环境的php运行环境(Apache配置、Mysql)搭建安装图文教程

    2023-07-21 16:21:41
  • 微信小程序学习笔记之文件上传、下载操作图文详解

    2023-09-07 21:13:21
  • Python利用多线程枚举实现获取wifi信息

    2021-12-05 03:58:12
  • vue中v-for通过动态绑定class实现触发效果

    2024-04-09 10:45:21
  • python监控进程状态,记录重启时间及进程号的实例

    2022-04-22 07:47:20
  • JavaScript实现前端倒计时效果

    2024-06-05 09:34:10
  • Python Pygame实现兔子猎人守护城堡游戏

    2021-09-21 11:09:59
  • python两个_多个字典合并相加的实例代码

    2023-05-05 07:50:33
  • 解决90%的常见问题的8个python NumPy函数

    2021-12-06 23:33:39
  • asp如何用WSH获取机器的IP配置信息?

    2010-06-13 14:39:00
  • pytorch 计算ConvTranspose1d输出特征大小方式

    2021-08-14 10:16:16
  • Python随机数用法实例详解【基于random模块】

    2023-10-26 08:48:49
  • Python实现将内容转为base64编码与解码

    2021-11-25 14:44:24
  • Python中字符编码简介、方法及使用建议

    2021-10-11 21:58:33
  • pandas删除行删除列增加行增加列的实现

    2022-10-27 13:22:55
  • python有证书的加密解密实现方法

    2023-02-10 08:07:30
  • asp之家 网络编程 m.aspxhome.com