Add _repr_latex_ methods for IPython prettyprint
[linpy.git] / pypol / tests / test_geometry.py
1 import math
2 import unittest
3
4 from ..geometry import *
5 from ..linexprs import Symbol
6 from ..polyhedra import Eq
7
8
9 class TestPoint(unittest.TestCase):
10
11 def setUp(self):
12 self.x = Symbol('x')
13 self.y = Symbol('y')
14 self.z = Symbol('z')
15 self.pt1 = Point({self.x: 10, self.y: 5, self.z: 1})
16 self.pt2 = Point({self.x: 15, self.y: 40, self.z: 60})
17 self.vec1 = Vector({self.x: 20, self.y: 30, self.z: 40})
18
19 def test_add(self):
20 self.assertEqual(self.pt1 + self.vec1, Point({self.x: 30, self.y: 35, self.z: 41}))
21 with self.assertRaises(TypeError):
22 self.pt1 + self.pt2
23
24 def test_eq(self):
25 self.assertEqual(self.pt1, self.pt1)
26 self.assertNotEqual(self.pt1, self.pt2)
27 self.assertNotEqual(self.pt1, self.vec1)
28
29 def test_sub(self):
30 self.assertEqual(self.pt1 - self.pt2, Vector({self.x: -5, self.y: -35, self.z: -59}))
31 self.assertEqual(self.pt1 - self.vec1, Point({self.x: -10, self.y: -25, self.z: -39}))
32
33 def test_aspolyhedron(self):
34 self.assertEqual(self.pt1.aspolyhedron(), Eq(self.x, 10) & Eq(self.y, 5) & Eq(self.z, 1))
35
36
37 class TestVector(unittest.TestCase):
38
39 def setUp(self):
40 self.x = Symbol('x')
41 self.y = Symbol('y')
42 self.z = Symbol('z')
43 self.pt1 = Point({self.x: 10, self.y: 5, self.z: 1})
44 self.pt2 = Point({self.x: 15, self.y: 40, self.z: 60})
45 self.vec1 = Vector({self.x: 20, self.y: 30, self.z: 40})
46 self.vec2 = Vector({self.x: 45, self.y: 70, self.z: 80})
47
48 def test_add(self):
49 self.assertEqual(self.vec1 + self.pt1, Point({self.x: 30, self.y: 35, self.z: 41}))
50 self.assertEqual(self.vec1 + self.vec2, Vector({self.x: 65, self.y: 100, self.z: 120}))
51
52 def test_angle(self):
53 self.assertEqual(math.degrees(self.vec1.angle(self.vec1)), 0)
54 self.assertAlmostEqual(math.degrees(self.vec1.angle(self.vec2)), 4.15129, places=5)
55 self.assertAlmostEqual(math.degrees(self.vec2.angle(self.vec1)), 4.15129, places=5)
56
57 def test_cross(self):
58 self.assertEqual(self.vec1.cross(self.vec2), Vector({self.x: -400, self.y: 200, self.z: 50}))
59
60 def test_div(self):
61 self.assertEqual(self.vec1 / 10, Vector({self.x: 2, self.y: 3, self.z: 4}))
62
63 def test_dot(self):
64 self.assertEqual(self.vec1.dot(self.vec2), 6200)
65
66 def test_eq(self):
67 self.assertEqual(self.vec1, self.vec1)
68 self.assertNotEqual(self.vec1, self.vec2)
69
70 def test_mul(self):
71 self.assertEqual(75 * self.vec1, Vector({self.x: 1500, self.y: 2250, self.z: 3000}))
72 self.assertEqual(self.vec1 * 75, Vector({self.x: 1500, self.y: 2250, self.z: 3000}))
73
74 def test_neg(self):
75 self.assertEqual(-self.vec1, Vector({self.x: -20, self.y: -30, self.z: -40}))
76
77 def test_norm(self):
78 self.assertAlmostEqual(self.vec1.norm(), 53.85165, places=5)
79
80 def test_norm2(self):
81 self.assertEqual(self.vec1.norm2(), 2900)
82
83 def test_asunit(self):
84 unit = self.vec1.asunit()
85 self.assertAlmostEqual(unit[self.x], 0.37139, 5)
86 self.assertAlmostEqual(unit[self.y], 0.55709, 5)
87 self.assertAlmostEqual(unit[self.z], 0.74278, 5)
88
89 def test_sub(self):
90 self.assertEqual(self.vec1 - self.pt1, Point({self.x: 10, self.y: 25, self.z: 39}))
91 self.assertEqual(self.vec1 - self.vec2, Vector({self.x: -25, self.y: -40, self.z: -40}))