Last-minute changes in the documentation
[linpy.git] / linpy / tests / test_geometry.py
1 # Copyright 2014 MINES ParisTech
2 #
3 # This file is part of LinPy.
4 #
5 # LinPy is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # LinPy is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with LinPy. If not, see <http://www.gnu.org/licenses/>.
17
18 import math
19 import unittest
20
21 from ..geometry import *
22 from ..linexprs import Symbol
23 from ..polyhedra import Eq
24
25
26 class TestPoint(unittest.TestCase):
27
28 def setUp(self):
29 self.x = Symbol('x')
30 self.y = Symbol('y')
31 self.z = Symbol('z')
32 self.pt1 = Point({self.x: 10, self.y: 5, self.z: 1})
33 self.pt2 = Point({self.x: 15, self.y: 40, self.z: 60})
34 self.vec1 = Vector({self.x: 20, self.y: 30, self.z: 40})
35
36 def test_add(self):
37 self.assertEqual(self.pt1 + self.vec1, Point({self.x: 30, self.y: 35, self.z: 41}))
38 with self.assertRaises(TypeError):
39 self.pt1 + self.pt2
40
41 def test_eq(self):
42 self.assertEqual(self.pt1, self.pt1)
43 self.assertNotEqual(self.pt1, self.pt2)
44 self.assertNotEqual(self.pt1, self.vec1)
45
46 def test_sub(self):
47 self.assertEqual(self.pt1 - self.pt2, Vector({self.x: -5, self.y: -35, self.z: -59}))
48 self.assertEqual(self.pt1 - self.vec1, Point({self.x: -10, self.y: -25, self.z: -39}))
49
50 def test_aspolyhedron(self):
51 self.assertEqual(self.pt1.aspolyhedron(), Eq(self.x, 10) & Eq(self.y, 5) & Eq(self.z, 1))
52
53
54 class TestVector(unittest.TestCase):
55
56 def setUp(self):
57 self.x = Symbol('x')
58 self.y = Symbol('y')
59 self.z = Symbol('z')
60 self.pt1 = Point({self.x: 10, self.y: 5, self.z: 1})
61 self.pt2 = Point({self.x: 15, self.y: 40, self.z: 60})
62 self.vec1 = Vector({self.x: 20, self.y: 30, self.z: 40})
63 self.vec2 = Vector({self.x: 45, self.y: 70, self.z: 80})
64
65 def test_add(self):
66 self.assertEqual(self.vec1 + self.pt1, Point({self.x: 30, self.y: 35, self.z: 41}))
67 self.assertEqual(self.vec1 + self.vec2, Vector({self.x: 65, self.y: 100, self.z: 120}))
68
69 def test_angle(self):
70 self.assertEqual(math.degrees(self.vec1.angle(self.vec1)), 0)
71 self.assertAlmostEqual(math.degrees(self.vec1.angle(self.vec2)), 4.15129, places=5)
72 self.assertAlmostEqual(math.degrees(self.vec2.angle(self.vec1)), 4.15129, places=5)
73
74 def test_cross(self):
75 self.assertEqual(self.vec1.cross(self.vec2), Vector({self.x: -400, self.y: 200, self.z: 50}))
76
77 def test_div(self):
78 self.assertEqual(self.vec1 / 10, Vector({self.x: 2, self.y: 3, self.z: 4}))
79
80 def test_dot(self):
81 self.assertEqual(self.vec1.dot(self.vec2), 6200)
82
83 def test_eq(self):
84 self.assertEqual(self.vec1, self.vec1)
85 self.assertNotEqual(self.vec1, self.vec2)
86
87 def test_mul(self):
88 self.assertEqual(75 * self.vec1, Vector({self.x: 1500, self.y: 2250, self.z: 3000}))
89 self.assertEqual(self.vec1 * 75, Vector({self.x: 1500, self.y: 2250, self.z: 3000}))
90
91 def test_neg(self):
92 self.assertEqual(-self.vec1, Vector({self.x: -20, self.y: -30, self.z: -40}))
93
94 def test_norm(self):
95 self.assertAlmostEqual(self.vec1.norm(), 53.85165, places=5)
96
97 def test_norm2(self):
98 self.assertEqual(self.vec1.norm2(), 2900)
99
100 def test_asunit(self):
101 unit = self.vec1.asunit()
102 self.assertAlmostEqual(unit[self.x], 0.37139, 5)
103 self.assertAlmostEqual(unit[self.y], 0.55709, 5)
104 self.assertAlmostEqual(unit[self.z], 0.74278, 5)
105
106 def test_sub(self):
107 self.assertEqual(self.vec1 - self.pt1, Point({self.x: 10, self.y: 25, self.z: 39}))
108 self.assertEqual(self.vec1 - self.vec2, Vector({self.x: -25, self.y: -40, self.z: -40}))