def test_mult_square_2x2_matrix(self): first_matrix = Matrix.make_from_list([[1, 0], [0, 1]]) second_matrix = Matrix.make_from_list([[3, 3], [3, 3]]) result_matrix = MatrixOperations.multiply_matrix( first_matrix, second_matrix) self.assertTrue(result_matrix.data_lines[0][0] == 3 and result_matrix.data_lines[1][1] == 3)
def test_plus_onesize_negative_matrix(self): first_matrix = Matrix.make_from_list([[1, 1]]) second_matrix = Matrix.make_from_list([[-2, 2]]) result_matrix = MatrixOperations.add_matrix(first_matrix, second_matrix) self.assertTrue(result_matrix.data_lines[0][0] == -1 and result_matrix.data_lines[0][1] == 3)
def test_plus_onesize_diff_char_matrix(self): first_matrix = Matrix.make_from_list([[1, 1]]) second_matrix = Matrix.make_from_list([[-1, -1]]) result_matrix = MatrixOperations.add_matrix(first_matrix, second_matrix) self.assertTrue(result_matrix.data_lines[0][0] == 0 and result_matrix.data_lines[0][1] == 0)
def test_plus_3x2_matrix(self): first_matrix = Matrix.make_from_list([[1, 1], [2, 2], [3, 3]]) second_matrix = Matrix.make_from_list([[-1, -1], [-1, -1], [-1, -1]]) result_matrix = MatrixOperations.add_matrix(first_matrix, second_matrix) self.assertTrue(result_matrix.data_lines[0][0] == 0 and result_matrix.data_lines[1][1] == 1 and result_matrix.data_lines[2][1] == 2)
def inverse(cls, matrix): if matrix.calculate_det() == 0: raise MatrixError( 'Matrix has zero determinant! Check your matrix.') minor_matrix = Matrix.make_from_list([[0 for i in range(matrix.cols)] for i in range(matrix.rows)]) for i in range(minor_matrix.rows): for j in range(minor_matrix.cols): minor = MatrixOperations.delete_col_and_row(matrix, i, j) minor_matrix.data_lines[i][j] = minor.calculate_det() * ( (-1)**(i + j)) minor_matrix = MatrixOperations.transpose(minor_matrix) return Matrix.make_from_list( [[elem / matrix.calculate_det() for elem in row] for row in minor_matrix.data_lines])
def test_scalar_1_mult_matrix(self): first_matrix = Matrix.make_from_list([[1, 2], [3, 4]]) scalar = 1 result_matrix = MatrixOperations.scalar_multiply_matrix( scalar, first_matrix) self.assertTrue(result_matrix.data_lines[0][0] == 1 and result_matrix.data_lines[1][1] == 4)
def test_scalar_2_mult_square_2x2_matrix(self): first_matrix = Matrix.make_from_list([[1, 2], [3, 4]]) skalar = 2 result_matrix = MatrixOperations.scalar_multiply_matrix( skalar, first_matrix) self.assertTrue(result_matrix.data_lines[0][0] == 2 and result_matrix.data_lines[1][1] == 8)
def scalar_multiply_matrix(cls, scalar, matrix): if scalar == 0: return 0 elif scalar == 1: return matrix return Matrix.make_from_list([[scalar * elem for elem in row] for row in matrix.data_lines])
def test_inverse_2x2_matrix(self): matrix = Matrix.make_from_list([[1, 2], [3, 4]]) result_matrix = MatrixOperations.inverse(matrix) self.assertTrue(result_matrix.data_lines[0][0] == -2 and result_matrix.data_lines[0][1] == 1 and result_matrix.data_lines[1][0] == 1.5 and result_matrix.data_lines[1][1] == -0.5)
def add_matrix(cls, first_matrix, second_matrix): if not MatrixOperations.is_both_same_size(first_matrix, second_matrix): raise MatrixOperationsError( 'Matrices of different sizes! Check matrices elements.') return Matrix.make_from_list( [[elem1 + elem2 for elem1, elem2 in zip(row1, row2)] for row1, row2 in zip( first_matrix.data_lines, second_matrix.data_lines)])
def test_can_calculate_determinant_of_matrix6x6(self): test_matrix = Matrix.make_from_list([[-1, 2, 3, 4, 5, 6], [7, 0, 4, -1, 0, -3], [0, 5, -6, 0, 0, 3], [1, 0, 3, 4, 5, 6], [0, 8, 0, 1, 2, 0], [-4, 0, 6, 0, 0, -5]]) det = test_matrix.calculate_det() self.assertEqual(det, -96)
def multiply_matrix(cls, first_matrix, second_matrix): if not MatrixOperations.can_multiply_matrix(first_matrix, second_matrix): raise MatrixOperationsError( 'Matrices has invalid sizes! Check matrices size.') result_matrix = Matrix.make_from_list( [[0 for i in range(second_matrix.cols)] for i in range(first_matrix.rows)]) for i in range(first_matrix.rows): for k in range(first_matrix.cols): for j in range(second_matrix.cols): result_matrix.data_lines[i][j] += first_matrix.data_lines[i][k] * \ second_matrix.data_lines[k][j] return result_matrix
def test_can_calculate_determinant_of_matrix3x3(self): test_matrix = Matrix.make_from_list([[7, 2, 0], [5, 8, 7], [1, 2, 3]]) det = test_matrix.calculate_det() self.assertEqual(det, 54)
def test_delete_zero_row_and_zero_col_in_matrix(self): test_matrix = Matrix.delete_col_and_row( Matrix.make_from_list([[1, 3], [5, 7]]), 0, 0) det = test_matrix.calculate_det() self.assertEqual(det, 7)
def test_can_calculate_determinant_of_matrix2x2(self): test_matrix = Matrix.make_from_list([[1, 3], [5, 7]]) det = test_matrix.calculate_det() self.assertEqual(det, -8)
def test_can_not_delete_incorrect_col_or_row(self): test_matrix = Matrix.make_from_list([[1, 3], [5, 7], [5, 7]]) with self.assertRaises(MatrixError): Matrix.delete_col_and_row(test_matrix, -100, -500)
def test_check_exception(self): with self.assertRaises(MatrixError): test_matrix = Matrix.make_from_list([[1, 3]]) test_matrix.calculate_det()
def test_inverse_invalid_size_matrix(self): matrix = Matrix.make_from_list([[1, 2, 2], [3, 4, 4]]) with self.assertRaises(MatrixError): MatrixOperations.inverse(matrix)
def test_can_not_multiply_matrices(self): first_matrix = Matrix.make_from_list([[1, 1], [1, 1]]) second_matrix = Matrix.make_from_list([[2, 2]]) self.assertEqual( MatrixOperations.can_multiply_matrix(first_matrix, second_matrix), False)
def test_plus_diffsize_matrix(self): first_matrix = Matrix.make_from_list([[1, 1]]) second_matrix = Matrix.make_from_list([[-1]]) with self.assertRaises(MatrixOperationsError): MatrixOperations.add_matrix(first_matrix, second_matrix)
def test_scalar_0_mult_matrix(self): first_matrix = Matrix.make_from_list([[1, 2], [3, 4]]) scalar = 0 result_matrix = MatrixOperations.scalar_multiply_matrix( scalar, first_matrix) self.assertEqual(result_matrix, 0)
def test_transpose_3x2_matrix(self): matrix = Matrix.make_from_list([[1, 2], [3, 4], [5, 6]]) result_matrix = MatrixOperations.transpose(matrix) self.assertTrue(result_matrix.data_lines[0][1] == 3 and result_matrix.data_lines[1][0] == 2 and result_matrix.data_lines[0][2] == 5)
def test_not_onesize_matrices(self): first_matrix = Matrix.make_from_list([[1, 1]]) second_matrix = Matrix.make_from_list([[2]]) self.assertEqual( MatrixOperations.is_both_same_size(first_matrix, second_matrix), False)
def test_inverse_matrix_with_zero_det(self): matrix = Matrix.make_from_list([[1, 2], [2, 4]]) with self.assertRaises(MatrixError): MatrixOperations.inverse(matrix)
def test_can_calculate_minor_0_1(self): test_matrix = Matrix.delete_col_and_row( Matrix.make_from_list([[1, 0, -3], [0, 0, 2], [-1, -2, 0]]), 0, 1) det = test_matrix.calculate_det() self.assertEqual(det, 2)
def test_create_matrix_with_not_full_first_string(self): with self.assertRaises(MatrixError): Matrix.make_from_list([[1, 1], [1, 1, 1], [1, 2, 1]])
def test_check_constructor(self): test_matrix = Matrix.make_from_list([[1, 1]]) self.assertTrue( test_matrix.cols == 2 and test_matrix.rows == 1 and test_matrix.data_lines[0][0] == test_matrix.data_lines[0][1] == 1)
def test_can_write_matrix_as_string(self): test_matrix = Matrix.make_from_list([[1, 3], [5, 7]]) self.assertEqual(str(test_matrix), '1 3\n5 7\n')
def test_get_data_line(self): test_matrix = Matrix.make_from_list([[7, 0], [5, 8]]) self.assertEqual(test_matrix.get_data_lines(), [[7, 0], [5, 8]])
def test_transpose_1x3_matrix(self): matrix = Matrix.make_from_list([[1, 2, 3]]) result_matrix = MatrixOperations.transpose(matrix) self.assertTrue(result_matrix.data_lines[0][0] == 1 and result_matrix.data_lines[1][0] == 2 and result_matrix.data_lines[2][0] == 3)