def test_plu_factorization(self): matrix = Matrix([[1, 5, 4], [3, 6, 4], [8, 2, 3]], fraction=True) permuted, lower, upper = plu_factorization(matrix) self.assertEqual( repr(permuted), "Matrix([['1', '0', '0'], ['0', '1', '0'], ['0', '0', '1']])") self.assertEqual( repr(lower), "Matrix([['1', '0', '0'], ['3', '1', '0'], ['8', '38/9', '1']])") self.assertEqual( repr(upper), "Matrix([['1', '5', '4'], ['0', '-9', '-8'], ['0', '0', '43/9']])") self.assertEqual(repr(lower * upper), repr(permuted * matrix)) matrix = Matrix([[0, 5, 4], [3, 0, 4], [8, 2, 3]], fraction=True) permuted, lower, upper = plu_factorization(matrix) self.assertEqual( repr(permuted), "Matrix([['0', '1', '0'], ['1', '0', '0'], ['0', '0', '1']])") self.assertEqual( repr(lower), "Matrix([['1', '0', '0'], ['0', '1', '0'], ['8/3', '2/5', '1']])") self.assertEqual( repr(upper), "Matrix([['3', '0', '4'], ['0', '5', '4'], ['0', '0', '-139/15']])" ) self.assertEqual(repr(lower * upper), repr(permuted * matrix)) matrix = fill_matrix(2, 3, 1) with self.assertRaises(DimensionError) as c: permuted, lower, upper = plu_factorization(matrix) self.assertTrue("Matrix must be a square matrix." in str(c.exception))
def test_gaussian_elimination(self): matrix = Matrix([[1, 2, 3], [4, 5, 6]], fraction=True) c_matrix, traceback, inverse_traceback = gaussian_elimination(matrix) self.assertEqual(repr(c_matrix), "Matrix([['1', '0', '-1'], ['0', '1', '2']])") self.assertEqual(repr(traceback), "[]") self.assertEqual(repr(inverse_traceback), "[]") matrix = Matrix([[4, 7], [2, 6]], fraction=True) c_matrix, traceback, inverse_traceback = gaussian_elimination( matrix, history=True) self.assertEqual(repr(c_matrix), "Matrix([['1', '0'], ['0', '1']])") self.assertEqual( repr(traceback), "[Matrix([['1/4', '0'], ['0', '1']]), " + "Matrix([['1', '0'], ['-2', '1']]), " + "Matrix([['1', '0'], ['0', '2/5']]), " + "Matrix([['1', '-7/4'], ['0', '1']])]") self.assertEqual(repr(inverse_traceback), "[]") matrix = Matrix([[4, 7], [2, 6]], fraction=True) c_matrix, traceback, inverse_traceback = gaussian_elimination( matrix, inverse_history=True) self.assertEqual(repr(c_matrix), "Matrix([['1', '0'], ['0', '1']])") self.assertEqual(repr(traceback), "[]") self.assertEqual( repr(inverse_traceback), "[Matrix([['4', '0'], ['0', '1']]), " + "Matrix([['1', '0'], ['2', '1']]), " + "Matrix([['1', '0'], ['0', '5/2']]), " + "Matrix([['1', '7/4'], ['0', '1']])]") matrix = Matrix([[0, 0, 0], [3, 2, 1]], fraction=True) c_matrix, traceback, inverse_traceback = gaussian_elimination( matrix, history=True, inverse_history=True) self.assertEqual(repr(c_matrix), "Matrix([['1', '2/3', '1/3'], ['0', '0', '0']])")
def test_inverse(self): matrix = Matrix([[1, 2], [3, 4]], fraction=True) inverted = inverse(matrix) self.assertEqual(repr(inverted), "Matrix([['-2', '1'], ['3/2', '-1/2']])") self.assertEqual(repr(is_identity(matrix * inverted)), "True") matrix = Matrix([[1, 1], [2, 2]], fraction=True) inverted = inverse(matrix) self.assertEqual(repr(inverted), "None")
def test_ref(self): matrix = Matrix([[1, 2, 3], [4, 5, 6]], fraction=True) c_matrix, traceback, inverse_traceback = ref(matrix) self.assertEqual(repr(c_matrix), "Matrix([['1', '2', '3'], ['0', '1', '2']])") self.assertEqual(repr(traceback), "[]") self.assertEqual(repr(inverse_traceback), "[]")
def test_el_matrix_add(self): matrix = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) el_matrix = el_matrix_add(matrix, 1, 2, 2) self.assertEqual(repr(el_matrix), "Matrix([['1', '0', '0'], ['0', '1', '2'], ['0', '0', '1']])") self.assertEqual(repr(el_matrix * matrix), "Matrix([['1', '2', '3'], ['18', '21', '24'], ['7', '8', '9']])")
def test_permute_matrix(self): matrix = Matrix([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) permute = permute_matrix(matrix) self.assertEqual( repr(permute), "Matrix([['1', '0', '0'], ['0', '1', '0'], ['0', '0', '1']])") matrix = Matrix([[0, 1, 1], [1, 2, 1], [2, 7, 9]]) permute = permute_matrix(matrix) self.assertEqual( repr(permute), "Matrix([['0', '1', '0'], ['1', '0', '0'], ['0', '0', '1']])") matrix = fill_matrix(2, 3, 1) with self.assertRaises(DimensionError) as c: permute = permute_matrix(matrix) self.assertTrue("Matrix must be a square matrix." in str(c.exception))
def test_rank(self): matrix = Matrix([[1, 2, 3], [4, 5, 6]], fraction=True) matrix_rank = rank(matrix) self.assertEqual(repr(matrix_rank), "2") matrix = fill_matrix(3, 3, 2) matrix_rank = rank(matrix) self.assertEqual(repr(matrix_rank), "1")
def test_lu_factorization(self): matrix = Matrix([[2, 1], [6, 8]], fraction=True) lower, upper = lu_factorization(matrix) self.assertEqual(repr(lower), "Matrix([['1', '0'], ['3', '1']])") self.assertEqual(repr(upper), "Matrix([['2', '1'], ['0', '5']])") self.assertEqual(repr(lower * upper), repr(matrix)) matrix = fill_matrix(2, 3, 1) with self.assertRaises(DimensionError) as c: lower, upper = lu_factorization(matrix) self.assertTrue("Matrix must be a square matrix." in str(c.exception))
def test_upper_triangular(self): matrix = Matrix([[2, 1], [6, 8]], fraction=True) upper, history, inverse_history = \ upper_triangular(matrix, history=True, inverse_history=True) self.assertEqual(repr(upper), "Matrix([['2', '1'], ['0', '5']])") self.assertEqual(repr(history), "[Matrix([['1', '0'], ['-3', '1']])]") self.assertEqual(repr(inverse_history), "[Matrix([['1', '0'], ['3', '1']])]") matrix = fill_matrix(2, 3, 1) with self.assertRaises(DimensionError) as c: upper = upper_triangular(matrix) self.assertTrue("Matrix must be a square matrix." in str(c.exception))
def test_lower_triangular(self): matrix = Matrix([[2, 1], [6, 8]], fraction=True) lower, history, inverse_history = \ lower_triangular(matrix, history=True, inverse_history=True) self.assertEqual(repr(lower), "Matrix([['5/4', '0'], ['6', '8']])") self.assertEqual(repr(history), "[Matrix([['1', '-1/8'], ['0', '1']])]") self.assertEqual(repr(inverse_history), "[Matrix([['1', '1/8'], ['0', '1']])]") matrix = fill_matrix(2, 3, 1) with self.assertRaises(DimensionError) as c: lower = lower_triangular(matrix) self.assertTrue("Matrix must be a square matrix." in str(c.exception))
def test_ldlt_factorization(self): matrix = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], fraction=True) lower, diag, lower_t = ldlt_factorization(matrix) self.assertEqual(repr(lower), "None") self.assertEqual(repr(diag), "None") self.assertEqual(repr(lower_t), "None") matrix = Matrix([[1, 2, 3], [2, 1, 4], [3, 4, 1]], fraction=True) lower, diag, lower_t = ldlt_factorization(matrix) self.assertEqual( repr(lower), "Matrix([['1', '0', '0'], ['2', '1', '0'], ['3', '2/3', '1']])") self.assertEqual( repr(diag), "Matrix([['1', '0', '0'], ['0', '-3', '0'], ['0', '0', '-20/3']])") self.assertEqual( repr(lower_t), "Matrix([['1', '2', '3'], ['0', '1', '2/3'], ['0', '0', '1']])") self.assertEqual(repr(lower * diag * lower_t), repr(matrix)) matrix = fill_matrix(2, 3, 1) with self.assertRaises(DimensionError) as c: lower, diag, lower_t = ldlt_factorization(matrix) self.assertTrue("Matrix must be a square matrix." in str(c.exception))
def test_ldu_factorization(self): matrix = Matrix([[1, 5, 4], [3, 6, 4], [8, 2, 3]], fraction=True) lower, diag, upper = ldu_factorization(matrix) self.assertEqual( repr(lower), "Matrix([['1', '0', '0'], ['3', '1', '0'], ['8', '38/9', '1']])") self.assertEqual( repr(diag), "Matrix([['1', '0', '0'], ['0', '-9', '0'], ['0', '0', '43/9']])") self.assertEqual( repr(upper), "Matrix([['1', '5', '4'], ['0', '1', '8/9'], ['0', '0', '1']])") self.assertEqual(repr(lower * diag * upper), repr(matrix)) matrix = fill_matrix(2, 3, 1) with self.assertRaises(DimensionError) as c: lower, diag, upper = ldu_factorization(matrix) self.assertTrue("Matrix must be a square matrix." in str(c.exception))