def lup(A): n = len(A) LU = [[float(A[i][j]) for j in range(n)] for i in range(n)] pi = range(n) for k in range(0, n): printmat(LU, perm=pi) p = 0 for i in range(k, n): if abs(LU[i][k]) > p: p = abs(LU[i][k]) k_prime = i if p == 0: raise Exception('Error singular matrix') pi[k], pi[k_prime] = pi[k_prime], pi[k] LU[k], LU[k_prime] = LU[k_prime], LU[k] for i in range(k + 1, n): LU[i][k] /= LU[k][k] for j in range(k + 1, n): LU[i][j] -= LU[i][k] * LU[k][j] printmat(LU, perm=pi) return LU, pi, 'solved'
def lup(A): """ >>> lup([[2, 0, 2, 0.6], [3, 3, 4, -2], [5, 5, 4, 2], [-1, -2, 3.4, -1]]) ([[5.0, 5.0, 4.0, 2.0], [0.4, -2.0, 0.3999999999999999, -0.20000000000000007], [-0.2, 0.5, 4.0, -0.49999999999999994], [0.6, -0.0, 0.4, -3.0]], [2, 0, 3, 1], 'solved') """ n = len(A) LU = [[float(A[i][j]) for j in range(n)] for i in range(n)] pi = range(n) # TODO Implement (psueodocode is in the book) for i in range(n): pi[i] = i for k in range(n): printmat(LU, perm=pi) p = 0 for i in range(k, n): if abs(LU[i][k]) > p: p = abs(LU[i][k]) ki = i if p == 0: print "error" temp = pi[k] pi[k] = pi[ki] pi[ki] = temp for i in range(n): temp1 = LU[k][i] LU[k][i] = LU[ki][i] LU[ki][i] = temp1 for i in range(k + 1, n): LU[i][k] /= LU[k][k] for j in range(k + 1, n): LU[i][j] -= (LU[i][k] * LU[k][j]) printmat(LU, perm=pi) return LU, pi, 'solved'
def lup(A): n = len(A) LU = [[float(A[i][j]) for j in range(n)] for i in range(n)] pi = range(n) result = "solved" for k in range(n): printmat(LU, perm=pi) p = 0 for i in range(k, n): if abs(LU[i][k]) > p: p = abs(LU[i][k]) k1 = i if p == 0: result = "Error : Singular matrix" temp = pi[k] pi[k] = pi[k1] pi[k1] = temp for i in range(n): temp = LU[k][i] LU[k][i] = LU[k1][i] LU[k1][i] = temp for i in range(k + 1, n): LU[i][k] = LU[i][k] / LU[k][k] for j in range(k + 1, n): LU[i][j] = LU[i][j] - LU[i][k] * LU[k][j] return LU, pi, result
def lup(A): """ >>> A = [[2, 0, 2, 0.6], [3, 3, 4, -2], [5, 5, 4, 2], [-1, -2, 3.4, -1]] >>> lup(A) ([[5.0, 5.0, 4.0, 2.0], [0.4, -2.0, 0.3999999999999999, -0.20000000000000007], [-0.2, 0.5, 4.0, -0.49999999999999994], [0.6, -0.0, 0.4, -3.0]], [2, 0, 3, 1], 'solved') """ n = len(A) LU = [[float(A[i][j]) for j in range(n)] for i in range(n)] pi = range(n) for k in range(0, n): printmat(LU, perm=pi) p = 0 for i in range(k, n): if abs(LU[i][k]) > p: p = abs(LU[i][k]) k1 = i if p == 0: raise Exception('SINGULAR MATRIX ERROR') pi[k], pi[k1] = pi[k1], pi[k] LU[k], LU[k1] = LU[k1], LU[k] for i in range(k + 1, n): LU[i][k] = LU[i][k] / LU[k][k] for j in range(k + 1, n): LU[i][j] = LU[i][j] - (LU[i][k] * LU[k][j]) printmat(LU, perm=pi) return LU, pi, 'solved'
def lu(A): """ LUP method to find combined upper and lower method. Parameters ---------- A: int Matrix to operate the lup method on Returns ------- LU: upper triangular and lower triangular matrix which is the solution of the given LU matrix pi: The permutation for an identity matrix >>> B = [[2,3],[3,1]] >>> lu(B) ([[2, 3], [1, -2]], 'solved') """ n = len(A) LU = [[A[i][j] for j in range(n)] for i in range(n)] for k in range(0, n): printmat(LU) for i in range(k + 1, n): LU[i][k] = LU[i][k] / LU[k][k] for i in range(k + 1, n): for j in range(k + 1, n): LU[i][j] = LU[i][j] - LU[i][k] * LU[k][j] printmat(LU) return LU, 'solved'
def lup(A): """ >>> A = [[2, 0, 2, 0.6], [3, 3, 4, -2], [5, 5, 4, 2], [-1, -2, 3.4, -1]] >>> lup(A) ([[5.0, 5.0, 4.0, 2.0], [0.4, -2.0, 0.3999999999999999, -0.20000000000000007], [-0.2, 0.5, 4.0, -0.49999999999999994], [0.6, -0.0, 0.4, -3.0]], [2, 0, 3, 1], 'solved') """ n = len(A) LU = [[float(A[i][j]) for j in range(n)] for i in range(n)] pi = range(n) for k in range(n): printmat(LU, perm=pi) p = 0 for i in range(k, n): if abs(LU[i][k]) > p: p = abs(LU[i][k]) q = i if p == 0: raise Exception("Error Singular Matrix") temp = pi[k] pi[k] = pi[q] pi[q] = temp temp = LU[k] LU[k] = LU[q] LU[q] = temp for i in range(k + 1, n): LU[i][k] /= LU[k][k] for j in range(k + 1, n): LU[i][j] -= LU[i][k] * LU[k][j] printmat(LU, perm=pi) return LU, pi, 'solved'
def lu(A): n = len(A) LU = [[A[i][j] for j in range(n)] for i in range(n)] for k in range(n): printmat(LU) for i in range(k + 1, n): LU[i][k] = LU[i][k] / LU[k][k] for i in range(k + 1, n): for j in range(k + 1, n): LU[i][j] = LU[i][j] - (LU[i][k] * LU[k][j]) return LU, 'solved'
def lup(A): """ LUP method to find combined upper and lower method. Parameters ---------- A: int Matrix to operate the lup method on Returns ------- LU: upper triangular and lower triangular matrix which is the solution of the given LU matrix pi: The permutation for an identity matrix >>> A =[[2,0,2,.6],[3,3,4,-2],[5,5,4,2],[-1,-2,3.4,-1]] >>> lup(A) ([[5.0, 5.0, 4.0, 2.0], [0.4, -2.0, 0.3999999999999999,\ -0.20000000000000007], [-0.2, 0.5, 4.0, -0.49999999999999994],\ [0.6, -0.0, 0.4, -3.0]], [2, 0, 3, 1], 'solved') """ n = len(A) LU = [[float(A[i][j]) for j in range(n)] for i in range(n)] pi = range(n) temp = [] for i in range(0, n): pi[i] = i for k in range(0, n): p = 0 for i in range(k, n): if abs(LU[i][k]) > p: p = abs(LU[i][k]) kp = i if p == 0: print"The matrix is singular" return printmat(LU, perm=pi) temp = LU[k] LU[k] = LU[kp] LU[kp] = temp temp = pi[k] pi[k] = pi[kp] pi[kp] = temp printmat(LU, perm=pi) for i in range(k + 1, n): LU[i][k] = LU[i][k] / LU[k][k] for j in range(k + 1, n): LU[i][j] = LU[i][j] - LU[i][k] * LU[k][j] return LU, pi, 'solved'
def check_lu(): A = LU_MATRIX print "===========================" print "Submitted by ", NAME print "LU:" print "Input:" printmat(A) print "Steps:" LU, result = lu(A) print "---------------------------" print "Output:" printmat(LU) print 'result = ', result print "---------------------------"
def lu(A): n = len(A) LU = [[float(A[i][j]) for j in range(n)] for i in range(n)] for k in range(0, n): printmat(LU) for i in range(k + 1, n): LU[i][k] /= LU[k][k] for i in range(k + 1, n): for j in range(k + 1, n): LU[i][j] -= LU[i][k] * LU[k][j] printmat(LU) return LU, 'solved'
def check_simplex(): A = SIMPLEX_MATRIX print "===========================" print "Submitted by ", NAME print "SIMPLEX:" print "Input:" printmat(A) print "Steps:" SIMP, x, result = simplex(A) print "---------------------------" print "Output:" printmat(SIMP) print "result = ", result print " z = ", x[0] print " x = ", x[1:] print "---------------------------"
def lu(A): """ >>> A = [[4, -5, 6], [8, -6, 7], [12, -7, 12]] >>> lu(A) ([[4, -5, 6], [2, 4, -5], [3, 2, 4]], 'solved') """ n = len(A) LU = [[A[i][j] for j in range(n)] for i in range(n)] for k in range(0, n): printmat(LU) for i in range(k + 1, n): LU[i][k] = LU[i][k] / LU[k][k] for i in range(k + 1, n): for j in range(k + 1, n): LU[i][j] = LU[i][j] - LU[i][k] * LU[k][j] printmat(LU) return LU, 'solved'
def lu(A): """ >>> lu([[4, -5, 6], [8, -6, 7], [12, -7, 12]]) ([[4, -5, 6], [2, 4, -5], [3, 2, 4]], 'solved') """ n = len(A) LU = [[A[i][j] for j in range(n)] for i in range(n)] # TODO Implement (psueodocode is in the book) for k in range(n): printmat(LU) for i in range(k + 1, n): LU[i][k] /= LU[k][k] for i in range(k + 1, n): for j in range(k + 1, n): LU[i][j] -= LU[i][k] * LU[k][j] printmat(LU) return LU, 'solved'
def check_lupsolve(): pi = LUPSOLVE_P LU = LUPSOLVE_MATRIX b = LUPSOLVE_B print "===========================" print "Submitted by ", NAME print "LUP-SOLVE:" print "---------------------------" print "Input:" printmat(LU, pi) print "RHS:" print b print "---------------------------" x = lupsolve(LU, pi, b) print "x = [" + ', '.join(['%3.1f' % z for z in x]) + ']' print "---------------------------"
def check_lup(): A = LUP_MATRIX print "===========================" print "Submitted by ", NAME print "LUP:" print "---------------------------" print "Input:" printmat(A) print "---------------------------" print "Steps:" LUP, p, result = lup(A) print "---------------------------" print "Output:" printmat(LUP, perm=p) print "---------------------------" print "result = ", result print "---------------------------"
def lu(A): """ >>> A = [[4, -5, 6], [8, -6, 7], [12, -7, 12]] >>> lu(A) ([[4, -5, 6], [2, 4, -5], [3, 2, 4]], 'solved') """ n = len(A) LU = [[A[i][j] for j in range(n)] for i in range(n)] for i in range(0, n): printmat(LU) for j in range(i + 1, n): LU[j][i] /= LU[i][i] for k in range(i + 1, n): for l in range(i + 1, n): LU[k][l] = LU[k][l] - (LU[k][i] * LU[i][l]) printmat(LU) return LU, 'solved'
def simplex(A): # Assume that the input is already put into a table. # Assume that the input is feasible already (no need to check) m = len(A) n = len(A[0]) x = [0] * (n - 1) wiggle = [0] * m while min(A[0][1:-1]) < 0: # find entering variable col = A[0].index(min(A[0][1:-1])) min_wiggle = float('inf') row = None for i in range(1, m): if A[i][col] > 0: wiggle = A[i][-1] / float(A[i][col]) if wiggle < min_wiggle: min_wiggle = wiggle row = i if row is None: return A, x, 'unbounded' printmat(A, pos=(row, col), row=row, col=col) A = pivot(A, row, col) printmat(A, pos=(row, col), row=row, col=col) # Look for the basic variables and copy into x for j in range(0, n - 1): colvec = [A[i][j] for i in range(m)] # copy out the column # Check if colvec is a column of the identity matrix. if colvec.count(0.0) == m - 1 and colvec.count(1.0) == 1: x[j] = A[colvec.index(1)][-1] else: x[j] = 0 return A, x, 'solved'