(norganizer) web status filter
[experiments.git] / evolution / utils.py
1
2 import math
3
4
5 class Vector:
6 def __init__(self, *data):
7 self.data = list(data)
8
9 def __repr__(self):
10 return repr(self.data)
11
12 def __add__(self, other):
13 return Vector(*map(lambda x, y: x+y, self, other))
14
15 def __sub__(self, other):
16 return Vector(*map(lambda x, y: x-y, self, other))
17
18 def __mul__(self, other):
19 return Vector(*map(lambda x: x*other, self))
20
21 def __div__(self, other):
22 return Vector(*map(lambda x: x/other, self))
23
24 def __getitem__(self, index):
25 return self.data[index]
26
27 def __len__(self):
28 return len(self.data)
29
30 def magnitude(self):
31 return math.sqrt(sum(map(lambda x:x**2, self.data)))
32
33 def normalize(self):
34 magnitude = self.magnitude()
35 if magnitude:
36 self.data = map(lambda x: x/magnitude, self)
37 return self
38
39 def truncate(self, upper):
40 def _trunc(value):
41 if value > 0:
42 return min(value, upper)
43 else:
44 return max(value, -1*upper)
45 self.data = map(_trunc, self)
46 return self
47
48