Guido van Rossum | e876949 | 1992-08-13 12:14:11 +0000 | [diff] [blame] | 1 | # A simple vector class |
| 2 | |
| 3 | |
| 4 | def vec(*v): |
Raymond Hettinger | fe63faa | 2003-09-10 21:12:59 +0000 | [diff] [blame] | 5 | return Vec(*v) |
Guido van Rossum | e876949 | 1992-08-13 12:14:11 +0000 | [diff] [blame] | 6 | |
| 7 | |
| 8 | class Vec: |
| 9 | |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 10 | def __init__(self, *v): |
Raymond Hettinger | fe63faa | 2003-09-10 21:12:59 +0000 | [diff] [blame] | 11 | self.v = list(v) |
Guido van Rossum | e876949 | 1992-08-13 12:14:11 +0000 | [diff] [blame] | 12 | |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 13 | def fromlist(self, v): |
Raymond Hettinger | fe63faa | 2003-09-10 21:12:59 +0000 | [diff] [blame] | 14 | if not isinstance(v, list): |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 15 | raise TypeError |
| 16 | self.v = v[:] |
| 17 | return self |
Guido van Rossum | e876949 | 1992-08-13 12:14:11 +0000 | [diff] [blame] | 18 | |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 19 | def __repr__(self): |
Raymond Hettinger | fe63faa | 2003-09-10 21:12:59 +0000 | [diff] [blame] | 20 | return 'vec(' + repr(self.v)[1:-1] + ')' |
Guido van Rossum | e876949 | 1992-08-13 12:14:11 +0000 | [diff] [blame] | 21 | |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 22 | def __len__(self): |
| 23 | return len(self.v) |
Guido van Rossum | e876949 | 1992-08-13 12:14:11 +0000 | [diff] [blame] | 24 | |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 25 | def __getitem__(self, i): |
| 26 | return self.v[i] |
Guido van Rossum | e876949 | 1992-08-13 12:14:11 +0000 | [diff] [blame] | 27 | |
Raymond Hettinger | fe63faa | 2003-09-10 21:12:59 +0000 | [diff] [blame] | 28 | def __add__(self, other): |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 29 | # Element-wise addition |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 30 | v = list(map(lambda x, y: x+y, self, other)) |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 31 | return Vec().fromlist(v) |
Guido van Rossum | e876949 | 1992-08-13 12:14:11 +0000 | [diff] [blame] | 32 | |
Raymond Hettinger | fe63faa | 2003-09-10 21:12:59 +0000 | [diff] [blame] | 33 | def __sub__(self, other): |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 34 | # Element-wise subtraction |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 35 | v = list(map(lambda x, y: x-y, self, other)) |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 36 | return Vec().fromlist(v) |
Guido van Rossum | e876949 | 1992-08-13 12:14:11 +0000 | [diff] [blame] | 37 | |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 38 | def __mul__(self, scalar): |
| 39 | # Multiply by scalar |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 40 | v = [x*scalar for x in self.v] |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 41 | return Vec().fromlist(v) |
Guido van Rossum | e876949 | 1992-08-13 12:14:11 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | |
| 45 | def test(): |
Andrew M. Kuchling | 946c53e | 2003-04-24 17:13:18 +0000 | [diff] [blame] | 46 | a = vec(1, 2, 3) |
| 47 | b = vec(3, 2, 1) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 48 | print(a) |
| 49 | print(b) |
| 50 | print(a+b) |
| 51 | print(a-b) |
| 52 | print(a*3.0) |
Guido van Rossum | e876949 | 1992-08-13 12:14:11 +0000 | [diff] [blame] | 53 | |
| 54 | test() |