def test_can_get_matrix_rank(self, mock_rank): mock_rank.return_value = 400 m = Matrix([3, 5, 9], [2, 3, 5]) self.assertFalse(m.is_full_rank()) mock_rank.assert_called_with() mock_rank.return_value = 2 self.assertTrue(m.is_full_rank())
def test_matrix_size(self, mock_height, mock_width): mock_width.return_value = 50 mock_height.return_value = 35 matrix = Matrix([1, 2], [3, 4], [5, 6]) self.assertEqual(matrix.size(), (35, 50)) mock_width.assert_called_with() mock_height.assert_called_with()
def test_square_matrices(self, mock_height, mock_width): mock_height.return_value = 4 mock_width.return_value = 4 matrix = Matrix([1, 2], [3, 4], [5, 6]) self.assertTrue(matrix.is_square()) mock_width.return_value = 3 self.assertFalse(matrix.is_square())
def test_can_return_unaltered_minors(self): matrix = Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]) self.assertEqual(matrix.cofactor(0, 0), 5) self.assertEqual(matrix.cofactor(0, 2), 5) self.assertEqual(matrix.cofactor(1, 1), 5) self.assertEqual(matrix.cofactor(2, 0), 5) self.assertEqual(matrix.cofactor(2, 0), 5)
def test_can_get_identity_matrix(self): i = Matrix.identity(1) self.assertEqual(i._rows, [[1]]) i = Matrix.identity(2) self.assertEqual(i._rows, [[1, 0], [0, 1]]) i = Matrix.identity(3) self.assertEqual(i._rows, [[1, 0, 0], [0, 1, 0], [0, 0, 1]])
def test_3d_matrix_minors(self): self.mock_width.return_value = 3 matrix = Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]) matrix_patch = patch("points.matrices.Matrix") mock_matrix = matrix_patch.start() try: matrices2 = [Mock(), Mock(), Mock(), Mock(), Mock(), Mock()] mock_matrix.side_effect = matrices2 for index, mat in enumerate(matrices2, start=1): mat.determinant.return_value = index * 100 self.assertEqual(matrix.minor(0, 0), 100) self.assertEqual(matrix.minor(0, 1), 200) self.assertEqual(matrix.minor(1, 1), 300) self.assertEqual(matrix.minor(1, 2), 400) self.assertEqual(matrix.minor(2, 0), 500) self.assertEqual(matrix.minor(2, 2), 600) mock_matrix.assert_any_call([5, 6], [8, 9]) mock_matrix.assert_any_call([4, 6], [7, 9]) mock_matrix.assert_any_call([1, 3], [7, 9]) mock_matrix.assert_any_call([1, 2], [7, 8]) mock_matrix.assert_any_call([2, 3], [5, 6]) mock_matrix.assert_any_call([1, 2], [4, 5]) for mat in matrices2: mat.determinant.assert_called_with() finally: matrix_patch.stop()
def test_2d_matrix_minors(self): self.mock_width.return_value = 2 matrix = Matrix([1, 2], [3, 4]) self.assertEqual(matrix.minor(0, 0), 4) self.assertEqual(matrix.minor(0, 1), 3) self.assertEqual(matrix.minor(1, 0), 2) self.assertEqual(matrix.minor(1, 1), 1)
def test_can_get_matrix_rank(self, mock_space): space = Mock() space.rank.return_value = 400 mock_space.return_value = space m = Matrix([3, 5, 9], [2, 3, 5]) self.assertEqual(m.rank(), 400) mock_space.assert_called_with() space.rank.assert_called_with()
def test_can_get_matrix_inverse(self, mock_det, mock_adjoint): adjoint = Mock() mock_adjoint.return_value = 3 mock_det.return_value = 0.25 matrix = Matrix([4, -3, 1], [2, -1, 2], [1, 5, 7]) self.assertEqual(matrix.inverse(), 12) mock_adjoint.assert_called_with() mock_det.assert_called_with()
def test_can_get_matrix_adjoint(self, mock_cof): cofactors = Mock() mock_cof.return_value = cofactors cofactors.transposed.return_value = "TRANSPOSED" matrix = Matrix([4, -3, 1], [2, -1, 2], [1, 5, 7]) self.assertEqual(matrix.adjoint(), "TRANSPOSED") mock_cof.assert_called_with() cofactors.transposed.assert_called_with()
def test_3d_matrix(self, mock_cof): self.mock_width.return_value = 3 mock_cof.side_effect = [16, -23, 42] matrix = Matrix([4, -3, 1], [2, -1, 2], [1, 5, 7]) self.assertEqual(matrix.determinant(), 175) mock_cof.assert_any_call(0, 0) mock_cof.assert_any_call(0, 1) mock_cof.assert_any_call(0, 2)
def test_reduced_row_echelon_checks(self, mock_check): mock_check.return_value = True self.assertTrue( Matrix([1, 0], [0, 1], [0, 0]).is_reduced_row_echelon()) mock_check.assert_called_with() self.assertFalse( Matrix([1, 0], [0, 2], [0, 0]).is_reduced_row_echelon()) self.assertFalse( Matrix([1, 0, 0], [0, 1, 1], [0, 0, 1]).is_reduced_row_echelon()) self.assertTrue( Matrix([1, 0, 0], [0, 0, 1], [0, 0, 0]).is_reduced_row_echelon())
def test_can_get_matrix_column_space(self, mock_span, mock_vec, mock_col): mock_col.return_value = ((3, 2), (5, 3), (9, 5)) mock_vec.side_effect = ("v1", "v2", "v3") mock_span.return_value = "SPAN" m = Matrix([3, 5, 9], [2, 3, 5]) space = m.column_space() mock_col.assert_called_with() mock_vec.assert_any_call((3, 2)) mock_vec.assert_any_call((5, 3)) mock_vec.assert_any_call((9, 5)) mock_span.assert_called_with("v1", "v2", "v3") self.assertEqual(space, "SPAN")
def test_can_get_matrix_minors(self, mock_cof): mock_cof.side_effect = [4, 8, 15, 16, 23, 42, 19, 20, 21] matrix = Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]) minors = matrix.cofactors() self.assertEqual(minors._rows, [[4, 8, 15], [16, 23, 42], [19, 20, 21]]) mock_cof.assert_any_call(0, 0) mock_cof.assert_any_call(0, 1) mock_cof.assert_any_call(0, 2) mock_cof.assert_any_call(1, 0) mock_cof.assert_any_call(1, 1) mock_cof.assert_any_call(1, 2) mock_cof.assert_any_call(2, 0) mock_cof.assert_any_call(2, 1) mock_cof.assert_any_call(2, 2)
def test_vector_must_be_right_size_for_matmul(self): matrix = Matrix([1, 2], [3, 4]) vector = Mock(Vector) vector.values.return_value = [5, 6] vector.__len__, vector.__len__.return_value = MagicMock(), 3 with self.assertRaises(ValueError): output = matrix @ vector
def test_can_mat_mul_vectors(self): matrix = Matrix([1, 2], [3, 4]) vector = Mock(Vector) vector.values.return_value = [5, 6] vector.__len__, vector.__len__.return_value = MagicMock(), 2 output = matrix @ vector self.assertIsInstance(output, Vector) self.assertEqual(output._values, [17, 39])
def test_matrices_not_equal(self): self.assertNotEqual(Matrix([1, 2], [3, 4]), Matrix([1, 9], [3, 4])) self.assertNotEqual(Matrix([1, 2], [3, 4]), Matrix([1, 2, 9], [3, 4, 9])) self.assertNotEqual(Matrix([1, 2, 9], [3, 4, 9]), Matrix([1, 2], [3, 4])) self.assertNotEqual(Matrix([1, 2, 9], [3, 4, 9]), "other")
def test_matrices_equal(self): self.assertEqual(Matrix([1, 2], [3, 4]), Matrix([1, 2], [3, 4]))
def test_can_gaussian_eliminate_square_matrices(self): m = Matrix([3, 5, 9], [2, 3, 5]) m.gauss() self.assertEqual(m._rows, [[3, 5, 9], [0, -1 / 3, -1]]) m = Matrix([2, 1, -1, 8], [-3, -1, 2, -11], [-2, 1, 2, -3]) m.gauss() self.assertEqual(m._rows, [[-3, -1, 2, -11], [0, 5 / 3, 2 / 3, 13 / 3], [0, 0, 1 / 5, -1 / 5]]) m = Matrix([1, -1, -1, 1], [3, 2, 12, 1], [2, -1, 1, 1]) m.gauss() self.assertEqual( m._rows, [[3, 2, 12, 1], [0, -7 / 3, -7, 1 / 3], [0, 0, 0, 3 / 7]])
def test_matrix_str_different_widths(self): matrix = Matrix([1, 2, 3.5], [4.002, 5, 6]) self.assertEqual(str(matrix), " 1 2 3.5\n4.002 5 6")
def test_items_in_matrix(self): matrix = Matrix([1, 2], [3, 4], [5, 6]) self.assertIn(4, matrix) self.assertNotIn(8, matrix)
def test_dimensions_must_be_int(self): with self.assertRaises(TypeError): Matrix.identity(1.4)
def test_matrix_str_simple(self): matrix = Matrix([1, 2], [3, 4], [5, 6]) self.assertEqual(str(matrix), "1 2\n3 4\n5 6")
def test_row_echelon_returns_true(self): self.assertTrue(Matrix([1, 0], [0, 2]).is_row_echelon()) self.assertTrue(Matrix([1, 0], [0, 2], [0, 0]).is_row_echelon())
def test_check_row_echelon_first(self, mock_check): mock_check.return_value = False self.assertFalse( Matrix([1, 0], [0, 1], [0, 0]).is_reduced_row_echelon()) mock_check.assert_called_with()
def test_not_in_row_echelon_if_leading_coefficients_wrong(self): self.assertFalse(Matrix([1, 0], [2, 0]).is_row_echelon()) self.assertFalse(Matrix([0, 1], [2, 0]).is_row_echelon())
def test_not_in_row_echelon_if_zeroes_above_non_zeroes(self): self.assertFalse(Matrix([1, 0], [0, 0], [0, 1]).is_row_echelon()) self.assertFalse(Matrix([0, 0], [1, 0], [0, 1]).is_row_echelon())
def test_matrix_repr(self, mock_size): mock_size.return_value = (12, 43) matrix = Matrix([1, 2], [3, 4], [5, 6]) self.assertEqual(repr(matrix), "<12×43 Matrix>")
def test_can_gaussian_eliminate_horizontal_matrices(self): m = Matrix([3, 5, 9, 2, 3, 5], [4, 6, 8, 3, 6, 7]) m.gauss() self.assertEqual(m._rows, [[4, 6, 8, 3, 6, 7], [0, 0.5, 3, -0.25, -1.5, -0.25]])
def test_can_gaussian_eliminate_vertical_matrices(self): m = Matrix([3, 5, 9], [2, 3, 5], [4, 6, 8], [3, 6, 7], [1, 2, 3]) m.gauss() self.assertEqual( m._rows, [[4, 6, 8], [0, 3 / 2, 1], [0, 0, 8 / 3], [0, 0, 0], [0, 0, 0]])