def test_copy(self): mat = SquareMatrix.copy(self.matF1) self.assertTrue(mat == self.matF1) mat = SquareMatrix.copy(self.matF5) self.assertTrue(mat == self.matF5)
def test_creation_from_list(self): mat= SquareMatrix.from_list(self.lsF1size[0],self.lsF1) self.is_matrix_equal(self.matF1, mat) mat= SquareMatrix.from_list(self.lsF2size[0] ,self.lsF2) self.is_matrix_equal(self.matF2, mat)
def test_creation_from_list(self): mat = SquareMatrix.from_list(self.lsF1size[0], self.lsF1) self.is_matrix_equal(self.matF1, mat) mat = SquareMatrix.from_list(self.lsF2size[0], self.lsF2) self.is_matrix_equal(self.matF2, mat)
def setUp(self): #setting up some fixtures self.lsF1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] self.lsF1size = (4, 4) self.lsF2 = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4] self.lsF2size = (4, 4) self.lsF3 = [3, 1, 4, 5, 2, 3, 2, 1, 2] self.lsF3size = (3, 3) self.lsF4 = [i for i in range(6)] self.lsF4size = (3, 2) self.lsF5 = [i for i in range(12)] self.lsF5size = (3, 4) self.lsF6 = [4, 6, 2, 3, 8, 1, 0, 5, 1] self.lsF6size = (3, 3) self.lsF7 = [1, 3, 5, 7, 0, 0, 1, 2, 2, 6, 8, 8] self.lsF7size = (4, 3) self.lsF8 = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] self.lsF8size = (4, 4) self.lsF9 = [1, 0, 0, 0, 1, 2, 0, 0, 1, 2, 3, 0, 1, 2, 3, 4] self.lsF9size = (4, 4) self.lsF10 = [3, 0, 0, 5, 2, 0, 2, 1, 2] self.lsF10size = (3, 3) self.lsF11 = [1, 2, 3, 4, 0, 2, 3, 4, 0, 0, 3, 4, 0, 0, 0, 4] self.lsF11size = (4, 4) self.lsF12 = [3, 1, 4, 0, 2, 3, 0, 0, 2] self.lsF12size = (3, 3) self.matF1 = Matrix.from_list(self.lsF1size[0], self.lsF1size[1], self.lsF1) self.matF2 = Matrix.from_list(self.lsF2size[0], self.lsF2size[1], self.lsF2) self.matF3 = Matrix.from_list(self.lsF3size[0], self.lsF3size[1], self.lsF3) self.matF4 = Matrix.from_list(self.lsF4size[0], self.lsF4size[1], self.lsF4) self.matF5 = Matrix.from_list(self.lsF5size[0], self.lsF5size[1], self.lsF5) self.matF6 = Matrix.from_list(self.lsF6size[0], self.lsF6size[1], self.lsF6) self.matF7 = Matrix.from_list(self.lsF7size[0], self.lsF7size[1], self.lsF7) self.matF8 = Matrix.from_list(self.lsF8size[0], self.lsF8size[1], self.lsF8) self.matF9 = SquareMatrix.from_list(self.lsF9size[0], self.lsF9) self.matF10 = SquareMatrix.from_list(self.lsF10size[0], self.lsF10) self.matF11 = SquareMatrix.from_list(self.lsF11size[0], self.lsF11) self.matF12 = SquareMatrix.from_list(self.lsF12size[0], self.lsF12)
def test_set_lower_triangular(self): mat = SquareMatrix.copy(self.matF2) mat.set_lower_triangular() self.is_matrix_equal_to_list(mat, self.lsF11) mat = SquareMatrix.copy(self.matF3) mat.set_lower_triangular() self.is_matrix_equal_to_list(mat, self.lsF12)
def test_set_lower_symmetry(self): mat = SquareMatrix.copy(self.matF1) mat.set_lower_simmetry() self.is_matrix_equal_to_list(mat, self.lsF1) mat = SquareMatrix.copy(self.matF2) mat.set_lower_simmetry() self.is_matrix_equal_to_list(mat, [1,1,1,1,1,2,2,2,1,2,3,3,1,2,3,4])
def test_set_transpose(self): mat = SquareMatrix.copy(self.matF1) mat.set_transpose() self.is_matrix_equal_to_list(mat, self.lsF1) mat = SquareMatrix.copy(self.matF2) mat.set_transpose() self.is_matrix_equal_to_list(mat, [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4])
def test_set_unit_lower_triangular(self): mat = SquareMatrix.copy(self.matF2) mat.set_unit_lower_triangular() self.is_matrix_equal_to_list(mat, [1,2,3,4, 0,1,3,4,0,0,1,4,0,0,0,1]) mat = SquareMatrix.copy(self.matF3) mat.set_unit_lower_triangular() self.is_matrix_equal_to_list(mat, [1,1,4,0,1,3,0,0,1])
def test_set_strictly_lower_triangular(self): mat = SquareMatrix.copy(self.matF2) mat.set_strictly_lower_triangular() self.is_matrix_equal_to_list(mat, [0,2,3,4, 0,0,3,4,0,0,0,4,0,0,0,0]) mat = SquareMatrix.copy(self.matF3) mat.set_strictly_lower_triangular() self.is_matrix_equal_to_list(mat, [0,1,4,0,0,3,0,0,0])
def test_identity(self): mat = SquareMatrix.from_list(self.lsF1size[0], self.lsF1) self.is_matrix_equal_to_list(mat, self.lsF1) mat = SquareMatrix.from_list(self.lsF2size[0], self.lsF2) self.is_matrix_equal_to_list(mat, self.lsF2) mat = SquareMatrix.from_list(self.lsF3size[0], self.lsF3) self.is_matrix_equal_to_list(mat, self.lsF3)
def test_set_unit_lower_triangular(self): mat = SquareMatrix.copy(self.matF2) mat.set_unit_lower_triangular() self.is_matrix_equal_to_list( mat, [1, 2, 3, 4, 0, 1, 3, 4, 0, 0, 1, 4, 0, 0, 0, 1]) mat = SquareMatrix.copy(self.matF3) mat.set_unit_lower_triangular() self.is_matrix_equal_to_list(mat, [1, 1, 4, 0, 1, 3, 0, 0, 1])
def test_set_lower_symmetry(self): mat = SquareMatrix.copy(self.matF1) mat.set_lower_simmetry() self.is_matrix_equal_to_list(mat, self.lsF1) mat = SquareMatrix.copy(self.matF2) mat.set_lower_simmetry() self.is_matrix_equal_to_list( mat, [1, 1, 1, 1, 1, 2, 2, 2, 1, 2, 3, 3, 1, 2, 3, 4])
def test_set_strictly_lower_triangular(self): mat = SquareMatrix.copy(self.matF2) mat.set_strictly_lower_triangular() self.is_matrix_equal_to_list( mat, [0, 2, 3, 4, 0, 0, 3, 4, 0, 0, 0, 4, 0, 0, 0, 0]) mat = SquareMatrix.copy(self.matF3) mat.set_strictly_lower_triangular() self.is_matrix_equal_to_list(mat, [0, 1, 4, 0, 0, 3, 0, 0, 0])
def test_identity(self): mat = SquareMatrix.from_list(self.lsF1size[0], self.lsF1) self.is_matrix_equal_to_list(mat , self.lsF1) mat = SquareMatrix.from_list(self.lsF2size[0], self.lsF2) self.is_matrix_equal_to_list(mat , self.lsF2) mat = SquareMatrix.from_list(self.lsF3size[0], self.lsF3) self.is_matrix_equal_to_list(mat , self.lsF3)
def test_set_transpose(self): mat = SquareMatrix.copy(self.matF1) mat.set_transpose() self.is_matrix_equal_to_list(mat, self.lsF1) mat = SquareMatrix.copy(self.matF2) mat.set_transpose() self.is_matrix_equal_to_list( mat, [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4])
def test_creation(self): mat = SquareMatrix(4, None, True) self.is_matrix_equal(self.matF8, mat) mat = SquareMatrix(4, [ Vector([1, 0, 0, 0]), Vector([0, 1, 0, 0]), Vector([0, 0, 1, 0]), Vector([0, 0, 0, 1]) ], False) self.is_matrix_equal(self.matF8, mat)
def test_lu_factorization(self): mat = SquareMatrix.from_list(3, [2, 4, 6, 4, -2, -4, -2, 6, 2]) L, U = mat.lu_factorization() self.is_matrix_equal_to_matrix(L.mult_matrix_update_by_column(U), mat) #ensuring A - (LU) = zero matrix mat.set_add(-(L.mult_matrix_update_by_column(U))) self.is_matrix_equal_to_matrix(mat, SquareMatrix.copy(self.matF1)) mat = SquareMatrix.from_list(3, [1, 5, -2, -2, -15, -11, 2, 8, -11]) L, U = mat.lu_factorization() self.is_matrix_equal_to_list(L, [1, 5, -2, 0, 1, 3, 0, 0, 1]) self.is_matrix_equal_to_list(U, [1, 0, 0, -2, -5, 0, 2, -2, -1])
def test_solve_lower_triangular_system(self): mat = SquareMatrix.from_list(3, [2, 4, 6, 4, -2, -4, -2, 6, 2]) L, U = mat.lu_factorization() vec = Vector([-10, 20, 18]) res = L.solve_lower_triangular_system(vec) self.is_vector_equal_to_list(res, [-10, 40, -16])
def test_solve_linear_system(self): mat = SquareMatrix.from_list(4, [2,-2,4,-4,0,-1,-1,1,1,1,5,-3,2,-1,4,-8]) vec = Vector([2,2,11,-3]) res = mat.solve_linear_system(vec) self.is_vector_equal_to_list(res,[1,-1,2,-1])
def test_gaussian_appended_reduction_upper_triangular(self): mat = SquareMatrix.from_list(3, [2,4,6,4, -2,-4,-2,6,2]) res = mat.gaussian_reduction_upper_triangular(True) vec = Vector([-10,20,18]) res= res.gaussian_appended_reduction_upper_triangular( vec) self.is_vector_equal_to_list(res, [-10,40,-16])
def test_solve_lower_triangular_system(self): mat = SquareMatrix.from_list(3, [2,4,6,4, -2,-4,-2,6,2]) L, U = mat.lu_factorization() vec = Vector([-10,20,18]) res= L.solve_lower_triangular_system( vec) self.is_vector_equal_to_list(res, [-10,40,-16])
def test_lu_factorization_compact(self): mat = SquareMatrix.from_list(3, [2,4,6,4, -2,-4,-2,6,2]) lu_res = mat.lu_factorization_compact() gres = mat.gaussian_reduction_upper_triangular(True) self.is_matrix_equal_to_list(lu_res, [2,2,3,4,-10,1.6,-2,10,-8]) self.is_matrix_equal_to_matrix(lu_res,gres)
def test_solve_linear_system(self): mat = SquareMatrix.from_list( 4, [2, -2, 4, -4, 0, -1, -1, 1, 1, 1, 5, -3, 2, -1, 4, -8]) vec = Vector([2, 2, 11, -3]) res = mat.solve_linear_system(vec) self.is_vector_equal_to_list(res, [1, -1, 2, -1])
def test_gaussian_appended_reduction_upper_triangular(self): mat = SquareMatrix.from_list(3, [2, 4, 6, 4, -2, -4, -2, 6, 2]) res = mat.gaussian_reduction_upper_triangular(True) vec = Vector([-10, 20, 18]) res = res.gaussian_appended_reduction_upper_triangular(vec) self.is_vector_equal_to_list(res, [-10, 40, -16])
def test_gaussian_reduction_upper_triangular(self): mat = SquareMatrix.from_list(3, [2, 4, 6, 4, -2, -4, -2, 6, 2]) res = mat.gaussian_reduction_upper_triangular() self.is_matrix_equal_to_list(res, [2, 0, 0, 4, -10, 0, -2, 10, -8]) res = mat.gaussian_reduction_upper_triangular(True) self.is_matrix_equal_to_list(res, [2, 2, 3, 4, -10, 1.6, -2, 10, -8]) vec = Vector([-10, 20, 18]) res = mat.gaussian_reduction_upper_triangular(True, vec) self.is_matrix_equal_to_list(res, [2, 2, 3, 4, -10, 1.6, -2, 10, -8]) self.is_vector_equal_to_list(vec, [-10, 40, -16]) mat = SquareMatrix.from_list(3, [1, -3, 1, 1, 0, 13, 2, -3, 20]) res = mat.gaussian_reduction_upper_triangular() self.is_matrix_equal_to_list(res, [1, 0, 0, 1, 3, 0, 2, 3, 6])
def test_solve_upper_triangular_system(self): mat = SquareMatrix.from_list(3, [-2, 0, 0, -1, -3, 0, 1, -2, 1]) vec = Vector([6, 9, 3]) it = vec.vector_iterator(True) res = mat.solve_upper_triangular_system(vec) self.is_vector_equal_to_list(res, [1, -5, 3])
def test_gaussian_reduction_upper_triangular(self): mat = SquareMatrix.from_list(3, [2,4,6,4, -2,-4,-2,6,2]) res = mat.gaussian_reduction_upper_triangular() self.is_matrix_equal_to_list(res, [2,0,0,4,-10,0,-2,10,-8]) res = mat.gaussian_reduction_upper_triangular(True) self.is_matrix_equal_to_list(res, [2,2,3,4,-10,1.6,-2,10,-8]) vec = Vector([-10,20,18]) res = mat.gaussian_reduction_upper_triangular(True, vec) self.is_matrix_equal_to_list(res, [2,2,3,4,-10,1.6,-2,10,-8]) self.is_vector_equal_to_list(vec, [-10,40,-16]) mat = SquareMatrix.from_list(3, [1,-3,1,1,0,13,2,-3,20]) res = mat.gaussian_reduction_upper_triangular() self.is_matrix_equal_to_list(res, [1,0,0,1,3,0,2,3,6])
def test_lu_factorization_compact(self): mat = SquareMatrix.from_list(3, [2, 4, 6, 4, -2, -4, -2, 6, 2]) lu_res = mat.lu_factorization_compact() gres = mat.gaussian_reduction_upper_triangular(True) self.is_matrix_equal_to_list(lu_res, [2, 2, 3, 4, -10, 1.6, -2, 10, -8]) self.is_matrix_equal_to_matrix(lu_res, gres)
def test_lu_factorization(self): mat = SquareMatrix.from_list(3, [2,4,6,4, -2,-4,-2,6,2]) L, U = mat.lu_factorization() self.is_matrix_equal_to_matrix(L.mult_matrix_update_by_column(U),mat) #ensuring A - (LU) = zero matrix mat.set_add (-(L.mult_matrix_update_by_column(U))) self.is_matrix_equal_to_matrix(mat, SquareMatrix.copy(self.matF1)) mat = SquareMatrix.from_list(3,[1,5,-2,-2,-15,-11,2,8,-11]) L,U = mat.lu_factorization() self.is_matrix_equal_to_list(L, [1,5,-2,0,1,3,0,0,1]) self.is_matrix_equal_to_list(U, [1,0,0,-2,-5,0,2,-2,-1])
def test_solve_upper_triangular_system(self): mat = SquareMatrix.from_list(3, [-2,0,0,-1,-3,0,1,-2,1]) vec = Vector([6,9,3]) it = vec.vector_iterator(True) res = mat.solve_upper_triangular_system(vec) self.is_vector_equal_to_list(res,[1,-5,3])
def setUp(self): #setting up some fixtures self.lsF1 = [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0] self.lsF1size = (4,4) self.lsF2 = [1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4] self.lsF2size = (4,4) self.lsF3 = [3,1,4, 5,2,3, 2,1,2] self.lsF3size = (3,3) self.lsF4 = [ i for i in range(6)] self.lsF4size = (3,2) self.lsF5 = [ i for i in range(12)] self.lsF5size = (3,4) self.lsF6 = [4,6,2, 3,8,1, 0,5,1] self.lsF6size= (3,3) self.lsF7 = [1,3,5,7, 0,0,1,2, 2,6,8,8] self.lsF7size = (4,3) self.lsF8 = [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1] self.lsF8size = (4,4) self.lsF9 = [1,0,0,0, 1,2,0,0, 1,2,3,0, 1,2,3,4] self.lsF9size = (4,4) self.lsF10 = [3,0,0, 5,2,0, 2,1,2] self.lsF10size = (3,3) self.lsF11 = [1,2,3,4, 0,2,3,4, 0,0,3,4, 0,0,0,4] self.lsF11size = (4,4) self.lsF12 = [3,1,4, 0,2,3, 0,0,2] self.lsF12size = (3,3) self.matF1 = Matrix.from_list(self.lsF1size[0], self.lsF1size[1],self.lsF1) self.matF2 = Matrix.from_list(self.lsF2size[0], self.lsF2size[1],self.lsF2) self.matF3 = Matrix.from_list(self.lsF3size[0], self.lsF3size[1],self.lsF3) self.matF4 = Matrix.from_list(self.lsF4size[0], self.lsF4size[1],self.lsF4) self.matF5 = Matrix.from_list(self.lsF5size[0], self.lsF5size[1],self.lsF5) self.matF6 = Matrix.from_list(self.lsF6size[0], self.lsF6size[1],self.lsF6) self.matF7 = Matrix.from_list(self.lsF7size[0], self.lsF7size[1],self.lsF7) self.matF8 = Matrix.from_list(self.lsF8size[0], self.lsF8size[1],self.lsF8) self.matF9 = SquareMatrix.from_list(self.lsF9size[0] ,self.lsF9) self.matF10 = SquareMatrix.from_list(self.lsF10size[0],self.lsF10) self.matF11 = SquareMatrix.from_list(self.lsF11size[0],self.lsF11) self.matF12 = SquareMatrix.from_list(self.lsF12size[0],self.lsF12)
def test_set_to_diagonal(self): self.matF2 = SquareMatrix.copy(self.matF2) self.matF2.set_to_diagonal([1,1,1,1]) self.is_matrix_equal_to_list(self.matF2,self.lsF8)
def test_set_to_diagonal(self): self.matF2 = SquareMatrix.copy(self.matF2) self.matF2.set_to_diagonal([1, 1, 1, 1]) self.is_matrix_equal_to_list(self.matF2, self.lsF8)