ceab418e33a90699aab78236f614781c27cf89b0
[linpy.git] / pypol / coordinates.py
1 import math
2 import numbers
3 import operator
4
5 from collections import OrderedDict, Mapping
6
7 from .geometry import GeometricObject
8 from .linexprs import Symbol
9
10
11 __all__ = [
12 'Point',
13 'Vector',
14 ]
15
16
17 class Coordinates:
18
19 __slots__ = (
20 '_coordinates',
21 )
22
23 def __new__(cls, coordinates):
24 if isinstance(coordinates, Mapping):
25 coordinates = coordinates.items()
26 self = object().__new__(cls)
27 self._coordinates = OrderedDict()
28 for symbol, coordinate in sorted(coordinates,
29 key=lambda item: item[0].sortkey()):
30 if not isinstance(symbol, Symbol):
31 raise TypeError('symbols must be Symbol instances')
32 if not isinstance(coordinate, numbers.Real):
33 raise TypeError('coordinates must be real numbers')
34 self._coordinates[symbol] = coordinate
35 return self
36
37 @property
38 def symbols(self):
39 return tuple(self._coordinates)
40
41 @property
42 def dimension(self):
43 return len(self.symbols)
44
45 def coordinates(self):
46 yield from self._coordinates.items()
47
48 def coordinate(self, symbol):
49 if not isinstance(symbol, Symbol):
50 raise TypeError('symbol must be a Symbol instance')
51 return self._coordinates[symbol]
52
53 __getitem__ = coordinate
54
55 def __bool__(self):
56 return any(self._coordinates.values())
57
58 def __hash__(self):
59 return hash(tuple(self.coordinates()))
60
61 def __repr__(self):
62 string = ', '.join(['{!r}: {!r}'.format(symbol, coordinate)
63 for symbol, coordinate in self.coordinates()])
64 return '{}({{{}}})'.format(self.__class__.__name__, string)
65
66 def _map(self, func):
67 for symbol, coordinate in self.coordinates():
68 yield symbol, func(coordinate)
69
70 def _iter2(self, other):
71 if self.symbols != other.symbols:
72 raise ValueError('arguments must belong to the same space')
73 coordinates1 = self._coordinates.values()
74 coordinates2 = other._coordinates.values()
75 yield from zip(self.symbols, coordinates1, coordinates2)
76
77 def _map2(self, other, func):
78 for symbol, coordinate1, coordinate2 in self._iter2(other):
79 yield symbol, func(coordinate1, coordinate2)
80
81
82 class Point(Coordinates, GeometricObject):
83 """
84 This class represents points in space.
85 """
86
87 def isorigin(self):
88 return not bool(self)
89
90 def __add__(self, other):
91 if not isinstance(other, Vector):
92 return NotImplemented
93 coordinates = self._map2(other, operator.add)
94 return Point(coordinates)
95
96 def __sub__(self, other):
97 coordinates = []
98 if isinstance(other, Point):
99 coordinates = self._map2(other, operator.sub)
100 return Vector(coordinates)
101 elif isinstance(other, Vector):
102 coordinates = self._map2(other, operator.sub)
103 return Point(coordinates)
104 else:
105 return NotImplemented
106
107 def __eq__(self, other):
108 return isinstance(other, Point) and \
109 self._coordinates == other._coordinates
110
111 def aspolyhedron(self):
112 from .polyhedra import Polyhedron
113 equalities = []
114 for symbol, coordinate in self.coordinates():
115 equalities.append(symbol - coordinate)
116 return Polyhedron(equalities)
117
118
119 class Vector(Coordinates):
120 """
121 This class represents displacements in space.
122 """
123
124 def __new__(cls, initial, terminal=None):
125 if not isinstance(initial, Point):
126 initial = Point(initial)
127 if terminal is None:
128 coordinates = initial._coordinates
129 elif not isinstance(terminal, Point):
130 terminal = Point(terminal)
131 coordinates = terminal._map2(initial, operator.sub)
132 return super().__new__(cls, coordinates)
133
134 def isnull(self):
135 return not bool(self)
136
137 def __add__(self, other):
138 if isinstance(other, (Point, Vector)):
139 coordinates = self._map2(other, operator.add)
140 return other.__class__(coordinates)
141 return NotImplemented
142
143 def angle(self, other):
144 """
145 Retrieve the angle required to rotate the vector into the vector passed
146 in argument. The result is an angle in radians, ranging between -pi and
147 pi.
148 """
149 if not isinstance(other, Vector):
150 raise TypeError('argument must be a Vector instance')
151 cosinus = self.dot(other) / (self.norm()*other.norm())
152 return math.acos(cosinus)
153
154 def cross(self, other):
155 """
156 Calculate the cross product of two Vector3D structures.
157 """
158 if not isinstance(other, Vector):
159 raise TypeError('other must be a Vector instance')
160 if self.dimension != 3 or other.dimension != 3:
161 raise ValueError('arguments must be three-dimensional vectors')
162 if self.symbols != other.symbols:
163 raise ValueError('arguments must belong to the same space')
164 x, y, z = self.symbols
165 coordinates = []
166 coordinates.append((x, self[y]*other[z] - self[z]*other[y]))
167 coordinates.append((y, self[z]*other[x] - self[x]*other[z]))
168 coordinates.append((z, self[x]*other[y] - self[y]*other[x]))
169 return Vector(coordinates)
170
171 def __truediv__(self, other):
172 """
173 Divide the vector by the specified scalar and returns the result as a
174 vector.
175 """
176 if not isinstance(other, numbers.Real):
177 return NotImplemented
178 coordinates = self._map(lambda coordinate: coordinate / other)
179 return Vector(coordinates)
180
181 def dot(self, other):
182 """
183 Calculate the dot product of two vectors.
184 """
185 if not isinstance(other, Vector):
186 raise TypeError('argument must be a Vector instance')
187 result = 0
188 for symbol, coordinate1, coordinate2 in self._iter2(other):
189 result += coordinate1 * coordinate2
190 return result
191
192 def __eq__(self, other):
193 return isinstance(other, Vector) and \
194 self._coordinates == other._coordinates
195
196 def __hash__(self):
197 return hash(tuple(self.coordinates()))
198
199 def __mul__(self, other):
200 if not isinstance(other, numbers.Real):
201 return NotImplemented
202 coordinates = self._map(lambda coordinate: other * coordinate)
203 return Vector(coordinates)
204
205 __rmul__ = __mul__
206
207 def __neg__(self):
208 coordinates = self._map(operator.neg)
209 return Vector(coordinates)
210
211 def norm(self):
212 return math.sqrt(self.norm2())
213
214 def norm2(self):
215 result = 0
216 for coordinate in self._coordinates.values():
217 result += coordinate ** 2
218 return result
219
220 def asunit(self):
221 return self / self.norm()
222
223 def __sub__(self, other):
224 if isinstance(other, (Point, Vector)):
225 coordinates = self._map2(other, operator.sub)
226 return other.__class__(coordinates)
227 return NotImplemented