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
)
109 def __add__(self
, other
):
110 if not isinstance(other
, Vector
):
111 return NotImplemented
112 coordinates
= self
._map
2(other
, operator
.add
)
113 return Point(coordinates
)
115 def __sub__(self
, other
):
117 if isinstance(other
, Point
):
118 coordinates
= self
._map
2(other
, operator
.sub
)
119 return Vector(coordinates
)
120 elif isinstance(other
, Vector
):
121 coordinates
= self
._map
2(other
, operator
.sub
)
122 return Point(coordinates
)
124 return NotImplemented
126 def __eq__(self
, other
):
127 return isinstance(other
, Point
) and \
128 self
._coordinates
== other
._coordinates
130 def aspolyhedron(self
):
131 from .polyhedra
import Polyhedron
133 for symbol
, coordinate
in self
.coordinates():
134 equalities
.append(symbol
- coordinate
)
135 return Polyhedron(equalities
)
138 class Vector(Coordinates
):
140 This class represents displacements in space.
143 def __new__(cls
, initial
, terminal
=None):
144 if not isinstance(initial
, Point
):
145 initial
= Point(initial
)
147 coordinates
= initial
._coordinates
148 elif not isinstance(terminal
, Point
):
149 terminal
= Point(terminal
)
150 coordinates
= terminal
._map
2(initial
, operator
.sub
)
151 return super().__new
__(cls
, coordinates
)
154 return not bool(self
)
156 def __add__(self
, other
):
157 if isinstance(other
, (Point
, Vector
)):
158 coordinates
= self
._map
2(other
, operator
.add
)
159 return other
.__class
__(coordinates
)
160 return NotImplemented
162 def angle(self
, other
):
164 Retrieve the angle required to rotate the vector into the vector passed
165 in argument. The result is an angle in radians, ranging between -pi and
168 if not isinstance(other
, Vector
):
169 raise TypeError('argument must be a Vector instance')
170 cosinus
= self
.dot(other
) / (self
.norm()*other
.norm())
171 return math
.acos(cosinus
)
173 def cross(self
, other
):
175 Calculate the cross product of two Vector3D structures.
177 if not isinstance(other
, Vector
):
178 raise TypeError('other must be a Vector instance')
179 if self
.dimension
!= 3 or other
.dimension
!= 3:
180 raise ValueError('arguments must be three-dimensional vectors')
181 if self
.symbols
!= other
.symbols
:
182 raise ValueError('arguments must belong to the same space')
183 x
, y
, z
= self
.symbols
185 coordinates
.append((x
, self
[y
]*other
[z
] - self
[z
]*other
[y
]))
186 coordinates
.append((y
, self
[z
]*other
[x
] - self
[x
]*other
[z
]))
187 coordinates
.append((z
, self
[x
]*other
[y
] - self
[y
]*other
[x
]))
188 return Vector(coordinates
)
190 def __truediv__(self
, other
):
192 Divide the vector by the specified scalar and returns the result as a
195 if not isinstance(other
, numbers
.Real
):
196 return NotImplemented
197 coordinates
= self
._map
(lambda coordinate
: coordinate
/ other
)
198 return Vector(coordinates
)
200 def dot(self
, other
):
202 Calculate the dot product of two vectors.
204 if not isinstance(other
, Vector
):
205 raise TypeError('argument must be a Vector instance')
207 for symbol
, coordinate1
, coordinate2
in self
._iter
2(other
):
208 result
+= coordinate1
* coordinate2
211 def __eq__(self
, other
):
212 return isinstance(other
, Vector
) and \
213 self
._coordinates
== other
._coordinates
216 return hash(tuple(self
.coordinates()))
218 def __mul__(self
, other
):
219 if not isinstance(other
, numbers
.Real
):
220 return NotImplemented
221 coordinates
= self
._map
(lambda coordinate
: other
* coordinate
)
222 return Vector(coordinates
)
227 coordinates
= self
._map
(operator
.neg
)
228 return Vector(coordinates
)
231 return math
.sqrt(self
.norm2())
235 for coordinate
in self
._coordinates
.values():
236 result
+= coordinate
** 2
240 return self
/ self
.norm()
242 def __sub__(self
, other
):
243 if isinstance(other
, (Point
, Vector
)):
244 coordinates
= self
._map
2(other
, operator
.sub
)
245 return other
.__class
__(coordinates
)
246 return NotImplemented