如题针对:
A9 = Matrix([[2,0,1],
[-1,-1,-2],
[-3,0,1]])
b9 = Vector([1, 0, 0])
ls9 = LinearSystem(A9, b9)
if not ls9.gauss_jordan_elimination():
print("No Solution!")
ls9.fancy_print()
---------
会得出这样得结果:
No Solution!
1.0 0.0 0.0 | 0.19999999999999996
0.0 0.0 1.0 | 0.6000000000000001
0.0 -1.0 0.0 | 1.4000000000000001
=====
经调试,发现判断主元时,没有考虑是负数情况,缺少变负为正考虑;
试着优化代码如下:
## 完成前向高斯消元
def _forward(self):
i,k =0,0
while i<self._m and k<self._n:
if self.Ab[i][k] < 0: ##增加判断
self.Ab[i] = self.Ab[i]*(-1)
但是打印结果发现会对:0.0也有-号了
1.0 0.0 0.0 | 0.19999999999999996
-0.0 1.0 0.0 | -1.4000000000000001
0.0 0.0 1.0 | 0.6000000000000001
请问老师,针对这类情况,您会怎么优化,改进代码??