赞
踩
import fractions def row_echelon(A): """ 将矩阵A化为行阶梯矩阵 """ lead = 0 rowCount = len(A) columnCount = len(A[0]) for r in range(rowCount): if lead >= columnCount: return i = r while A[i][lead] == 0: i += 1 if i == rowCount: i = r lead += 1 if columnCount == lead: return A[i], A[r] = A[r], A[i] lv = A[r][lead] A[r] = [fractions.Fraction(mrx, lv) for mrx in A[r]] for i in range(rowCount): if i != r: lv = A[i][lead] A[i] = [iv - lv*rv for rv,iv in zip(A[r],A[i])] lead += 1 # 定义一个3x3的矩阵 A = [ [4,1,2], [13,-2,3], [0,3,2] ] # 将矩阵化为行最简形式 row_echelon(A) # 以分数形式打印结果 for row in A: print([str(fractions.Fraction(x).limit_denominator()) for x in row])
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。