def metodoCholesky(A, B, n): #creamos matriz nula G G = [[0.0] * n] * n #creamos matriz nula G_T for i in range(n): suma = A[i][i] for k in range(i): suma = suma - A[k][i]**2 if suma < 0: #no es definida positiva return ["NULL", "NULL"] A[i][i] = math.sqrt(suma) for j in range(i + 1, n): suma = A[i][j] for k in range(i): suma = suma - A[k][i] * A[k][j] A[i][j] = suma / A[i][i] for j in range(n): for i in range(n): if (i > j): A[i][j] = 0.0 G = A Gt = funciones.transMatriz(G, n) print "\n Resolucion por Algoritmo Cholesky " print " -------------------------------------------------" print "\n Matriz G Triangular Superior" funciones.imprimeMatriz(G) print "\n Matriz Gt Triangular Inferior" funciones.imprimeMatriz(Gt) y = funciones.solucionL(Gt, B, n) #Ly = b "obtenemos y" x = funciones.solucionU(G, y, n) #Ux = y "obtenemos x" return [y, x]
def metodoDoolittle(A, B, n): U = [[0.0] * n for j in range(n)] L = [[float(i == j) for j in range(n)] for i in range(n)] a = funciones.matrizAumentada(A, B, n) for j in range(n): a = funciones.pivot(a, j) A = [] B = [] for i in range(n): A.append(a[i][0:n]) B.append(a[i][n]) for k in range(n): for i in range(k + 1): U[i][k] = A[i][k] - sum(L[i][p] * U[p][k] for p in range(i)) for i in range(k + 1, n): if U[k][k] == 0: return L, U, ["NULL", "NULL"] L[i][k] = (A[i][k] - sum(L[i][p] * U[p][k] for p in range(k))) / float(U[k][k]) print "\n Resolucion por Algoritmo LU-Doolittle " print " -------------------------------------------------" print "\n Matriz U Triangular Superior" funciones.imprimeMatriz(U) print "\n Matriz L Triangular Inferior" funciones.imprimeMatriz(L) y = funciones.solucionL(L, B, n) #Ly = b "obtenemos y" x = funciones.solucionU(U, y, n) #Ux = y "obtenemos x" return [y, x]
def metodoGramm(A, b, n): #La matriz A es mxn por tanto la matriz E sera de orden mxn y U nxn #Creamos matriz E E = [] for i in range(n): E.append([0] * n) #Creamos matriz U U = [] for i in range(n): U.append([0] * n) #Algoritmo for j in range(n): for k in range(n): E[k][j] = A[k][j] for i in range(j): U[i][j] = funciones.productoGramm(E, n, i, j) for k in range(n): E[k][j] = E[k][j] - U[i][j] * E[k][i] U[j][j] = math.sqrt(funciones.sumaGramm(E, n, j)) for k in range(n): E[k][j] = E[k][j] / U[j][j] y = funciones.solGramm(E, b, n) x = funciones.solGramm(U, y, n) print "\n Resolucion por Algoritmo de Gramm Schmidt " print " -------------------------------------------------" print " Matriz E(Q):" funciones.imprimeMatriz(E) print " Matriz U(R):" funciones.imprimeMatriz(U) return y, x
def metodoLU1(A, B, n): L = [] for i in range(n): L.append([0] * n) U = [] for i in range(n): U.append([0] * n) #pivot a = funciones.matrizAumentada(A, B, n) for j in range(n): a = funciones.pivot(a, j) A = [] B = [] for i in range(n): A.append(a[i][0:n]) B.append(a[i][n]) #LU1 for j in range(0, n): U[j][j] = 1.0 for i in range(j, n): L[i][j] = A[i][j] - funciones.suma(U, L, i, j, j) #ultima j por i for i in range(j + 1, n): if L[j][j] == 0: return ["NULL", "NULL"] U[j][i] = (A[j][i] - funciones.suma(U, L, j, i, j)) / L[j][j] print "\n Resolucion por Algoritmo LU1-Crout " print " -------------------------------------------------" print "\n Matriz U Triangular Superior" funciones.imprimeMatriz(U) print "\n Matriz L Triangular Inferior" funciones.imprimeMatriz(L) y = funciones.solucionL(L, B, n) #Ly = b "obtenemos y" x = funciones.solucionU(U, y, n) #Ux = y "obtenemos x" return [y, x]
def LDLt(A, B, n): Lt = [[0.0] * n] * n for i in range(n): suma = A[i][i] for k in range(i): suma = suma - A[k][i]**2 if suma < 0: #no es definida positiva return ["NULL", "NULL"] A[i][i] = math.sqrt(suma) for j in range(i + 1, n): suma = A[i][j] for k in range(i): suma = suma - A[k][i] * A[k][j] A[i][j] = suma / A[i][i] for j in range(n): for i in range(n): if (i > j): A[i][j] = 0.0 Lt = A L = funciones.transMatriz(Lt, n) D = [[float(i == j) for j in range(n)] for i in range(n)] for i in range(n): D[i][i] = float(L[i][i]) for j in range(i + 1): L[i][j] = L[i][j] / D[j][j] for i in range(n): for j in range(i, n): Lt[i][j] = Lt[i][j] / D[i][i] for i in range(n): D[i][i] = D[i][i]**2 print "\n Resolucion por Algoritmo LDLt " print " -------------------------------------------------" print " Matriz L Triangular Inferior" funciones.imprimeMatriz(L) print " Matriz D Diagonal" funciones.imprimeMatriz(D) print " Matriz Lt Triangular Superior" funciones.imprimeMatriz(Lt) print "Para hallar el resultado de LDLt.x=b requerimos 3 ecuaciones:" z = funciones.solucionL(L, B, n) #Lz = b "obtenemos z" y = funciones.solucionL(D, z, n) #Dy = z "obtenemos y" x = funciones.solucionU(Lt, y, n) #Ltx = y "obtenemos x" return z, y, x
def LDMt(A, B, n): L = [] for i in range(n): L.append([0] * n) Mt = [] for i in range(n): Mt.append([0] * n) #pivot a = funciones.matrizAumentada(A, B, n) for j in range(n): a = funciones.pivot(a, j) A = [] B = [] for i in range(n): A.append(a[i][0:n]) B.append(a[i][n]) for j in range(0, n): Mt[j][j] = 1.0 for i in range(j, n): L[i][j] = A[i][j] - funciones.suma(Mt, L, i, j, j) #ultima j por i for i in range(j + 1, n): if L[j][j] == 0: return ["NULL", "NULL", "NULL"] Mt[j][i] = (A[j][i] - funciones.suma(Mt, L, j, i, j)) / L[j][j] D = [[float(i == j) for j in range(n)] for i in range(n)] for i in range(n): D[i][i] = float(L[i][i]) for j in range(i + 1): L[i][j] = L[i][j] / D[j][j] print "\n Resolucion por Algoritmo LDMt " print " -------------------------------------------------" print " Matriz L Triangular Inferior" funciones.imprimeMatriz(L) print " Matriz D Diagonal" funciones.imprimeMatriz(D) print " Matriz Mt Triangular Superior" funciones.imprimeMatriz(Mt) print "Para hallar el resultado de LDMt.x=b requerimos 3 ecuaciones:" z = funciones.solucionL(L, B, n) #Lz = b "obtenemos z" y = funciones.solucionL(D, z, n) #Dy = z "obtenemos y" x = funciones.solucionU(Mt, y, n) #Mtx = y "obtenemos x" return z, y, x
def metodoAasen(aa, bb, nn): global aux, P, T, I, P_aux, b_aux, pvt, L, Lt, Pt, A, b, n A = aa[:] b = bb[:] n = nn aux = 0.0 P = [[0 for i in range(n)] for j in range(n)] T, I, P_aux, b_aux, pvt, L, Lt, Pt = [], [], [], [], [], [], [], [] for i in range(n + 1): if i != n: T.append([]) I.append([]) pvt.append(i) L.append([]) Lt.append([]) Pt.append([]) b_aux.append(0.0) P_aux.append(0.0) for j in range(n + 1): if i != n and j != n: T[i].append(0.0) Lt[i].append(0.0) Pt[i].append(0.0) if i == j: I[i].append(1.0) L[i].append(1.0) else: I[i].append(0.0) L[i].append(0.0) a = [[0 for i in range(n + 1)] for j in range(n + 1)] a = funciones.paso(A, n) #PIVOTACION alfa, beta, h, l, v = [], [], [], [], [] smax = 0.0 iq = 0 iaux = 0 suma = 0.0 aux = 0.0 a = [[0 for i in range(n + 1)] for j in range(n + 1)] a = funciones.paso(A, n) for i in range(n + 1): alfa.append(0.0) beta.append(0.0) h.append(0.0) for i in range(n + 2): l.append(0.0) v.append(0.0) ### EMPIEZA ################ for j in range(1, n + 1): if j == 1: h[j] = a[1][1] elif j == 2: h[1] = beta[1] h[2] = a[2][2] else: l[0] = 0.0 l[1] = 0.0 for k in range(2, j): l[k] = L[j][k] l[j] = 1.0 h[j] = a[j][j] for k in range(1, j): h[k] = beta[k - 1] * l[k - 1] + alfa[k] * l[k] + beta[k] * l[k + 1] h[j] = h[j] - l[k] * h[k] if j == 1 or j == 2: alfa[j] = h[j] else: alfa[j] = h[j] - beta[j - 1] * L[j][j - 1] if j <= n - 1: smax = 0.0 iq = j for k in range(j + 1, n + 1): suma = 0.0 for e in range(1, j + 1): suma -= L[k][e] * h[e] v[k] = a[k][j] + suma if (abs(v[k]) >= smax): ###posible >= smax = abs(v[k]) iq = k aux = v[j + 1] v[j + 1] = v[iq] v[iq] = aux for k in range(2, j + 1): aux = L[j + 1][k] L[j + 1][k] = L[iq][k] L[iq][k] = aux iaux = pvt[j + 1] pvt[j + 1] = pvt[iq] pvt[iq] = iaux for k in range(j + 1, n + 1): aux = a[j + 1][k] a[j + 1][k] = a[iq][k] a[iq][k] = aux for k in range(j + 1, n + 1): aux = a[k][j + 1] a[k][j + 1] = a[k][iq] a[k][iq] = aux beta[j] = v[j + 1] if j <= n - 2: for k in range(j + 2, n + 1): L[k][j + 1] = v[k] if (v[j + 1] != 0.0): for k in range(j + 2, n + 1): L[k][j + 1] = L[k][j + 1] / v[j + 1] for j in range(n - 1): T[j][j] = alfa[j + 1] T[j + 1][j] = beta[j + 1] T[j][j + 1] = beta[j + 1] T[n - 1][n - 1] = alfa[n] for i in range(1, n + 1): P_aux[i] = I[pvt[i]] for i in range(n): for j in range(n): P[i][j] = P_aux[i + 1][j + 1] #FIN PIVOTE for i in range(n): b_aux[i] = b[pvt[i + 1] - 1] #LZ = b_aux j = 0 z = [] for i in range(n): z.append(0.0) while j < n: res = b_aux[j] for k in range(j): res -= L[j + 1][k + 1] * z[k] res /= float(L[j + 1][j + 1]) z[j] = res j += 1 # GAUS Tw =Z a = [] for i in range(n): a.append([]) for j in range(n): a[i].append(float(T[i][j])) for i in range(n): a[i].append(z[i]) maxi = 0 pos_maxi = -1 P1 = [] h1 = 0.0 for i in range(n): P1.append(i) for i in range(n - 1): maxi = 0 pos_maxi = -1 for p in range(i, n): if maxi < abs(a[p][i]): maxi = abs(a[p][i]) pos_maxi = p a[i], a[pos_maxi] = a[pos_maxi], a[i] P1[i], P1[pos_maxi] = P1[pos_maxi], P1[i] for j in range(i + 1, n): h1 = a[j][i] / float(a[i][i]) for k in range(i, n + 1): a[j][k] -= float((h1) * (a[i][k])) w = [] for i in range(n): w.append(0.0) j = n - 1 while j >= 0: res = a[j][n] for k in range(j + 1, n): res -= a[j][k] * w[k] res /= float(a[j][j]) w[j] = res j -= 1 #LY = w for i in range(n): for j in range(n): Lt[i][j] = L[j + 1][i + 1] j = n - 1 y = [] for i in range(n): y.append(0.0) while j >= 0: res2 = 0.0 for k in range(j + 1, n): res2 += Lt[j][k] * y[k] y[j] = float(w[j] - res2) / Lt[j][j] j -= 1 #X = P(t)y x = [] for i in range(n): x.append(0.0) for i in range(n): for j in range(n): Pt[i][j] = P[j][i] for i in range(n): aux = 0.0 for j in range(n): aux += Pt[i][j] * y[j] x[i] = aux print "\n Resolucion por Algoritmo de Aasen " print " -------------------------------------------------" print " Matriz P" funciones.imprimeMatriz(P) print " Matriz L" funciones.imprimeMatriz(funciones.transMatriz(Lt, n)) print " Matriz T" funciones.imprimeMatriz(T) print " Como A.x = b, en P.A.Pt = L.T.Lt:" return x