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