forked from cliveverghese/proj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.py
55 lines (41 loc) · 1009 Bytes
/
Vector.py
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
from math import sqrt
class Vector:
data = []
def __init__(self,items):
self.data = items
def __repr__(self):
return repr(self.data)
def __add__(self,other):
temp = []
for j in range(len(self.data)):
temp.append(self.data[j] + other.data[j])
return Vector(temp)
def __sub__(self,other):
temp = []
for j in range(len(self.data)):
temp.append(self.data[j] - other.data[j])
return Vector(temp)
def __getitem__(self,a):
return self.data[a]
def __setitem__(self,a,b):
self.data[a] = b
def getList(self):
return self.data
def magnitude(self):
temp = 0
for j in range(len(self.data)):
temp += self.data[j] * self.data[j]
return sqrt(temp)
def dot(self,other):
temp = 0
for j in range(len(self.data)):
temp += self.data[j] * other.data[j]
return temp
def cosine(self,other):
temp = self.dot(other)
mul = self.magnitude() * other.magnitude()
if mul == 0:
return 0
return temp / mul
def remove(self,pos):
self.data[pos] = 0