def test_dot_product(self): vector1 = Vector([7.887, 4.138]) vector2 = Vector([-8.802, 6.776]) self.assertAlmostEqual(vector1.dot_product(vector2), -41.382286, 5, "Incorrect dot product") vector1 = Vector([-5.955, -4.904, -1.874]) vector2 = Vector([-4.496, -8.755, 7.103]) self.assertAlmostEqual(vector1.dot_product(vector2), 56.397178, 5, "Incorrect dot product")
def test_normalization(self): vector = Vector([5.581, -2.136]) self.assertEqual(vector.normalized(), Vector([0.9339352140866403, -0.35744232526233]), "Incorrect normalized vector") vector = Vector([1.996, 3.108, -4.554]) self.assertEqual(vector.normalized(), Vector([0.3404012959433014, 0.5300437012984873, -0.7766470449528028]), "Incorrect normalized vector")
def test_compute_solution(self): # first system p1 = Plane(Vector([5.862, 1.178, -10.366]), -8.15) p2 = Plane(Vector([-2.931, -0.589, 5.183]), -4.075) system1 = LinearSystem([p1, p2]) self.assertEqual(system1.compute_solution(), 'No solutions', 'There is no solution to this system.') # # second system p1 = Plane(Vector([8.631, 5.112, -1.816]), -5.113) p2 = Plane(Vector([4.315, 11.132, -5.27]), -6.775) p3 = Plane(Vector([-2.158, 3.01, -1.727]), -0.831) system2 = LinearSystem([p1, p2, p3]) self.assertEqual(system2.compute_solution(), 'Infinitely many solutions', 'There are infinite solutions to this system.') # third system p1 = Plane(Vector([5.262, 2.739, -9.878]), -3.441) p2 = Plane(Vector([5.111, 6.358, 7.638]), -2.152) p3 = Plane(Vector([2.016, -9.924, -1.367]), -9.278) p4 = Plane(Vector([2.167, -13.543, -18.883]), -10.567) system3 = LinearSystem([p1, p2, p3, p4]) self.assertEqual( system3.compute_solution(), Vector( [-1.1772018757899585, 0.707150558138741, -0.08266358490228289]), 'Wrong solution computed!')
def test_is_parallel1(self): plane1 = Plane(normal_vector=Vector([-0.412, 3.806, 0.728]), constant_term=-3.46) plane2 = Plane(normal_vector=Vector([1.03, -9.515, -1.82]), constant_term=8.65) self.assertEqual(plane1.is_parallel(plane2), True, "Planes are parallel") self.assertEqual(plane1 == plane2, True, "Planes are equal")
def test_swap_rows(self): s = LinearSystem([self.p0, self.p1, self.p2, self.p3]) s.swap_rows(0, 1) result = (s[0] == self.p1 and s[1] == self.p0 and s[2] == self.p2 and s[3] == self.p3) self.assertTrue(result, 'test_swap_rows : test case 1 failed') s.swap_rows(1, 3) result = (s[0] == self.p1 and s[1] == self.p3 and s[2] == self.p2 and s[3] == self.p0) self.assertTrue(result, 'test_swap_rows : test case 2 failed') s.swap_rows(3, 1) result = (s[0] == self.p1 and s[1] == self.p0 and s[2] == self.p2 and s[3] == self.p3) self.assertTrue(result, 'test_swap_rows : test case 3 failed') s.multiply_coefficient_and_row(1, 0) result = (s[0] == self.p1 and s[1] == self.p0 and s[2] == self.p2 and s[3] == self.p3) self.assertTrue(result, 'test_swap_rows : test case 4 failed') s.multiply_coefficient_and_row(-1, 2) result = (s[0] == self.p1 and s[1] == self.p0 and s[2] == Plane(normal_vector=Vector([-1.0, -1.0, 1.0]), constant_term='-3') and s[3] == self.p3) self.assertTrue(result, 'test_swap_rows : test case 5 failed') s.multiply_coefficient_and_row(10, 1) result = (s[0] == self.p1 and s[1] == Plane( normal_vector=Vector([10.0, 10.0, 10.0]), constant_term='10') and s[2] == Plane(normal_vector=Vector([-1.0, -1.0, 1.0]), constant_term='-3') and s[3] == self.p3) self.assertTrue(result, 'test_swap_rows : test case 6 failed') s.add_multiple_times_row_to_row(0, 0, 1) result = (s[0] == self.p1 and s[1] == Plane( normal_vector=Vector([10.0, 10.0, 10.0]), constant_term='10') and s[2] == Plane(normal_vector=Vector([-1.0, -1.0, 1.0]), constant_term='-3') and s[3] == self.p3) self.assertTrue(result, 'test_swap_rows : test case 7 failed') s.add_multiple_times_row_to_row(1, 0, 1) result = (s[0] == self.p1 and s[1] == Plane( normal_vector=Vector([10.0, 11.0, 10.0]), constant_term='12') and s[2] == Plane(normal_vector=Vector([-1.0, -1.0, 1.0]), constant_term='-3') and s[3] == self.p3) self.assertTrue(result, 'test_swap_rows : test case 8 failed') s.add_multiple_times_row_to_row(-1, 1, 0) result = (s[0] == Plane(normal_vector=Vector([-10.0, -10.0, -10.0]), constant_term='-10') and s[1] == Plane(normal_vector=Vector([10.0, 11.0, 10.0]), constant_term='12') and s[2] == Plane(normal_vector=Vector([-1.0, -1.0, 1.0]), constant_term='-3') and s[3] == self.p3) self.assertTrue(result, 'test_swap_rows : test case 9 failed')
def setUp(self): self.p0 = Plane(normal_vector=Vector([1.0, 1.0, 1.0]), constant_term='1') self.p1 = Plane(normal_vector=Vector([0.0, 1.0, 0.0]), constant_term='2') self.p2 = Plane(normal_vector=Vector([1.0, 1.0, -1.0]), constant_term='3') self.p3 = Plane(normal_vector=Vector([1.0, 0.0, -2.0]), constant_term='2')
def gaussian_elimination_and_extraction(self): rref = self.compute_rref() rref.check_exceptions() num_variables = rref.dimension solution_coordinates = [rref.planes[i].constant_term for i in range(num_variables)] return Vector(solution_coordinates)
def test_hyper_plane(self): p1 = HyperPlane(normal_vector=Vector([0.786, 0.786]), constant_term=0.786) p2 = HyperPlane(normal_vector=Vector([-0.131, -0.131]), constant_term=-0.131) system = LinearSystem([p1, p2]) self.assertEqual(system.compute_solution(), 'Infinitely many solutions', 'Wrong solution computed!') p1 = HyperPlane(normal_vector=Vector([2.102, 7.489, -0.786]), constant_term=-5.113) p2 = HyperPlane(normal_vector=Vector([-1.131, 8.318, -1.209]), constant_term=-6.775) p3 = HyperPlane(normal_vector=Vector([9.015, 5.873, -1.105]), constant_term=-0.831) system = LinearSystem([p1, p2, p3]) self.assertEqual( system.compute_solution(), Vector( [0.3846829686036449, -0.864793338457455, -0.7058953075102014]), 'Wrong solution computed!') p1 = HyperPlane(normal_vector=Vector( [0.786, 0.786, 8.123, 1.111, -8.363]), constant_term=-9.955) p2 = HyperPlane(normal_vector=Vector( [0.131, -0.131, 7.05, -2.813, 1.19]), constant_term=-1.991) p3 = HyperPlane(normal_vector=Vector( [9.015, -5.873, -1.105, 2.013, -2.802]), constant_term=-3.982) system = LinearSystem([p1, p2, p3]) self.assertEqual(system.compute_solution(), 'Infinitely many solutions', 'Wrong solution computed!')
def test_projection(self): vector1 = Vector([3.039, 1.879]) vector2 = Vector([0.825, 2.036]) self.assertEqual(vector1.projection_parallel_component(vector2), Vector([1.0826069624844668, 2.671742758325302]), "Incorrect projection parallel component") vector1 = Vector([-9.88, -3.264, -8.159]) vector2 = Vector([-2.155, -9.353, -9.473]) self.assertEqual(vector1.projection_orthogonal_component(vector2), Vector([-8.350081043195763, 3.376061254287722, -1.4337460427811841]), "Incorrect projection orthogonal component") vector1 = Vector([3.009, -6.172, 3.692, -2.51]) vector2 = Vector([6.404, -9.144, 2.759, 8.718]) self.assertEqual(vector1.projection_parallel_component(vector2), Vector([1.9685161672140898, -2.8107607484393564, 0.8480849633578503, 2.679813233256158]), "Incorrect projection parallel component") self.assertEqual(vector1.projection_orthogonal_component(vector2), Vector([1.04048383278591, -3.3612392515606433, 2.8439150366421497, -5.189813233256158]), "Incorrect projection orthogonal component")
def test_line_intersection2(self): line1 = Line(Vector([7.204, 3.1882]), 8.68) line2 = Line(Vector([8.172, 4.114]), 9.883) self.assertEqual(line1.intersection_point(line2), Vector([1.1722591690709803, 0.07372340066892474]), "Both points should be equal")
def test_scalar(self): vector = Vector([1.671, -1.012, -0.318]) vector.scalar(7.41) self.assertEqual(vector, Vector([1.671, -1.012, -0.318]), "Incorrect scalar")
def test_compute_parametrized_solution(self): p1 = Plane(normal_vector=Vector([0.786, 0.786, 0.588]), constant_term=-0.714) p2 = Plane(normal_vector=Vector([-0.131, -0.131, 0.244]), constant_term=0.319) system = LinearSystem([p1, p2]) result = Parametrization(Vector( [0.319, 0, 0]), [Vector([0.131, 1, 0]), Vector([-0.244, 0, 1])]) self.assertEqual(system.compute_parametrized_solution(), result, 'Wrong solution computed!') p1 = Plane(Vector([8.631, 5.112, -1.816]), -5.113) p2 = Plane(Vector([4.315, 11.132, -5.27]), -6.775) p3 = Plane(Vector([-2.158, 3.01, -1.727]), -0.831) system = LinearSystem([p1, p2, p3]) result = Parametrization(Vector( [-0.831, 0, 0]), [Vector([-3.01, 1, 0]), Vector([1.727, 0, 1])]) self.assertEqual(system.compute_parametrized_solution(), result, 'Wrong solution computed!') p1 = Plane(Vector([0.935, 1.76, -9.365]), -9.955) p2 = Plane(Vector([0.187, 0.352, -1.873]), -1.991) p3 = Plane(Vector([0.374, 0.704, -3.746]), -3.982) p4 = Plane(Vector([-0.561, -1.056, 5.619]), 5.973) system = LinearSystem([p1, p2, p3, p4]) result = Parametrization(Vector( [5.973, 0, 0]), [Vector([1.056, 1, 0]), Vector([-5.619, 0, 1])]) self.assertEqual(system.compute_parametrized_solution(), result, 'Wrong solution computed!')
def test_magnitude(self): vector = Vector([-0.221, 7.437]) self.assertAlmostEqual(vector.magnitude(), 7.44028292473, 5, "Incorrect magnitude") vector = Vector([8.813, -1.331, -6.247]) self.assertAlmostEqual(vector.magnitude(), 10.8841875673, 5, "Incorrect magnitude")
def test_compute_rref(self): p1 = Plane(normal_vector=Vector([1.0, 1.0, 1.0]), constant_term='1') p2 = Plane(normal_vector=Vector([0.0, 1.0, 1.0]), constant_term='2') s = LinearSystem([p1, p2]) r = s.compute_rref() result = (r[0] == Plane(normal_vector=Vector([1.0, 0.0, 0.0]), constant_term='-1') and r[1] == p2) self.assertTrue(result, 'test_compute_rref : test case 1 failed') p1 = Plane(normal_vector=Vector([1.0, 1.0, 1.0]), constant_term='1') p2 = Plane(normal_vector=Vector([1.0, 1.0, 1.0]), constant_term='2') s = LinearSystem([p1, p2]) r = s.compute_rref() result = (r[0] == p1 and r[1] == Plane(constant_term='1')) self.assertTrue(result, 'test_compute_rref : test case 2 failed') p1 = Plane(normal_vector=Vector([1.0, 1.0, 1.0]), constant_term='1') p2 = Plane(normal_vector=Vector([0.0, 1.0, 0.0]), constant_term='2') p3 = Plane(normal_vector=Vector([1.0, 1.0, -1.0]), constant_term='3') p4 = Plane(normal_vector=Vector([1.0, 0.0, -2.0]), constant_term='2') s = LinearSystem([p1, p2, p3, p4]) r = s.compute_rref() result = (r[0] == Plane(normal_vector=Vector([1.0, 0.0, 0.0]), constant_term='0') and r[1] == p2 and r[2] == Plane(normal_vector=Vector([0.0, 0.0, -2.0]), constant_term='2') and r[3] == Plane()) self.assertTrue(result, 'test_compute_rref : test case 3 failed') p1 = Plane(normal_vector=Vector([0.0, 1.0, 1.0]), constant_term='1') p2 = Plane(normal_vector=Vector([1.0, -1.0, 1.0]), constant_term='2') p3 = Plane(normal_vector=Vector([1.0, 2.0, -5.0]), constant_term='3') s = LinearSystem([p1, p2, p3]) r = s.compute_rref() result = (r[0] == Plane(normal_vector=Vector([1.0, 0.0, 0.0]), constant_term=Decimal('23') / Decimal('9')) and r[1] == Plane(normal_vector=Vector([0.0, 1.0, 0.0]), constant_term=Decimal('7') / Decimal('9')) and r[2] == Plane(normal_vector=Vector([0.0, 0.0, 1.0]), constant_term=Decimal('2') / Decimal('9'))) self.assertTrue(result, 'test_compute_rref : test case 4 failed')
def test_angle_in_radian(self): vector1 = Vector([3.183, -7.627]) vector2 = Vector([-2.668, 5.319]) self.assertAlmostEqual(vector1.angle_in_radian(vector2), 3.07202289163, 5, "Incorrect angle in radian")
def test_is_parallel3(self): plane1 = Plane(Vector([-7.926, 8.625, -7.212]), -7.95) plane2 = Plane(Vector([-2.642, 2.875, -2.404]), 2.44) self.assertEqual(plane1.is_parallel(plane2), True, "Planes are not parallel") self.assertEqual(plane1 == plane2, False, "Planes are not equal")
def test_is_parallel2(self): plane1 = Plane(Vector([2.611, 5.528, 0.283]), 4.6) plane2 = Plane(Vector([7.715, 8.306, 5.342]), 3.76) self.assertEqual(plane1.is_parallel(plane2), False, "Planes are not parallel") self.assertEqual(plane1 == plane2, False, "Planes are not equal")
def test_line_intersection1(self): line1 = Line(normal_vector=Vector([4.046, 2.836]), constant_term=1.21) line2 = Line(normal_vector=Vector([10.115, 7.09]), constant_term=3.025) self.assertEqual(line1.intersection_point(line2), Vector([0.0, 0.0]), "Both points should be equal")
def test_triangle_area(self): vector1 = Vector([1.5, 9.547, 3.691]) vector2 = Vector([-6.007, 0.124, 5.772]) self.assertAlmostEqual(vector1.triangle_area(vector2), 42.5649373994, 5, "Incorrect triangle area")
def test_compute_triangular_form(self): p1 = Plane(normal_vector=Vector([1.0, 1.0, 1.0]), constant_term='1') p2 = Plane(normal_vector=Vector([0.0, 1.0, 1.0]), constant_term='2') s = LinearSystem([p1, p2]) t = s.compute_triangular_form() result = (t[0] == p1 and t[1] == p2) self.assertTrue(result, 'test_compute_triangular_form : test case 1 failed') p1 = Plane(normal_vector=Vector([1.0, 1.0, 1.0]), constant_term='1') p2 = Plane(normal_vector=Vector([1.0, 1.0, 1.0]), constant_term='2') s = LinearSystem([p1, p2]) t = s.compute_triangular_form() result = (t[0] == p1 and t[1] == Plane(constant_term='1')) self.assertTrue(result, 'test_compute_triangular_form : test case 2 failed') p1 = Plane(normal_vector=Vector([1.0, 1.0, 1.0]), constant_term='1') p2 = Plane(normal_vector=Vector([0.0, 1.0, 0.0]), constant_term='2') p3 = Plane(normal_vector=Vector([1.0, 1.0, -1.0]), constant_term='3') p4 = Plane(normal_vector=Vector([1.0, 0.0, -2.0]), constant_term='2') s = LinearSystem([p1, p2, p3, p4]) t = s.compute_triangular_form() result = (t[0] == p1 and t[1] == p2 and t[2] == Plane(normal_vector=Vector([0.0, 0.0, -2.0]), constant_term='2') and t[3] == Plane()) self.assertTrue(result, 'test_compute_triangular_form : test case 3 failed') p1 = Plane(normal_vector=Vector([0.0, 1.0, 1.0]), constant_term='1') p2 = Plane(normal_vector=Vector([1.0, -1.0, 1.0]), constant_term='2') p3 = Plane(normal_vector=Vector([1.0, 2.0, -5.0]), constant_term='3') s = LinearSystem([p1, p2, p3]) t = s.compute_triangular_form() result = (t[0] == Plane(normal_vector=Vector([1.0, -1.0, 1.0]), constant_term='2') and t[1] == Plane(normal_vector=Vector([0.0, 1.0, 1.0]), constant_term='1') and t[2] == Plane(normal_vector=Vector([0.0, 0.0, -9.0]), constant_term='-2')) self.assertTrue(result, 'test_compute_triangular_form : test case 4 failed')
def test_cross_product(self): vector1 = Vector([8.462, 7.893, -8.187]) vector2 = Vector([6.984, -5.975, 4.778]) self.assertEqual(vector1.cross_product(vector2), Vector([-11.204570999999994, -97.609444, -105.68516199999999]), "Incorrect cross product")
def test_parallel_and_orthogonal(self): vector1 = Vector([-7.579, -7.88]) vector2 = Vector([22.737, 23.64]) self.assertIs(vector1.is_parallel(vector2), True, "Vectors are parallel") self.assertIs(vector1.is_orthogonal(vector2), False, "Vectors are not orthogonal") vector1 = Vector([-2.029, 9.97, 4.172]) vector2 = Vector([-9.231, -6.639, -7.245]) self.assertIs(vector1.is_parallel(vector2), False, "Vectors are not parallel") self.assertIs(vector1.is_orthogonal(vector2), False, "Vectors are not orthogonal") vector1 = Vector([-2.328, -7.284, -1.214]) vector2 = Vector([-1.821, 1.072, -2.94]) self.assertIs(vector1.is_parallel(vector2), False, "Vectors are not parallel") self.assertIs(vector1.is_orthogonal(vector2), True, "Vectors are orthogonal") vector1 = Vector([2.118, 4.827]) vector2 = Vector([0, 0]) self.assertIs(vector1.is_parallel(vector2), True, "Vectors are parallel") self.assertIs(vector1.is_orthogonal(vector2), True, "Vectors are orthogonal")
def test_angle_in_degree(self): vector1 = Vector([7.35, 0.221, 5.188]) vector2 = Vector([2.751, 8.259, 3.985]) self.assertAlmostEqual(vector1.angle_in_degree(vector2), 60.2758335058, 5, "Incorrect angle in degree")
def test_line_intersection3(self): line1 = Line(Vector([1.182, 5.562]), 6.744) line2 = Line(Vector([1.773, 8.343]), 9.525) self.assertIsNone(line1.intersection_point(line2), "There is no intersection between these two lines")
def test_parallelogram_area(self): vector1 = Vector([-8.987, -9.838, 5.031]) vector2 = Vector([-4.268, -1.861, -8.866]) self.assertAlmostEqual(vector1.parallelogram_area(vector2), 142.122221402, 5, "Incorrect parallelogram area")