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 return any(self
._coordinates
.values())
78 return hash(tuple(self
.coordinates()))
81 string
= ', '.join(['{!r}: {!r}'.format(symbol
, coordinate
)
82 for symbol
, coordinate
in self
.coordinates()])
83 return '{}({{{}}})'.format(self
.__class
__.__name
__, string
)
86 for symbol
, coordinate
in self
.coordinates():
87 yield symbol
, func(coordinate
)
89 def _iter2(self
, other
):
90 if self
.symbols
!= other
.symbols
:
91 raise ValueError('arguments must belong to the same space')
92 coordinates1
= self
._coordinates
.values()
93 coordinates2
= other
._coordinates
.values()
94 yield from zip(self
.symbols
, coordinates1
, coordinates2
)
96 def _map2(self
, other
, func
):
97 for symbol
, coordinate1
, coordinate2
in self
._iter
2(other
):
98 yield symbol
, func(coordinate1
, coordinate2
)
101 class Point(Coordinates
, GeometricObject
):
103 This class represents points in space.
107 return not bool(self
)
110 return super().__hash
__()
112 def __add__(self
, other
):
113 if not isinstance(other
, Vector
):
114 return NotImplemented
115 coordinates
= self
._map
2(other
, operator
.add
)
116 return Point(coordinates
)
118 def __sub__(self
, other
):
120 if isinstance(other
, Point
):
121 coordinates
= self
._map
2(other
, operator
.sub
)
122 return Vector(coordinates
)
123 elif isinstance(other
, Vector
):
124 coordinates
= self
._map
2(other
, operator
.sub
)
125 return Point(coordinates
)
127 return NotImplemented
129 def __eq__(self
, other
):
130 return isinstance(other
, Point
) and \
131 self
._coordinates
== other
._coordinates
133 def aspolyhedron(self
):
134 from .polyhedra
import Polyhedron
136 for symbol
, coordinate
in self
.coordinates():
137 equalities
.append(symbol
- coordinate
)
138 return Polyhedron(equalities
)
141 class Vector(Coordinates
):
143 This class represents displacements in space.
146 def __new__(cls
, initial
, terminal
=None):
147 if not isinstance(initial
, Point
):
148 initial
= Point(initial
)
150 coordinates
= initial
._coordinates
152 if not isinstance(terminal
, Point
):
153 terminal
= Point(terminal
)
154 coordinates
= terminal
._map
2(initial
, operator
.sub
)
155 return super().__new
__(cls
, coordinates
)
158 return not bool(self
)
161 return super().__hash
__()
163 def __add__(self
, other
):
164 if isinstance(other
, (Point
, Vector
)):
165 coordinates
= self
._map
2(other
, operator
.add
)
166 return other
.__class
__(coordinates
)
167 return NotImplemented
169 def angle(self
, other
):
171 Retrieve the angle required to rotate the vector into the vector passed
172 in argument. The result is an angle in radians, ranging between -pi and
175 if not isinstance(other
, Vector
):
176 raise TypeError('argument must be a Vector instance')
177 cosinus
= self
.dot(other
) / (self
.norm()*other
.norm())
178 return math
.acos(cosinus
)
180 def cross(self
, other
):
182 Calculate the cross product of two Vector3D structures.
184 if not isinstance(other
, Vector
):
185 raise TypeError('other must be a Vector instance')
186 if self
.dimension
!= 3 or other
.dimension
!= 3:
187 raise ValueError('arguments must be three-dimensional vectors')
188 if self
.symbols
!= other
.symbols
:
189 raise ValueError('arguments must belong to the same space')
190 x
, y
, z
= self
.symbols
192 coordinates
.append((x
, self
[y
]*other
[z
] - self
[z
]*other
[y
]))
193 coordinates
.append((y
, self
[z
]*other
[x
] - self
[x
]*other
[z
]))
194 coordinates
.append((z
, self
[x
]*other
[y
] - self
[y
]*other
[x
]))
195 return Vector(coordinates
)
197 def __truediv__(self
, other
):
199 Divide the vector by the specified scalar and returns the result as a
202 if not isinstance(other
, numbers
.Real
):
203 return NotImplemented
204 coordinates
= self
._map
(lambda coordinate
: coordinate
/ other
)
205 return Vector(coordinates
)
207 def dot(self
, other
):
209 Calculate the dot product of two vectors.
211 if not isinstance(other
, Vector
):
212 raise TypeError('argument must be a Vector instance')
214 for symbol
, coordinate1
, coordinate2
in self
._iter
2(other
):
215 result
+= coordinate1
* coordinate2
218 def __eq__(self
, other
):
219 return isinstance(other
, Vector
) and \
220 self
._coordinates
== other
._coordinates
223 return hash(tuple(self
.coordinates()))
225 def __mul__(self
, other
):
226 if not isinstance(other
, numbers
.Real
):
227 return NotImplemented
228 coordinates
= self
._map
(lambda coordinate
: other
* coordinate
)
229 return Vector(coordinates
)
234 coordinates
= self
._map
(operator
.neg
)
235 return Vector(coordinates
)
238 return math
.sqrt(self
.norm2())
242 for coordinate
in self
._coordinates
.values():
243 result
+= coordinate
** 2
247 return self
/ self
.norm()
249 def __sub__(self
, other
):
250 if isinstance(other
, (Point
, Vector
)):
251 coordinates
= self
._map
2(other
, operator
.sub
)
252 return other
.__class
__(coordinates
)
253 return NotImplemented