-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector_class.py
More file actions
96 lines (70 loc) · 2.4 KB
/
Vector_class.py
File metadata and controls
96 lines (70 loc) · 2.4 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from math import sqrt
class VectorIncompatibleException(Exception):
pass
def vectors_compatible(func):
def inner(*args, **kwargs):
vector_dimensions = len(args[0].vectors)
for arg in args:
if vector_dimensions != len(arg.vectors):
raise VectorIncompatibleException()
return func(*args, **kwargs)
return inner
class Vector(object):
def __init__(self, vectors):
self.vectors = vectors
def vector_operation(self,b, fn):
return Vector([fn(i[0] , i[1]) for i in zip(self.vectors, b.vectors)])
@vectors_compatible
def add(self,b):
return self.vector_operation(b,lambda x, y : x + y)
@vectors_compatible
def subtract(self,b):
return self.vector_operation(b,lambda x, y : x - y)
def toString(self):
return self.__str__()
@vectors_compatible
def dot(self,b):
multipled = self.vector_operation(b,lambda x, y : x * y)
return sum(multipled.vectors)
def __str__(self):
return str(tuple(self.vectors)).replace(' ','')
def __eq__(self, b):
return self.vectors == b.vectors
def equals(self,b):
return self.__eq__(b)
def norm(self):
squared = self.vector_operation(self,lambda x, y : x * y)
r = sum(squared.vectors)
return sqrt(r)
import unittest
class TestFirst(unittest.TestCase):
def test_first(self):
test = self
test.assert_equals = self.assertEqual
test.expect = self.assertEqual
Test = self
Test.assert_equals = self.assertEqual
a = Vector([1,2])
b = Vector([3,4])
test.expect(a.add(b), Vector([4,6]))
test.expect(a.toString() , '(1,2)')
c = Vector([3,4])
self.assertEqual (b.toString(), c.toString())
def test_second(self):
test = self
test.assert_equals = self.assertEqual
test.expect = self.assertEqual
Test = self
Test.assert_equals = self.assertEqual
a = Vector([1,2,3])
b = Vector([3,4,5])
c = Vector([5,6,7,8])
test.expect(a.add(b),Vector([4,6,8]))
test.expect(a.subtract(b), Vector([-2,-2,-2]))
# 1*3+2*4+3*5 = 26
test.expect(a.dot(b), 26)
from math import sqrt
test.expect(a.norm() , sqrt(14))
# sqrt(1^2+2^2+3^2)
with self.assertRaises(Exception):
a.add(c)