From: Vivien Maisonneuve Date: Tue, 19 Aug 2014 13:05:03 +0000 (+0200) Subject: Minor improvements in class Vector X-Git-Tag: 1.0~47 X-Git-Url: https://scm.cri.mines-paristech.fr/git/linpy.git/commitdiff_plain/af70cb24e2db153fa0afad42296cc1528b82b2a2?hp=064e82c1b36bfdef47a5bcbbbf7757c7b9adea91 Minor improvements in class Vector --- diff --git a/doc/reference.rst b/doc/reference.rst index de5300d..4e71658 100644 --- a/doc/reference.rst +++ b/doc/reference.rst @@ -677,7 +677,7 @@ Geometric Objects .. method:: cross(vector) Compute the cross product of two 3D vectors. - If either one of the vectors is not tridimensional, a :exc:`ValueError` exception is raised. + If either one of the vectors is not three-dimensional, a :exc:`ValueError` exception is raised. .. method:: dot(vector) diff --git a/linpy/geometry.py b/linpy/geometry.py index a0a7b7c..43344e4 100644 --- a/linpy/geometry.py +++ b/linpy/geometry.py @@ -231,7 +231,7 @@ 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. + their coordinates, or as the displacement between two points. """ if not isinstance(initial, Point): initial = Point(initial) @@ -283,10 +283,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 +295,18 @@ 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) + if isinstance(other, numbers.Real): + coordinates = self._map(lambda coordinate: coordinate / other) + return Vector(coordinates) + return NotImplemented def __eq__(self, other): """ Test whether two vectors are equal. """ - return isinstance(other, Vector) and \ - self._coordinates == other._coordinates + if isinstance(other, Vector): + return self._coordinates == other._coordinates + return NotImplemented def angle(self, other): """ @@ -321,7 +322,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') @@ -348,7 +349,7 @@ class Vector(Coordinates): return result def __hash__(self): - return hash(tuple(self.coordinates())) + return super().__hash__() def norm(self): """