Exemplo n.º 1
0
    def setUp(self):
        self.A = [[1, 2, 0, 0, 3], [4, 5, 6, 0, 0], [0, 7, 8, 0, 9],
                  [0, 0, 0, 10, 0], [11, 0, 0, 0, 12]]

        self.x = [[5], [4], [3], [2], [1]]

        self.x_full = FullMatrix(5, 1)

        self.A_full = FullMatrix(5, 5)
        self.A_sparse = SparseMatrix(5, 5)

        for i in range(5):
            self.x_full.addElement(i, 0, self.x[i][0])
            for j in range(5):
                self.A_full.addElement(i, j, self.A[i][j])
                self.A_sparse.addElement(i, j, self.A[i][j])
Exemplo n.º 2
0
    def setUp(self):
        self.tolerance = 10**-7
        import pickle
        '''
		Load the memplus matrix in SparseMatrix format from 
		the file "memplus_sparse.bin" as generated and verified
		by the bin_generator.

		Also create the column matrix with rowRank = rowRank of A
		and with all of its entries as 1.0
		'''
        f = open("memplus/memplus_sparse.bin", "rb")
        self.A_sparse = pickle.load(f)
        f.close()
        self.x_full = FullMatrix(self.A_sparse.rowRank, 1)
        for i in range(self.x_full.rowRank):
            self.x_full.addElement(i, 0, 1.0)
Exemplo n.º 3
0
class TestPartII(unittest.TestCase):
    """
	Test suite for part II, when ground truth is not known.
	"""
    def setUp(self):
        self.tolerance = 10**-7
        import pickle
        '''
		Load the memplus matrix in SparseMatrix format from 
		the file "memplus_sparse.bin" as generated and verified
		by the bin_generator.

		Also create the column matrix with rowRank = rowRank of A
		and with all of its entries as 1.0
		'''
        f = open("memplus_sparse.bin", "rb")
        self.A_sparse = pickle.load(f)
        f.close()
        self.x_full = FullMatrix(self.A_sparse.rowRank, 1)
        for i in range(self.x_full.rowRank):
            self.x_full.addElement(i, 0, 1.0)

    from memory_profiler import profile
    mem = open("memory_partII.txt", "w+")

    @profile(stream=mem)
    def test_combined(self):
        # import time
        # start = time.time()
        self.A_sparse.rowPermute(0, 2)
        self.A_sparse.rowPermute(0, 4)
        self.A_sparse.rowPermute(9, 2999)
        self.A_sparse.rowPermute(4999, 9999)
        self.A_sparse.rowPermute(5, 14999)
        self.A_sparse.rowScale(1, 3, 3.0)
        self.A_sparse.rowPermute(1, 4)
        self.A_sparse.rowScale(4, 3, -3.0)
        b = self.A_sparse.productAx(self.x_full)
        # end = time.time()
        # runtime = open("runtime_partII.txt","w+")
        # runtime.write("Total runtime for matrix operations in part II: %.4f seconds\n" % (end-start))
        # runtime.close()
        self.assertTrue(helper_partII(self.A_sparse, b) < self.tolerance)
Exemplo n.º 4
0
    def __init__(self, A, b, x0=0, tol=10**-9, max_iter=10**100):
        """
		Initializes the matrix A, column matrix b, initial guess x0, tolerance,
		and maximum number of iterations max_iter.

		D_inv is the inverse of the diagonal matrix D obtained from the diagonal elements of A.
		R is the matrix obtained from (A - D) which is equivalent to (L+U)

		Db is the product obtained from the matrix multiplication of D_inv and b.

		"""
        self.A = copy.deepcopy(A)

        self.b = b

        self.n = A.colRank

        if x0 == 0:
            self.x0 = FullMatrix(self.n, 1)
        else:
            self.x0 = x0

        self.tol = tol
        self.max_iter = max_iter

        self.D_inv = SparseMatrix(self.n, self.n)

        self.R = copy.deepcopy(A)

        for i in range(self.n):
            aii = A.retrieveElement(i, i)
            self.D_inv.addElement(i, i, 1 / aii)
            self.R.deleteElement(i, i)

        self.Db = self.D_inv.productAx(self.b)

        self.x = False
Exemplo n.º 5
0
Programming Assignment 2
Jacobi Solver Testing
Author: Tejas Advait (TA275)
"""

import math
import numpy as np
from matrix import FullMatrix, SparseMatrix
import pickle
from jacobi import Jacobi_Solver

f = open("mat1/sparse_mat1.bin", "rb")
mat1 = pickle.load(f)
f.close()

b1 = FullMatrix(mat1.rowRank, 1)  #b-vector with 1 in its first row
b2 = FullMatrix(mat1.rowRank, 1)  #b-vector with 1 in its fifth row
b3 = FullMatrix(mat1.rowRank, 1)  #b-vector with 1 in every row

b1.addElement(0, 0, 1.0)
b2.addElement(4, 0, 1.0)
for i in range(b3.rowRank):
    b3.addElement(i, 0, 1.0)

J1 = Jacobi_Solver(mat1, b1)  #System with mat1 and b1
J2 = Jacobi_Solver(mat1, b2)  #System with mat1 and b2
J3 = Jacobi_Solver(mat1, b3)  #System with mat1 and b3

J1.solve()
J2.solve()
J3.solve()
Exemplo n.º 6
0
class WilkinsonTestPartI(unittest.TestCase):
    """
	Test suite for part I, when ground truth is known.
	"""
    def setUp(self):
        self.A = [[1, 2, 0, 0, 3], [4, 5, 6, 0, 0], [0, 7, 8, 0, 9],
                  [0, 0, 0, 10, 0], [11, 0, 0, 0, 12]]

        self.x = [[5], [4], [3], [2], [1]]

        self.x_full = FullMatrix(5, 1)

        self.A_full = FullMatrix(5, 5)
        self.A_sparse = SparseMatrix(5, 5)

        for i in range(5):
            self.x_full.addElement(i, 0, self.x[i][0])
            for j in range(5):
                self.A_full.addElement(i, j, self.A[i][j])
                self.A_sparse.addElement(i, j, self.A[i][j])

        self.A_full.augment(self.x_full)
        self.A_sparse.augment(self.x_full)

    def test_rowPermute(self):
        self.A_full.rowPermute(0, 2)
        self.A_full.rowPermute(0, 4)
        self.A_sparse.rowPermute(0, 2)
        self.A_sparse.rowPermute(0, 4)

        self.assertTrue(norm2(self.A_full, self.A_sparse) == 0.0)

    def test_rowScale(self):
        self.A_full.rowScale(0, 3, 3)
        self.A_full.rowScale(4, 1, -4.4)
        self.A_sparse.rowScale(0, 3, 3)
        self.A_sparse.rowScale(4, 1, -4.4)
        self.assertTrue(norm2(self.A_full, self.A_sparse) == 0.0)

    def test_productAx(self):
        x = self.A_full.deaugment()
        self.A_sparse.deaugment()
        full = self.A_full.productAx(x)
        sparse = self.A_sparse.productAx(x)
        self.assertTrue(norm2(full, sparse) == 0.0)

    def test_combined(self):
        self.A_full.rowPermute(0, 2)
        self.A_full.rowPermute(0, 4)
        self.A_sparse.rowPermute(0, 2)
        self.A_sparse.rowPermute(0, 4)
        self.A_full.rowScale(0, 3, 3)
        self.A_full.rowScale(4, 1, -4.4)
        self.A_sparse.rowScale(0, 3, 3)
        self.A_sparse.rowScale(4, 1, -4.4)

        x = self.A_sparse.deaugment()
        self.A_full.deaugment()

        full = self.A_full.productAx(x)
        sparse = self.A_sparse.productAx(x)
        self.assertTrue(norm2(full, sparse) == 0.0)
Exemplo n.º 7
0
class Direct_Full_Solver:
    """
	An instance is a representation of the linear system to be solved directly
	in the full-matrix format.
	"""
    def __init__(self, A, b):
        """
		Initializes the matrix A, and column matrix b.
		"""

        assert (A.colRank == A.rowRank)
        self.A = copy.deepcopy(A)
        self.b = copy.deepcopy(b)
        self.A_inv = FullMatrix(A.rowRank, A.colRank)
        self.x = FullMatrix(A.rowRank, 1)
        self.det = np.float64(np.linalg.det(A._mat))
        # self.A_inv._mat = np.linalg.inv(A._mat)
        # self.A.augment(self.b)

    def partial_pivot(self):
        """
		Performs partial column pivoting of the matrix A.
		"""

        for i in range(self.A.rowRank):
            index = i + int(self.A._mat[i:, i].argmax())

            if i != index:
                self.A.rowPermute(i, index)

        self.b = self.A.deaugment()

    def minor(self, rowInd, colInd):
        """
		Calculates the minor of the matrix at rowInd and colInd.
		"""
        assert (self.A.rowRank == self.A.colRank)
        sub = np.delete(np.delete(self.A._mat, rowInd, axis=0), colInd, axis=1)
        return np.float64(np.linalg.det(sub))

    def cofactor(self, rowInd, colInd):
        """
		Calculates the cofactor of the matrix at rowInd and colInd.
		"""
        assert (self.A.rowRank == self.A.colRank)
        return (-1)**(rowInd + colInd) * self.minor(rowInd, colInd)

    def invertA(self):
        """
		Inverts the matrix A.
		"""

        assert (self.A.rowRank == self.A.colRank)
        for i in range(self.A.rowRank):
            for j in range(self.A.colRank):
                self.A_inv.addElement(j, i, self.cofactor(i, j) / self.det)

    def solve(self):
        """
		Solves the system and updates x.
		"""
        # self.partial_pivot()
        self.invertA()
        #BACK-SUBSTITUTION CHECK HERE
        #
        #
        self.x = self.A_inv.productAx(self.b)