[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))
|
yMax = int(math.ceil(yMax))
|
||||||
return (xMin, yMin, xMax, yMax)
|
return (xMin, yMin, xMax, yMax)
|
||||||
|
|
||||||
|
|
||||||
class Vector(object):
|
class Vector(object):
|
||||||
"""A math-like vector."""
|
"""A math-like vector."""
|
||||||
|
|
||||||
def __init__(self, values, keep=False):
|
def __init__(self, values, keep=False):
|
||||||
self.values = values if keep else list(values)
|
self.values = values if keep else list(values)
|
||||||
|
|
||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
return self.values[index]
|
return self.values[index]
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.values)
|
return len(self.values)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "Vector(%s)" % self.values
|
return "Vector(%s)" % self.values
|
||||||
|
|
||||||
def _vectorOp(self, other, op):
|
def _vectorOp(self, other, op):
|
||||||
if isinstance(other, Vector):
|
if isinstance(other, Vector):
|
||||||
assert len(self.values) == len(other.values)
|
assert len(self.values) == len(other.values)
|
||||||
a = self.values
|
a = self.values
|
||||||
b = other.values
|
b = other.values
|
||||||
return [op(a[i], b[i]) for i in range(len(self.values))]
|
return [op(a[i], b[i]) for i in range(len(self.values))]
|
||||||
if isinstance(other, Number):
|
if isinstance(other, Number):
|
||||||
return [op(v, other) for v in self.values]
|
return [op(v, other) for v in self.values]
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def _scalarOp(self, other, op):
|
def _scalarOp(self, other, op):
|
||||||
if isinstance(other, Number):
|
if isinstance(other, Number):
|
||||||
return [op(v, other) for v in self.values]
|
return [op(v, other) for v in self.values]
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def _unaryOp(self, op):
|
def _unaryOp(self, op):
|
||||||
if isinstance(other, Number):
|
if isinstance(other, Number):
|
||||||
return [op(v) for v in self.values]
|
return [op(v) for v in self.values]
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
return Vector(self._vectorOp(other, operator.add), keep=True)
|
return Vector(self._vectorOp(other, operator.add), keep=True)
|
||||||
def __iadd__(self, other):
|
def __iadd__(self, other):
|
||||||
self.values = self._vectorOp(other, operator.add)
|
self.values = self._vectorOp(other, operator.add)
|
||||||
return self
|
return self
|
||||||
__radd__ = __add__
|
__radd__ = __add__
|
||||||
|
|
||||||
def __sub__(self, other):
|
def __sub__(self, other):
|
||||||
return Vector(self._vectorOp(other, operator.sub), keep=True)
|
return Vector(self._vectorOp(other, operator.sub), keep=True)
|
||||||
def __isub__(self, other):
|
def __isub__(self, other):
|
||||||
self.values = self._vectorOp(other, operator.sub)
|
self.values = self._vectorOp(other, operator.sub)
|
||||||
return self
|
return self
|
||||||
def __rsub__(self, other):
|
def __rsub__(self, other):
|
||||||
return other + (-self)
|
return other + (-self)
|
||||||
|
|
||||||
def __mul__(self, other):
|
def __mul__(self, other):
|
||||||
return Vector(self._scalarOp(other, operator.mul), keep=True)
|
return Vector(self._scalarOp(other, operator.mul), keep=True)
|
||||||
def __imul__(self, other):
|
def __imul__(self, other):
|
||||||
self.values = self._scalarOp(other, operator.mul)
|
self.values = self._scalarOp(other, operator.mul)
|
||||||
return self
|
return self
|
||||||
__rmul__ = __mul__
|
__rmul__ = __mul__
|
||||||
|
|
||||||
def __truediv__(self, other):
|
def __truediv__(self, other):
|
||||||
return Vector(self._scalarOp(other, operator.div), keep=True)
|
return Vector(self._scalarOp(other, operator.div), keep=True)
|
||||||
def __itruediv__(self, other):
|
def __itruediv__(self, other):
|
||||||
self.values = self._scalarOp(other, operator.div)
|
self.values = self._scalarOp(other, operator.div)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __pos__(self):
|
def __pos__(self):
|
||||||
return Vector(self._unaryOp(other, operator.pos), keep=True)
|
return Vector(self._unaryOp(other, operator.pos), keep=True)
|
||||||
def __neg__(self):
|
def __neg__(self):
|
||||||
return Vector(self._unaryOp(other, operator.neg), keep=True)
|
return Vector(self._unaryOp(other, operator.neg), keep=True)
|
||||||
def __round__(self):
|
def __round__(self):
|
||||||
return Vector(self._unaryOp(other, round), keep=True)
|
return Vector(self._unaryOp(other, round), keep=True)
|
||||||
def toInt(self):
|
def toInt(self):
|
||||||
return self.__round__()
|
return self.__round__()
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if type(other) == Vector:
|
if type(other) == Vector:
|
||||||
return self.values == other.values
|
return self.values == other.values
|
||||||
else:
|
else:
|
||||||
return self.values == other
|
return self.values == other
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
return not self.__eq__(other)
|
return not self.__eq__(other)
|
||||||
|
|
||||||
def __bool__(self):
|
def __bool__(self):
|
||||||
return any(self.values)
|
return any(self.values)
|
||||||
__nonzero__ = __bool__
|
__nonzero__ = __bool__
|
||||||
|
|
||||||
def __abs__(self):
|
def __abs__(self):
|
||||||
return math.sqrt(sum([x*x for x in self.values]))
|
return math.sqrt(sum([x*x for x in self.values]))
|
||||||
def dot(self, other):
|
def dot(self, other):
|
||||||
a = self.values
|
a = self.values
|
||||||
b = other.values if type(other) == Vector else b
|
b = other.values if type(other) == Vector else b
|
||||||
assert len(a) == len(b)
|
assert len(a) == len(b)
|
||||||
return sum([a[i] * b[i] for i in range(len(a))])
|
return sum([a[i] * b[i] for i in range(len(a))])
|
||||||
|
|
||||||
|
|
||||||
def pairwise(iterable, reverse=False):
|
def pairwise(iterable, reverse=False):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user