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

Matrices
Liberdade é um prato facil de comer.
Mas deficil de digerir
Jean-Jaques Rousseau
< Matrices Básicas | Operações Algébricas | Copiar Matriz >

Operações Algébricas

  • Adicionar Matrices:
    Python Listing: ../Matrix.py.
    def Matrices_Add(A,B):
        if (   len(A)!=len(B)   ):
            print "Matrices_Add: Matrices does not have same no of rows"
            exit()
    
        C=[]
        for i in range( len(A) ):
            C.append(  []  )
            if (   len(A[i])!=len(B[i])   ):
                print "Matrices_Add: Matrices does not have same no of columns"
                exit()
    
            for j in range( len(A[i]) ):
                C[i].append(   1.0*(A[i][j]+B[i][j])   )
                
        return C
    
    
  • Subtrair Matrices:
    Python Listing: ../Matrix.py.
    def Matrices_Sub(A,B):
        if (   len(A)!=len(B)   ):
            print "Matrices_Add: Matrices does not have same no of lines"
            exit()
    
        C=[]
        for i in range( len(A) ):
            C.append(  []  )
            if (   len(A[i])!=len(B[i])   ):
                print "Matrices_Add: Matrices does not have same no of columns"
                exit()
    
            for j in range( len(A[i]) ):
                C[i].append(   1.0*(A[i][j]-B[i][j])   )
                
        return C
    
    
  • Multiplicar Matrices:
    Python Listing: ../Matrix.py.
    def Matrix_Mult_Scalar(A,a):
        C=[]
        for i in range( len(A) ):
            C.append(  []  )
            for j in range( len(A[i]) ):
                C[i].append(   a*A[i][j]  )
                
        return C
    
    def Matrix_Mult_Vector(A,v):
        if (   len(A[0])!=len(v)   ):
            print "Matrices_Mult_Vector: Matrices and Vector has incompatible dimensions"
            exit()
        w=[]
        for i in range( len(A) ):
            sum=0.0
            for j in range( len(A[i]) ):
                sum+=1.0*A[i][j]*v[j]
    
            w.append(sum)
        return w
    
    def Matrices_Mult(A,B):
        if (   len(A[0])!=len(B)   ):
            print "Matrices_Mult: Matrices has incompatible dimensions"
            exit()
    
        C=[]
        for i in range( len(A) ):
            C.append( [] )
            for j in range( len(B[0]) ):
                sum=0.0
                for k in range( len(B) ):
                    sum+=1.0*A[i][k]*B[k][j]
    
                C[i].append(sum)
        return C
    
    
< Matrices Básicas | Operações Algébricas | Copiar Matriz >
Messages:
0 secs.