[arrayTools] fix mixed indentation
This particual file originally contained spaces, so we revert to that... sorry.
This commit is contained in:
parent
558dad9028
commit
8afa763ad2
@ -121,97 +121,98 @@ def intRect(rect1):
|
||||
yMax = int(math.ceil(yMax))
|
||||
return (xMin, yMin, xMax, yMax)
|
||||
|
||||
|
||||
class Vector(object):
|
||||
"""A math-like vector."""
|
||||
"""A math-like vector."""
|
||||
|
||||
def __init__(self, values, keep=False):
|
||||
self.values = values if keep else list(values)
|
||||
def __init__(self, values, keep=False):
|
||||
self.values = values if keep else list(values)
|
||||
|
||||
def __getitem__(self, index):
|
||||
return self.values[index]
|
||||
def __getitem__(self, index):
|
||||
return self.values[index]
|
||||
|
||||
def __len__(self):
|
||||
return len(self.values)
|
||||
def __len__(self):
|
||||
return len(self.values)
|
||||
|
||||
def __repr__(self):
|
||||
return "Vector(%s)" % self.values
|
||||
def __repr__(self):
|
||||
return "Vector(%s)" % self.values
|
||||
|
||||
def _vectorOp(self, other, op):
|
||||
if isinstance(other, Vector):
|
||||
assert len(self.values) == len(other.values)
|
||||
a = self.values
|
||||
b = other.values
|
||||
return [op(a[i], b[i]) for i in range(len(self.values))]
|
||||
if isinstance(other, Number):
|
||||
return [op(v, other) for v in self.values]
|
||||
raise NotImplementedError
|
||||
def _vectorOp(self, other, op):
|
||||
if isinstance(other, Vector):
|
||||
assert len(self.values) == len(other.values)
|
||||
a = self.values
|
||||
b = other.values
|
||||
return [op(a[i], b[i]) for i in range(len(self.values))]
|
||||
if isinstance(other, Number):
|
||||
return [op(v, other) for v in self.values]
|
||||
raise NotImplementedError
|
||||
|
||||
def _scalarOp(self, other, op):
|
||||
if isinstance(other, Number):
|
||||
return [op(v, other) for v in self.values]
|
||||
raise NotImplementedError
|
||||
def _scalarOp(self, other, op):
|
||||
if isinstance(other, Number):
|
||||
return [op(v, other) for v in self.values]
|
||||
raise NotImplementedError
|
||||
|
||||
def _unaryOp(self, op):
|
||||
if isinstance(other, Number):
|
||||
return [op(v) for v in self.values]
|
||||
raise NotImplementedError
|
||||
def _unaryOp(self, op):
|
||||
if isinstance(other, Number):
|
||||
return [op(v) for v in self.values]
|
||||
raise NotImplementedError
|
||||
|
||||
def __add__(self, other):
|
||||
return Vector(self._vectorOp(other, operator.add), keep=True)
|
||||
def __iadd__(self, other):
|
||||
self.values = self._vectorOp(other, operator.add)
|
||||
return self
|
||||
__radd__ = __add__
|
||||
def __add__(self, other):
|
||||
return Vector(self._vectorOp(other, operator.add), keep=True)
|
||||
def __iadd__(self, other):
|
||||
self.values = self._vectorOp(other, operator.add)
|
||||
return self
|
||||
__radd__ = __add__
|
||||
|
||||
def __sub__(self, other):
|
||||
return Vector(self._vectorOp(other, operator.sub), keep=True)
|
||||
def __isub__(self, other):
|
||||
self.values = self._vectorOp(other, operator.sub)
|
||||
return self
|
||||
def __rsub__(self, other):
|
||||
return other + (-self)
|
||||
def __sub__(self, other):
|
||||
return Vector(self._vectorOp(other, operator.sub), keep=True)
|
||||
def __isub__(self, other):
|
||||
self.values = self._vectorOp(other, operator.sub)
|
||||
return self
|
||||
def __rsub__(self, other):
|
||||
return other + (-self)
|
||||
|
||||
def __mul__(self, other):
|
||||
return Vector(self._scalarOp(other, operator.mul), keep=True)
|
||||
def __imul__(self, other):
|
||||
self.values = self._scalarOp(other, operator.mul)
|
||||
return self
|
||||
__rmul__ = __mul__
|
||||
def __mul__(self, other):
|
||||
return Vector(self._scalarOp(other, operator.mul), keep=True)
|
||||
def __imul__(self, other):
|
||||
self.values = self._scalarOp(other, operator.mul)
|
||||
return self
|
||||
__rmul__ = __mul__
|
||||
|
||||
def __truediv__(self, other):
|
||||
return Vector(self._scalarOp(other, operator.div), keep=True)
|
||||
def __itruediv__(self, other):
|
||||
self.values = self._scalarOp(other, operator.div)
|
||||
return self
|
||||
def __truediv__(self, other):
|
||||
return Vector(self._scalarOp(other, operator.div), keep=True)
|
||||
def __itruediv__(self, other):
|
||||
self.values = self._scalarOp(other, operator.div)
|
||||
return self
|
||||
|
||||
def __pos__(self):
|
||||
return Vector(self._unaryOp(other, operator.pos), keep=True)
|
||||
def __neg__(self):
|
||||
return Vector(self._unaryOp(other, operator.neg), keep=True)
|
||||
def __round__(self):
|
||||
return Vector(self._unaryOp(other, round), keep=True)
|
||||
def toInt(self):
|
||||
return self.__round__()
|
||||
def __pos__(self):
|
||||
return Vector(self._unaryOp(other, operator.pos), keep=True)
|
||||
def __neg__(self):
|
||||
return Vector(self._unaryOp(other, operator.neg), keep=True)
|
||||
def __round__(self):
|
||||
return Vector(self._unaryOp(other, round), keep=True)
|
||||
def toInt(self):
|
||||
return self.__round__()
|
||||
|
||||
def __eq__(self, other):
|
||||
if type(other) == Vector:
|
||||
return self.values == other.values
|
||||
else:
|
||||
return self.values == other
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
def __eq__(self, other):
|
||||
if type(other) == Vector:
|
||||
return self.values == other.values
|
||||
else:
|
||||
return self.values == other
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
def __bool__(self):
|
||||
return any(self.values)
|
||||
__nonzero__ = __bool__
|
||||
def __bool__(self):
|
||||
return any(self.values)
|
||||
__nonzero__ = __bool__
|
||||
|
||||
def __abs__(self):
|
||||
return math.sqrt(sum([x*x for x in self.values]))
|
||||
def dot(self, other):
|
||||
a = self.values
|
||||
b = other.values if type(other) == Vector else b
|
||||
assert len(a) == len(b)
|
||||
return sum([a[i] * b[i] for i in range(len(a))])
|
||||
def __abs__(self):
|
||||
return math.sqrt(sum([x*x for x in self.values]))
|
||||
def dot(self, other):
|
||||
a = self.values
|
||||
b = other.values if type(other) == Vector else b
|
||||
assert len(a) == len(b)
|
||||
return sum([a[i] * b[i] for i in range(len(a))])
|
||||
|
||||
|
||||
def pairwise(iterable, reverse=False):
|
||||
|
Loading…
x
Reference in New Issue
Block a user