1dc50eb345f734fd79d213d93f3a26d8d14b6bb7
5 from abc
import ABC
, abstractproperty
, abstractmethod
6 from collections
import OrderedDict
, Mapping
8 from .linexprs
import Symbol
18 class GeometricObject(ABC
):
26 return len(self
.symbols
)
29 def aspolyhedron(self
):
33 return self
.aspolyhedron()
42 def __new__(cls
, coordinates
):
43 if isinstance(coordinates
, Mapping
):
44 coordinates
= coordinates
.items()
45 self
= object().__new
__(cls
)
46 self
._coordinates
= OrderedDict()
47 for symbol
, coordinate
in sorted(coordinates
,
48 key
=lambda item
: item
[0].sortkey()):
49 if not isinstance(symbol
, Symbol
):
50 raise TypeError('symbols must be Symbol instances')
51 if not isinstance(coordinate
, numbers
.Real
):
52 raise TypeError('coordinates must be real numbers')
53 self
._coordinates
[symbol
] = coordinate
58 return tuple(self
._coordinates
)
62 return len(self
.symbols
)
64 def coordinates(self
):
65 yield from self
._coordinates
.items()
67 def coordinate(self
, symbol
):
68 if not isinstance(symbol
, Symbol
):
69 raise TypeError('symbol must be a Symbol instance')
70 return self
._coordinates
[symbol
]
72 __getitem__
= coordinate
75 yield from self
._coordinates
.values()
78 return any(self
._coordinates
.values())
81 return hash(tuple(self
.coordinates()))
84 string
= ', '.join(['{!r}: {!r}'.format(symbol
, coordinate
)
85 for symbol
, coordinate
in self
.coordinates()])
86 return '{}({{{}}})'.format(self
.__class
__.__name
__, string
)
89 for symbol
, coordinate
in self
.coordinates():
90 yield symbol
, func(coordinate
)
92 def _iter2(self
, other
):
93 if self
.symbols
!= other
.symbols
:
94 raise ValueError('arguments must belong to the same space')
95 coordinates1
= self
._coordinates
.values()
96 coordinates2
= other
._coordinates
.values()
97 yield from zip(self
.symbols
, coordinates1
, coordinates2
)
99 def _map2(self
, other
, func
):
100 for symbol
, coordinate1
, coordinate2
in self
._iter
2(other
):
101 yield symbol
, func(coordinate1
, coordinate2
)
104 class Point(Coordinates
, GeometricObject
):
106 This class represents points in space.
110 return not bool(self
)
113 return super().__hash
__()
115 def __add__(self
, other
):
116 if not isinstance(other
, Vector
):
117 return NotImplemented
118 coordinates
= self
._map
2(other
, operator
.add
)
119 return Point(coordinates
)
121 def __sub__(self
, other
):
123 if isinstance(other
, Point
):
124 coordinates
= self
._map
2(other
, operator
.sub
)
125 return Vector(coordinates
)
126 elif isinstance(other
, Vector
):
127 coordinates
= self
._map
2(other
, operator
.sub
)
128 return Point(coordinates
)
130 return NotImplemented
132 def __eq__(self
, other
):
133 return isinstance(other
, Point
) and \
134 self
._coordinates
== other
._coordinates
136 def aspolyhedron(self
):
137 from .polyhedra
import Polyhedron
139 for symbol
, coordinate
in self
.coordinates():
140 equalities
.append(symbol
- coordinate
)
141 return Polyhedron(equalities
)
144 class Vector(Coordinates
):
146 This class represents displacements in space.
149 def __new__(cls
, initial
, terminal
=None):
150 if not isinstance(initial
, Point
):
151 initial
= Point(initial
)
153 coordinates
= initial
._coordinates
155 if not isinstance(terminal
, Point
):
156 terminal
= Point(terminal
)
157 coordinates
= terminal
._map
2(initial
, operator
.sub
)
158 return super().__new
__(cls
, coordinates
)
161 return not bool(self
)
164 return super().__hash
__()
166 def __add__(self
, other
):
167 if isinstance(other
, (Point
, Vector
)):
168 coordinates
= self
._map
2(other
, operator
.add
)
169 return other
.__class
__(coordinates
)
170 return NotImplemented
172 def angle(self
, other
):
174 Retrieve the angle required to rotate the vector into the vector passed
175 in argument. The result is an angle in radians, ranging between -pi and
178 if not isinstance(other
, Vector
):
179 raise TypeError('argument must be a Vector instance')
180 cosinus
= self
.dot(other
) / (self
.norm()*other
.norm())
181 return math
.acos(cosinus
)
183 def cross(self
, other
):
185 Calculate the cross product of two Vector3D structures.
187 if not isinstance(other
, Vector
):
188 raise TypeError('other must be a Vector instance')
189 if self
.dimension
!= 3 or other
.dimension
!= 3:
190 raise ValueError('arguments must be three-dimensional vectors')
191 if self
.symbols
!= other
.symbols
:
192 raise ValueError('arguments must belong to the same space')
193 x
, y
, z
= self
.symbols
195 coordinates
.append((x
, self
[y
]*other
[z
] - self
[z
]*other
[y
]))
196 coordinates
.append((y
, self
[z
]*other
[x
] - self
[x
]*other
[z
]))
197 coordinates
.append((z
, self
[x
]*other
[y
] - self
[y
]*other
[x
]))
198 return Vector(coordinates
)
200 def __truediv__(self
, other
):
202 Divide the vector by the specified scalar and returns the result as a
205 if not isinstance(other
, numbers
.Real
):
206 return NotImplemented
207 coordinates
= self
._map
(lambda coordinate
: coordinate
/ other
)
208 return Vector(coordinates
)
210 def dot(self
, other
):
212 Calculate the dot product of two vectors.
214 if not isinstance(other
, Vector
):
215 raise TypeError('argument must be a Vector instance')
217 for symbol
, coordinate1
, coordinate2
in self
._iter
2(other
):
218 result
+= coordinate1
* coordinate2
221 def __eq__(self
, other
):
222 return isinstance(other
, Vector
) and \
223 self
._coordinates
== other
._coordinates
226 return hash(tuple(self
.coordinates()))
228 def __mul__(self
, other
):
229 if not isinstance(other
, numbers
.Real
):
230 return NotImplemented
231 coordinates
= self
._map
(lambda coordinate
: other
* coordinate
)
232 return Vector(coordinates
)
237 coordinates
= self
._map
(operator
.neg
)
238 return Vector(coordinates
)
241 return math
.sqrt(self
.norm2())
245 for coordinate
in self
._coordinates
.values():
246 result
+= coordinate
** 2
250 return self
/ self
.norm()
252 def __sub__(self
, other
):
253 if isinstance(other
, (Point
, Vector
)):
254 coordinates
= self
._map
2(other
, operator
.sub
)
255 return other
.__class
__(coordinates
)
256 return NotImplemented