5 from abc
import ABC
, abstractmethod
6 from collections
import OrderedDict
, Mapping
8 from .linexprs
import Symbol
17 class Coordinates(ABC
):
29 return tuple(self
._coordinates
)
33 return len(self
.symbols
)
35 def coordinates(self
):
36 yield from self
._coordinates
.items()
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
]
43 __getitem__
= coordinate
46 return any(self
._coordinates
.values())
49 return hash(tuple(self
.coordinates()))
52 string
= ', '.join(['{!r}: {!r}'.format(symbol
, coordinate
)
53 for symbol
, coordinate
in self
.coordinates()])
54 return '{}({{{}}})'.format(self
.__class
__.__name
__, string
)
57 for symbol
, coordinate
in self
.coordinates():
58 yield symbol
, func(coordinate
)
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
)
67 def _map2(self
, other
, func
):
68 for symbol
, coordinate1
, coordinate2
in self
._iter
2(other
):
69 yield symbol
, func(coordinate1
, coordinate2
)
72 class Point(Coordinates
):
74 This class represents points in space.
77 def __new__(cls
, coordinates
=None):
78 if isinstance(coordinates
, Mapping
):
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
94 def __add__(self
, other
):
95 if not isinstance(other
, Vector
):
97 coordinates
= self
._map
2(other
, operator
.add
)
98 return Point(coordinates
)
100 def __sub__(self
, other
):
102 if isinstance(other
, Point
):
103 coordinates
= self
._map
2(other
, operator
.sub
)
104 return Vector(coordinates
)
105 elif isinstance(other
, Vector
):
106 coordinates
= self
._map
2(other
, operator
.sub
)
107 return Point(coordinates
)
109 return NotImplemented
111 def __eq__(self
, other
):
112 return isinstance(other
, Point
) and \
113 self
._coordinates
== other
._coordinates
115 def aspolyhedron(self
):
116 from .polyhedra
import Polyhedron
118 for symbol
, coordinate
in self
.coordinates():
119 equalities
.append(symbol
- coordinate
)
120 return Polyhedron(equalities
)
123 class Vector(Coordinates
):
125 This class represents displacements in space.
132 def __new__(cls
, initial
, terminal
=None):
133 self
= object().__new
__(cls
)
134 if not isinstance(initial
, Point
):
135 initial
= Point(initial
)
137 self
._coordinates
= initial
._coordinates
138 elif not isinstance(terminal
, Point
):
139 terminal
= Point(terminal
)
140 self
._coordinates
= terminal
._map
2(initial
, operator
.sub
)
144 return not bool(self
)
146 def __add__(self
, other
):
147 if isinstance(other
, (Point
, Vector
)):
148 coordinates
= self
._map
2(other
, operator
.add
)
149 return other
.__class
__(coordinates
)
150 return NotImplemented
152 def angle(self
, other
):
154 Retrieve the angle required to rotate the vector into the vector passed
155 in argument. The result is an angle in radians, ranging between -pi and
158 if not isinstance(other
, Vector
):
159 raise TypeError('argument must be a Vector instance')
160 cosinus
= self
.dot(other
) / (self
.norm()*other
.norm())
161 return math
.acos(cosinus
)
163 def cross(self
, other
):
165 Calculate the cross product of two Vector3D structures.
167 if not isinstance(other
, Vector
):
168 raise TypeError('other must be a Vector instance')
169 if self
.dimension
!= 3 or other
.dimension
!= 3:
170 raise ValueError('arguments must be three-dimensional vectors')
171 if self
.symbols
!= other
.symbols
:
172 raise ValueError('arguments must belong to the same space')
173 x
, y
, z
= self
.symbols
175 coordinates
.append((x
, self
[y
]*other
[z
] - self
[z
]*other
[y
]))
176 coordinates
.append((y
, self
[z
]*other
[x
] - self
[x
]*other
[z
]))
177 coordinates
.append((z
, self
[x
]*other
[y
] - self
[y
]*other
[x
]))
178 return Vector(coordinates
)
180 def __truediv__(self
, other
):
182 Divide the vector by the specified scalar and returns the result as a
185 if not isinstance(other
, numbers
.Real
):
186 return NotImplemented
187 coordinates
= self
._map
(lambda coordinate
: coordinate
/ other
)
188 return Vector(coordinates
)
190 def dot(self
, other
):
192 Calculate the dot product of two vectors.
194 if not isinstance(other
, Vector
):
195 raise TypeError('argument must be a Vector instance')
197 for symbol
, coordinate1
, coordinate2
in self
._iter
2(other
):
198 result
+= coordinate1
* coordinate2
201 def __eq__(self
, other
):
202 return isinstance(other
, Vector
) and \
203 self
._coordinates
== other
._coordinates
206 return hash(tuple(self
.coordinates()))
208 def __mul__(self
, other
):
209 if not isinstance(other
, numbers
.Real
):
210 return NotImplemented
211 coordinates
= self
._map
(lambda coordinate
: other
* coordinate
)
212 return Vector(coordinates
)
217 coordinates
= self
._map
(operator
.neg
)
218 return Vector(coordinates
)
221 return math
.sqrt(self
.norm2())
225 for coordinate
in self
._coordinates
.values():
226 result
+= coordinate
** 2
230 return self
/ self
.norm()
232 def __sub__(self
, other
):
233 if isinstance(other
, (Point
, Vector
)):
234 coordinates
= self
._map
2(other
, operator
.sub
)
235 return other
.__class
__(coordinates
)
236 return NotImplemented