X-Git-Url: https://scm.cri.ensmp.fr/git/linpy.git/blobdiff_plain/064e82c1b36bfdef47a5bcbbbf7757c7b9adea91..cc1d83eaadffc1d5de296e2ec2b401d04de70c41:/linpy/geometry.py diff --git a/linpy/geometry.py b/linpy/geometry.py index a0a7b7c..80f7771 100644 --- a/linpy/geometry.py +++ b/linpy/geometry.py @@ -19,8 +19,8 @@ import math import numbers import operator -from abc import ABC, abstractproperty, abstractmethod -from collections import OrderedDict, Mapping +from abc import ABC, abstractmethod, abstractproperty +from collections import Mapping, OrderedDict from .linexprs import Symbol @@ -79,20 +79,21 @@ class Coordinates: def __new__(cls, coordinates): """ - Create a coordinate system from a dictionary or a sequence that maps the - symbols to their coordinates. Coordinates must be rational numbers. + Create a coordinate system from a dictionary or a sequence that maps + the symbols to their coordinates. Coordinates must be rational numbers. """ if isinstance(coordinates, Mapping): coordinates = coordinates.items() self = object().__new__(cls) - self._coordinates = OrderedDict() - for symbol, coordinate in sorted(coordinates, - key=lambda item: item[0].sortkey()): + self._coordinates = [] + for symbol, coordinate in coordinates: if not isinstance(symbol, Symbol): raise TypeError('symbols must be Symbol instances') if not isinstance(coordinate, numbers.Real): raise TypeError('coordinates must be real numbers') - self._coordinates[symbol] = coordinate + self._coordinates.append((symbol, coordinate)) + self._coordinates.sort(key=lambda item: item[0].sortkey()) + self._coordinates = OrderedDict(self._coordinates) return self @property @@ -141,12 +142,20 @@ class Coordinates: """ return any(self._coordinates.values()) + def __eq__(self, other): + """ + Return True if two coordinate systems are equal. + """ + if isinstance(other, self.__class__): + return self._coordinates == other._coordinates + return NotImplemented + def __hash__(self): return hash(tuple(self.coordinates())) def __repr__(self): string = ', '.join(['{!r}: {!r}'.format(symbol, coordinate) - for symbol, coordinate in self.coordinates()]) + for symbol, coordinate in self.coordinates()]) return '{}({{{}}})'.format(self.__class__.__name__, string) def _map(self, func): @@ -193,8 +202,8 @@ class Point(Coordinates, GeometricObject): def __sub__(self, other): """ If other is a point, substract it from self and return the resulting - vector. If other is a vector, translate the point by the opposite vector - and returns the resulting point. + vector. If other is a vector, translate the point by the opposite + vector and returns the resulting point. """ coordinates = [] if isinstance(other, Point): @@ -205,14 +214,6 @@ class Point(Coordinates, GeometricObject): return Point(coordinates) return NotImplemented - def __eq__(self, other): - """ - Test whether two points are equal. - """ - if isinstance(other, Point): - return self._coordinates == other._coordinates - return NotImplemented - def aspolyhedron(self): from .polyhedra import Polyhedron equalities = [] @@ -230,8 +231,8 @@ class Vector(Coordinates): def __new__(cls, initial, terminal=None): """ - Create a vector from a dictionary or a sequence that maps the symbols to - their coordinates, or as the difference between two points. + Create a vector from a dictionary or a sequence that maps the symbols + to their coordinates, or as the displacement between two points. """ if not isinstance(initial, Point): initial = Point(initial) @@ -283,10 +284,10 @@ class Vector(Coordinates): """ Multiplies a Vector by a scalar value. """ - if not isinstance(other, numbers.Real): - return NotImplemented - coordinates = self._map(lambda coordinate: other * coordinate) - return Vector(coordinates) + if isinstance(other, numbers.Real): + coordinates = self._map(lambda coordinate: other * coordinate) + return Vector(coordinates) + return NotImplemented __rmul__ = __mul__ @@ -295,17 +296,10 @@ class Vector(Coordinates): Divide the vector by the specified scalar and returns the result as a vector. """ - if not isinstance(other, numbers.Real): - return NotImplemented - coordinates = self._map(lambda coordinate: coordinate / other) - return Vector(coordinates) - - def __eq__(self, other): - """ - Test whether two vectors are equal. - """ - return isinstance(other, Vector) and \ - self._coordinates == other._coordinates + if isinstance(other, numbers.Real): + coordinates = self._map(lambda coordinate: coordinate / other) + return Vector(coordinates) + return NotImplemented def angle(self, other): """ @@ -321,7 +315,7 @@ class Vector(Coordinates): def cross(self, other): """ Compute the cross product of two 3D vectors. If either one of the - vectors is not tridimensional, a ValueError exception is raised. + vectors is not three-dimensional, a ValueError exception is raised. """ if not isinstance(other, Vector): raise TypeError('other must be a Vector instance') @@ -347,9 +341,6 @@ class Vector(Coordinates): result += coordinate1 * coordinate2 return result - def __hash__(self): - return hash(tuple(self.coordinates())) - def norm(self): """ Return the norm of the vector. @@ -367,7 +358,7 @@ class Vector(Coordinates): def asunit(self): """ - Return the normalized vector, i.e. the vector of same direction but with - norm 1. + Return the normalized vector, i.e. the vector of same direction but + with norm 1. """ return self / self.norm()