def hw3(filename, O, K, d, maxIter, tolerance, p, G): with open(util.getMatrixFile(filename)) as file: g = Graph.fromFile(file, O) if G: printGraph(g) L = g.getLaplacian() X = plot.formCoordinateVectors(L, d) P, iterations, delta, meta = kMeans(X, K, d, maxIter, tolerance) coarse = formCoarse(P, L) convergence = "(Convergence)" if iterations != maxIter else "" if p: P.visualizeShape() print(coarse) print(f"\nIterations = {iterations} {convergence}") for i in range(len(meta)): print(f"|A{i + 1}| = {meta[i]}") print() if d == 2 or d == 3: plot.visualize(X, P, d)
def hw6(filename, O, K, m, maxIter, tolerance, p): with open(util.getMatrixFile(filename)) as file: G = Graph.fromFile(file, O) L = G.getLaplacian() W = constructW(L, m) P, iterations, delta, meta = kMeans(W, K, m, maxIter, tolerance) if m == 2 or m == 3: plot.visualize(W, P, 2)
def hw1(fileName, O, display): with open(util.getMatrixFile(fileName)) as file: A = Dense.fromFile(file, O) # Convert matrix to np array so I can use scipy factorization A = np.array(A.data) # Get LDLt factorization start = time.time() L, D, P = linalg.ldl(A) U = np.transpose(L) end = time.time() print(f"\nTime to factor input matrix was {end - start} seconds") # Convert to CSR format used in my library L = Sparse.fromDense(Dense(L.shape[0], L.shape[1], L.tolist())) U = Sparse.fromDense(Dense(U.shape[0], U.shape[1], U.tolist())) # Generate random solution vector x = Vector.fromRandom(L.columns, 0, 5) # Resulting b matrix times x vector bL = L.multVec(x) bU = U.multVec(x) # Solve lower triangular system start = time.time() r1 = forwardSparse(L, bL) end = time.time() print(f"Time to solve lower system was {end - start} seconds") # Solve upper triangular system start = time.time() r2 = backwardSparse(U, bU) end = time.time() print(f"Time to solve upper system was {end - start} seconds\n") if display: print("Solution to lower triangular") util.compareVectors(x, r1) print("\nSolution to upper triangular") util.compareVectors(x, r2)
def hw5(filename, O, maxIter, tolerance, preconditioner, M, display, displayResidual): with open(util.getMatrixFile(filename)) as file: A = Sparse.fromFile(file, O) if preconditioner.lower() == "d": method = "Diagonal" preconditioner = diagonalPreconditioner(A) elif preconditioner.lower() == "sgs": method = "Symmetric Gauss Seidel" preconditioner = sgsPreconditioner(A) elif preconditioner.lower() == "2l": method = "Symmetric Two Level" if M == "l1": M = l1Solver(A) method += " (L1 Smoother)" elif M == "fgs": M = fgsSolver(A) method += " (Forward Gauss Seidel)" preconditioner = twolevelPreconditioner(A, M) else: raise Exception("No valid preconditioner selected") # Generate random solution vector x = Vector.fromRandom(A.columns, 0, 5) b = A.multVec(x) # Generate first iteration xInit = Vector(A.columns) start = time.time() xResult, iterations, residual = pcg(A, b, xInit, maxIter, tolerance, preconditioner, displayResidual) end = time.time() convergence = "(Convergence)" if maxIter != iterations else "" if display: util.compareVectors(x, xResult) print(f"\nPreconditioner = {method}") print(f"Iterations = {iterations} {convergence}") print(f"Residual = {residual}") print(f"Time to solve was {end - start}\n")
def hw2(fileName, O, maxIter, tolerance, iterMatrix, display, displayResidual): with open(util.getMatrixFile(fileName)) as file: A = Sparse.fromFile(file, O) # Generate random solution vector x = Vector.fromRandom(A.columns, 0, 5) b = A.multVec(x) # Generate first iteration xInit = Vector(A.columns) # Get proper iteration matrix if iterMatrix.lower() == "l1": method = "l1 Smoother" iterSolver = l1Solver(A) elif iterMatrix.lower() == "fgs": method = "Forward Gauss Seidel" iterSolver = fgsSolver(A) elif iterMatrix.lower() == "bgs": method = "Backward Gauss Seidel" iterSolver = bgsSolver(A) elif iterMatrix.lower() == "sgs": method = "Symmetric Gauss Seidel" iterSolver = sgsSolver(A) else: raise Exception("No valid iteration matrix selected") # Solve system using stationary iterative method start = time.time() xResult, iterations, residual, accuracy = stationaryIterative( A, b, xInit, maxIter, tolerance, iterSolver, displayResidual) end = time.time() convergence = "(Convergence)" if maxIter != iterations else "" if display: util.compareVectors(x, xResult) print(f"\nB Matrix = {method}") print(f"Iterations = {iterations} {convergence}") print(f"Residual = {residual}") print(f"Accuracy = {accuracy}") print(f"Time to solve was {end - start}\n")
def recursive(filename, O, tau, modularity, printResult, visualize): with open(util.getMatrixFile(filename)) as file: g = Graph.fromFile(file, O) As, Ps = recursiveLubys(g, tau, modularity) Pk = formVertexToK1Aggregate(Ps, len(Ps)) Q = QModularity(As[0], Pk) if printResult: Pk.visualizeShape() print(As[-1]) print(f"\nClusters = {Pk.columns}") print(f"Levels = {len(As)}") print(f"Q = {Q}\n") if visualize: plot.visualizeGraph(g.edgeVertex, Pk)
def single(filename, O, modularity, printResult, visualize): with open(util.getMatrixFile(filename)) as file: g = Graph.fromFile(file, O) if modularity: w = modularityWeights(g.adjacency, g.edgeVertex) else: w = randomWeights(g.edgeVertex) P = lubys(g.edgeVertex, w) coarse = formCoarse(P, g.adjacency) Q = QModularity(g.adjacency, P) if printResult: P.visualizeShape() print(coarse) print(f"\nClusters = {P.columns}") print(f"Q = {Q}\n") if visualize: plot.visualizeGraph(g.edgeVertex, P, w.data)
def main(): with open(util.getMatrixFile("25.mtx")) as file: A = Sparse.fromFile(file) A.visualizeShape()