5 from collections
import OrderedDict
, Mapping
7 from .linexprs
import Symbol
22 def __new__(cls
, coordinates
):
23 if isinstance(coordinates
, Mapping
):
24 coordinates
= coordinates
.items()
25 self
= object().__new
__(cls
)
26 self
._coordinates
= OrderedDict()
27 for symbol
, coordinate
in sorted(coordinates
,
28 key
=lambda item
: item
[0].sortkey()):
29 if not isinstance(symbol
, Symbol
):
30 raise TypeError('symbols must be Symbol instances')
31 if not isinstance(coordinate
, numbers
.Real
):
32 raise TypeError('coordinates must be real numbers')
33 self
._coordinates
[symbol
] = coordinate
38 return tuple(self
._coordinates
)
42 return len(self
.symbols
)
44 def coordinates(self
):
45 yield from self
._coordinates
.items()
47 def coordinate(self
, symbol
):
48 if not isinstance(symbol
, Symbol
):
49 raise TypeError('symbol must be a Symbol instance')
50 return self
._coordinates
[symbol
]
52 __getitem__
= coordinate
55 return any(self
._coordinates
.values())
58 return hash(tuple(self
.coordinates()))
61 string
= ', '.join(['{!r}: {!r}'.format(symbol
, coordinate
)
62 for symbol
, coordinate
in self
.coordinates()])
63 return '{}({{{}}})'.format(self
.__class
__.__name
__, string
)
66 for symbol
, coordinate
in self
.coordinates():
67 yield symbol
, func(coordinate
)
69 def _iter2(self
, other
):
70 if self
.symbols
!= other
.symbols
:
71 raise ValueError('arguments must belong to the same space')
72 coordinates1
= self
._coordinates
.values()
73 coordinates2
= other
._coordinates
.values()
74 yield from zip(self
.symbols
, coordinates1
, coordinates2
)
76 def _map2(self
, other
, func
):
77 for symbol
, coordinate1
, coordinate2
in self
._iter
2(other
):
78 yield symbol
, func(coordinate1
, coordinate2
)
81 class Point(Coordinates
):
83 This class represents points in space.
89 def __add__(self
, other
):
90 if not isinstance(other
, Vector
):
92 coordinates
= self
._map
2(other
, operator
.add
)
93 return Point(coordinates
)
95 def __sub__(self
, other
):
97 if isinstance(other
, Point
):
98 coordinates
= self
._map
2(other
, operator
.sub
)
99 return Vector(coordinates
)
100 elif isinstance(other
, Vector
):
101 coordinates
= self
._map
2(other
, operator
.sub
)
102 return Point(coordinates
)
104 return NotImplemented
106 def __eq__(self
, other
):
107 return isinstance(other
, Point
) and \
108 self
._coordinates
== other
._coordinates
110 def aspolyhedron(self
):
111 from .polyhedra
import Polyhedron
113 for symbol
, coordinate
in self
.coordinates():
114 equalities
.append(symbol
- coordinate
)
115 return Polyhedron(equalities
)
118 class Vector(Coordinates
):
120 This class represents displacements in space.
127 def __new__(cls
, initial
, terminal
=None):
128 if not isinstance(initial
, Point
):
129 initial
= Point(initial
)
131 coordinates
= initial
._coordinates
132 elif not isinstance(terminal
, Point
):
133 terminal
= Point(terminal
)
134 coordinates
= terminal
._map
2(initial
, operator
.sub
)
135 return super().__new
__(cls
, coordinates
)
138 return not bool(self
)
140 def __add__(self
, other
):
141 if isinstance(other
, (Point
, Vector
)):
142 coordinates
= self
._map
2(other
, operator
.add
)
143 return other
.__class
__(coordinates
)
144 return NotImplemented
146 def angle(self
, other
):
148 Retrieve the angle required to rotate the vector into the vector passed
149 in argument. The result is an angle in radians, ranging between -pi and
152 if not isinstance(other
, Vector
):
153 raise TypeError('argument must be a Vector instance')
154 cosinus
= self
.dot(other
) / (self
.norm()*other
.norm())
155 return math
.acos(cosinus
)
157 def cross(self
, other
):
159 Calculate the cross product of two Vector3D structures.
161 if not isinstance(other
, Vector
):
162 raise TypeError('other must be a Vector instance')
163 if self
.dimension
!= 3 or other
.dimension
!= 3:
164 raise ValueError('arguments must be three-dimensional vectors')
165 if self
.symbols
!= other
.symbols
:
166 raise ValueError('arguments must belong to the same space')
167 x
, y
, z
= self
.symbols
169 coordinates
.append((x
, self
[y
]*other
[z
] - self
[z
]*other
[y
]))
170 coordinates
.append((y
, self
[z
]*other
[x
] - self
[x
]*other
[z
]))
171 coordinates
.append((z
, self
[x
]*other
[y
] - self
[y
]*other
[x
]))
172 return Vector(coordinates
)
174 def __truediv__(self
, other
):
176 Divide the vector by the specified scalar and returns the result as a
179 if not isinstance(other
, numbers
.Real
):
180 return NotImplemented
181 coordinates
= self
._map
(lambda coordinate
: coordinate
/ other
)
182 return Vector(coordinates
)
184 def dot(self
, other
):
186 Calculate the dot product of two vectors.
188 if not isinstance(other
, Vector
):
189 raise TypeError('argument must be a Vector instance')
191 for symbol
, coordinate1
, coordinate2
in self
._iter
2(other
):
192 result
+= coordinate1
* coordinate2
195 def __eq__(self
, other
):
196 return isinstance(other
, Vector
) and \
197 self
._coordinates
== other
._coordinates
200 return hash(tuple(self
.coordinates()))
202 def __mul__(self
, other
):
203 if not isinstance(other
, numbers
.Real
):
204 return NotImplemented
205 coordinates
= self
._map
(lambda coordinate
: other
* coordinate
)
206 return Vector(coordinates
)
211 coordinates
= self
._map
(operator
.neg
)
212 return Vector(coordinates
)
215 return math
.sqrt(self
.norm2())
219 for coordinate
in self
._coordinates
.values():
220 result
+= coordinate
** 2
224 return self
/ self
.norm()
226 def __sub__(self
, other
):
227 if isinstance(other
, (Point
, Vector
)):
228 coordinates
= self
._map
2(other
, operator
.sub
)
229 return other
.__class
__(coordinates
)
230 return NotImplemented