【Python】SymPy库——关于矩阵的基本操作和运算

网上有很多关于科学计算包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)

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值