blob: 787af6962c1ec3c2a74c3ae0d6b0ee144ebf9455 [file] [log] [blame]
Guido van Rossume8769491992-08-13 12:14:11 +00001class Vec:
Alexander Belopolsky31c27402010-07-05 21:44:05 +00002 """ A simple vector class
Guido van Rossume8769491992-08-13 12:14:11 +00003
Alexander Belopolsky31c27402010-07-05 21:44:05 +00004 Instances of the Vec class can be constructed from numbers
5
6 >>> a = Vec(1, 2, 3)
7 >>> b = Vec(3, 2, 1)
8
9 added
10 >>> a + b
11 Vec(4, 4, 4)
12
13 subtracted
14 >>> a - b
15 Vec(-2, 0, 2)
16
17 and multiplied by a scalar on the left
18 >>> 3.0 * a
19 Vec(3.0, 6.0, 9.0)
20
21 or on the right
22 >>> a * 3.0
23 Vec(3.0, 6.0, 9.0)
24 """
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000025 def __init__(self, *v):
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000026 self.v = list(v)
Guido van Rossume8769491992-08-13 12:14:11 +000027
Georg Brandl5ada7c72010-07-05 20:13:41 +000028 @classmethod
29 def fromlist(cls, v):
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000030 if not isinstance(v, list):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000031 raise TypeError
Georg Brandl5ada7c72010-07-05 20:13:41 +000032 inst = cls()
33 inst.v = v
34 return inst
Guido van Rossume8769491992-08-13 12:14:11 +000035
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000036 def __repr__(self):
Alexander Belopolsky31c27402010-07-05 21:44:05 +000037 args = ', '.join(repr(x) for x in self.v)
38 return 'Vec({})'.format(args)
Guido van Rossume8769491992-08-13 12:14:11 +000039
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000040 def __len__(self):
41 return len(self.v)
Guido van Rossume8769491992-08-13 12:14:11 +000042
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000043 def __getitem__(self, i):
44 return self.v[i]
Guido van Rossume8769491992-08-13 12:14:11 +000045
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000046 def __add__(self, other):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000047 # Element-wise addition
Alexander Belopolsky31c27402010-07-05 21:44:05 +000048 v = [x + y for x, y in zip(self.v, other.v)]
Georg Brandl5ada7c72010-07-05 20:13:41 +000049 return Vec.fromlist(v)
Guido van Rossume8769491992-08-13 12:14:11 +000050
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000051 def __sub__(self, other):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000052 # Element-wise subtraction
Alexander Belopolsky31c27402010-07-05 21:44:05 +000053 v = [x - y for x, y in zip(self.v, other.v)]
Georg Brandl5ada7c72010-07-05 20:13:41 +000054 return Vec.fromlist(v)
Guido van Rossume8769491992-08-13 12:14:11 +000055
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000056 def __mul__(self, scalar):
57 # Multiply by scalar
Alexander Belopolsky31c27402010-07-05 21:44:05 +000058 v = [x * scalar for x in self.v]
Georg Brandl5ada7c72010-07-05 20:13:41 +000059 return Vec.fromlist(v)
Guido van Rossume8769491992-08-13 12:14:11 +000060
Alexander Belopolsky31c27402010-07-05 21:44:05 +000061 __rmul__ = __mul__
Guido van Rossume8769491992-08-13 12:14:11 +000062
63
64def test():
Alexander Belopolsky31c27402010-07-05 21:44:05 +000065 import doctest
66 doctest.testmod()
Guido van Rossume8769491992-08-13 12:14:11 +000067
68test()