python高斯消元

'''
python高斯消元
直接消:
先把每行第一个变成1,然后逐个去减每行数,之后再反着消,最后把原系数矩阵变成单位矩阵

'''
a = [[12, -3, 3, 15], [18, -3, 1, 15], [-1, 2, 1, 6]]
for i in range(len(a)):
    tmp = a[i][i]
    for j in range(len(a[0])):
        a[i][j] *= 1/tmp

    for j in range(i+1, len(a)):
        tmp = a[j][i]
        for k in range(len(a[0])):
            a[j][k] -= a[i][k]*tmp

for i in range(len(a[0])-2, 0, -1):
    for j in range(i):
        tmp = a[j][i]
        for k in range(len(a[0])):
            a[j][k] -= a[i][k]*tmp

for item in a:
    print(item)

你可能感兴趣的:(python,J#)