我在使用 Python 时遇到了一些麻烦。使用 Python 2.X 并且知道它在除法方面有一些困难,这就是我添加的原因,from __future__ import division
并且到目前为止它运行良好。但是我注意到,当我想在这个项目中用浮点数覆盖一个 int 时它不起作用,我在一个较小的项目中对其进行了测试,但它工作正常。
有人知道这里出了什么问题吗?
A = [
[3, 3, 3],
[1, 1, 2],
[1, 0, 3]
]
b = [0, 0, 3]
def gauss_elim(A,b):
print A[0], b[0]
print A[1], b[1]
print A[2], b[2]
n = len(A)
temp = []
temp_b = []
MAX = 0
te = [0,0,0]
for i in range(0,n-1):
k=i
for a in range(i,n-1):
### Pivotsearch Start ###
if math.fabs(A[a][i]) < math.fabs(A[a+1][i]):
MAX = (a+1)
temp = A[i]
A[i] = A[MAX]
A[MAX] = temp
temp_b = b[i]
b[i] =b[MAX]
b[MAX]=temp_b
### Pivotsearch End ###
for j in range(i+1, n):
if A[j][i] == 0:
continue
A[j][i] = A[j][i] - ( A[j][i] / A[i][i] ) * A[i][i]
b[j] = ( b[j] - (A[j][i])/(A[i][i]) ) #<<<< DOES NOT OVERWRITE
k = k + 1
print 'done:'
print A[0], b[0]
print A[1], b[1]
print A[2], b[2]
gauss_elim(A,b)