Demo entry 6356478

tpmet.py

   

Submitted by anonymous on Apr 18, 2017 at 16:53
Language: Python. Code size: 3.5 kB.

import csv

# IMPORT MATRIX (A)
filename = input('Enter matrix file name: ')
with open(filename) as file:
    A = list(list(i) for i in csv.reader(file, delimiter=','))
    
for i in range(len(A)):
    A[i]=list(map(float,A[i]))

# IMPORT VECTOR (b)
filename = input('Enter vector file name: ')
with open(filename) as file:
    b = list(list(i) for i in csv.reader(file, delimiter=','))
    
b=b[0]
b=list(map(float,b))

# CHEQUEAR DIMENSIONES
if len(A) == len(b):

    
    # FUNCIONES
    def resta(u,v):
        """
        devuelve la resta de vectores u-v
        """
        w=[]
        for i in range(len(u)):
            w.append(u[i]-v[i])
        return w
    
    def columna(matrix,n):
        """
        devuelve la columna n-esima de matrix
        """
        col=[]
        for row in matrix:
            col.append(row[n])
        return col
    
    def addcol(matrix,vec,n):
        """
        agrega el vec a la columna n-esima de matrix
        """
        i=0
        for row in matrix:
            row[n]=vec[i]
            i+=1
        return matrix
    
    def norma(vector):
        """
        devuelve la norma del vector
        """
        norm=0
        for i in range(len(vector)):
            norm=norm+vector[i]**2
        norm=norm**(1/2)
        return norm
            
    def ceros(matrix,fil,col):
        """
        devuelve una matriz de fil x col llena de ceros
        """
        matrix=[[]for i in range(fil)]
        for i in range(fil):
            for j in range(col):
                matrix[i].append(0)
        return matrix
    
    def prod(v,u):
        """
        producto escalar
        """
        w=0
        for i in range(len(v)):
            w=w+(v[i])*(u[i])
        return w
    
    def trasp(matrix):
        """
        devuelve la matriz transpuesta
        """
        B=[]
        for i in range(len(matrix[0])):
            B.append([row[i] for row in matrix])
        return B
    
    def matVec(matrix, vector):
        """
        producto entre matriz y vector
        """
        u=[]
        for i in range(len(matrix)):
            u.append(prod(matrix[i],vector))
        return u

    # DESCOMPOSICION QR    
    Q=[]
    Q=ceros(Q,len(A),len(A[0]))
    R=[]
    R=ceros(R,len(A[0]),len(A[0]))
    
    for i in range(len(A[0])):
        if i==0:
            a=columna(A,0)
            norm = norma(a)
            R[0].insert(0,norm)
            q=list(map(lambda x: x/norm,a))
            Q=addcol(Q,q,0)
        else:
            a=columna(A,i)
            p=a
            for j in range(i):
                q=columna(Q,j)
                r=prod(a,q)
                p=resta(p,list(map(lambda x: x*r,q)))
                R[j][i]=r
            R[i][i]=norma(p)
            Q=addcol(Q,list(map(lambda x: x/R[i][i],p)),i)
                  
    # RESOLVER X
    x=[]
    x=ceros(x,1,len(R))
    x=x[0]
    y=matVec(trasp(Q),b)
    
    for i in range(len(R)-1,-1,-1):
        if i ==len(R)-1:
            x[i]=y[i]/R[i][i]
        else:
            x[i]=y[i]
            for j in range(i+1,len(R)):
                x[i]=x[i]-x[j]*R[i][j]
            x[i]=x[i]/R[i][i]
    
    # EXPORTA EL RESULTADO (x)
    with open('out.csv', 'w') as file:
        writer = csv.writer(file)
        writer.writerow(x)
        
    print('El resultado se guardo en out.cvs')

else:
    print('Dimensiones incompatibles')

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).