예제 #1
0
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)
예제 #2
0
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")
예제 #3
0
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")