Python中的高斯消除与透视

时间:2018-02-28 01:34:44

标签: python numpy gaussian numerical-analysis

遇到此问题的麻烦。我无法说出错误。这似乎有点太复杂,我得到错误。我实际上是用维基百科的伪代码做的。

import numpy as np
import math

A = [[5,1,0,2,1],[0,4,0,1,2],[1,1,4,1,1],[0,1,2,6,0],[0,0,1,2,4]]
b = [[1],[2],[3],[4],[5]]

def swap_rows(A,i,j):
    temp = A[i]
    A[i]=A[j]
    A[j] = temp
    return(A) 

def Gauss(A):
    n = len(A)
    for k in range(n):
        myList = []
        for p in range(k,n):
            myList.append(abs(A[p][k]))
        i_max = np.argmax(myList)
        if A[i_max][k] == 0:
            print('Error: Matrix is singular')
            return
        A = swap_rows(a,k,i_max)
        for i in range(k,n):
            f = (A[i][k])/(A[k][k])
            for j in range(k,n):
                A[i][j] = A[i][j]-A[k][j]*f
        A[i][k] = 0
    return(A)

Gauss(A)

0 个答案:

没有答案