numpy对矩阵求逆并输出分数的形式
import numpy as np
from fractions import Fraction
# 使输出结果为分数
np.set_printoptions(formatter={'all': lambda x: str(Fraction(x).limit_denominator())})
# 创建4*4矩阵
a = np.array([[1, 0, 0, 1], [-1, 1, 0, 0], [0, -1, 1, 0], [0, 0, -1, 1]])
b = np.array([[2, 0, 5, 6], [1, 3, 3, 6], [-1, 1, 2, 1], [1, 0, 1, 3]])
# 矩阵求逆
a_ = np.linalg.inv(a)
b_ = np.linalg.inv(b)
print(a)
print(b)
print(a_)
print(b_)
# 对A逆B进行矩阵相乘
print(np.dot(a_, b))