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