-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.py
More file actions
47 lines (41 loc) · 1.06 KB
/
vector.py
File metadata and controls
47 lines (41 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
Vector type with +, * redefined as vector addition and dot product
From Jon Jacky's Intro to Python course:
http://staff.washington.edu/jon/python-course/
"""
class vector(list):
def __repr__(self):
"""
String representation, uses list (superclass) representation
"""
return 'vector(%s)' % super(vector, self).__repr__()
def __add__(self, v):
"""
redefine + as element-wise vector sum
"""
assert len(self) == len(v)
return vector([x1 + x2 for x1, x2 in zip(self, v)])
def __mul__(self, v):
"""
redefine * as vector dot product
"""
assert len(self) == len(v)
return sum([x1 * x2 for x1, x2 in zip(self, v)])
l1 = [1, 2, 3]
l2 = [4, 5, 6]
v1 = vector(l1)
v2 = vector(l2)
if __name__ == '__main__':
print 'l1'
print l1
print 'l1 + l2'
print l1 + l2
# print l1 * l2 # TypeError
print 'zip(l1, l2)'
print zip(l1, l2)
print 'v1'
print v1
print 'v1 + v2'
print v1 + v2
print 'v1 * v2'
print v1 * v2