1.矩阵构造

代码如下:

from sympy import *

m1 = Matrix([[1, 2], [2, 0], [3, 4]])
# 构造单位矩阵
m2 = eye(3)
# 构造零矩阵
m3 = zeros(3)
# 构造1矩阵
m4 = ones(3)
# 构造对角矩阵
m5 = diag(1, 2, 3)
print(m1, m2, m3, m4, m5, sep='\n')

运行结果:

2.矩阵计算

代码如下:

from sympy import *

M = Matrix([[3, -2, 4, -2], [5, 3, -3, -2], [5, -2, 2, -2], [5, -2, -3, 3]])
# 矩阵转置
m1 = M.T
# 逆矩阵
m2 = M ** -1
# 矩阵的行列式
m3 = M.det()
# 矩阵的特征值
m4 = M.eigenvals()
# 矩阵的特征多项式
k = symbols('k')
p = M.charpoly(k)
m5 = factor(p)
print(m1, m2, m3, m4, m5, sep='\n')

运行结果: