SmtC: Show me the Code
Ole Peter Smith
Instituto de Matemática e Estatística
Universidade Federal de Goiás
http://www.olesmith.com.br

Eliminação do Gauss
E se Eva tinha optado pela cobra?
Facebook.
< Pivotação Parcial | Lado Direito Matricial | Matriz Inversa >

Lado Direito Matricial

\(\underline{\underline{A}} ~ \underline{\underline{X}} = \underline{\underline{B}}\)
  • Forward:
    Python Listing: ../../../Code/Gauss.py.
    def Gauss2_Forward(A,B=[]):
        det=1.0
        for i in range( len(A) ):
            pivote=Gauss_Pivote(A,i)
    
            if (pivote!=i):
                if (B): Matrices_Rows_Swap(B,i,pivote)
                Matrices_Rows_Swap(A,i,pivote)
                det*=-1.0
            
            if (A[i][i]==0.0):
                print "Gauss2_Forward: Matrix Singular at ",i,"x",i
                return 0.0
                
            det*=A[i][i]
            fact=1.0/A[i][i]
            
            #Always work on b first
            if (B): Matrices_Row_Mult(B,i,fact)
            Matrices_Row_Mult(A,i,fact)
    
            for j in range( i+1,len(A) ):
                if (B): Matrices_Rows_Operation(B,j,-A[j][i],i)
                Matrices_Rows_Operation(A,j,-A[j][i],i)
    
        return det
    
    
  • Backward:
    Python Listing: ../../../Code/Gauss.py.
    def Gauss2_Backward(A,B=[]):
        for i in range(len(A)-1,-1,-1):
            for j in range(i):
                if (B): Matrices_Rows_Operation(B,j,-A[j][i],i)
                Matrices_Rows_Operation(A,j,-A[j][i],i)
                
    
  • Both:
    Python Listing: ../../../Code/Gauss.py.
    def Gauss2(A,X=[]):
        det=Gauss2_Forward(A,X)
        print "Gauss Forward, det",det
    
        if (det==0.0):
            exit()
        
        Gauss2_Backward(A,X)
    
        return det
    
    
  • Test:
    Python Listing: ../../../Code/Gauss.py.
    def Gauss2_Test(A,B=[]):
        #Save for residual calc
        AA=Matrix_Copy(A)
        BB=Matrix_Copy(B)
        
        det=Gauss2(A,B)
        
        #B is our solution, calc residual
        R_B=Matrices_Mult(AA,B)
        R_B=Matrices_Sub(R_B,BB)
        
        residual=Matrix_Norm(R_B)/Matrix_Norm(BB)
        print "Matrix_Gauss_Test, Residual: %.6e" %  residual
    
        return det
    
    
    
< Pivotação Parcial | Lado Direito Matricial | Matriz Inversa >
Messages:
0 secs.