forked from EricSchles/neuralnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
48 lines (37 loc) · 1.11 KB
/
vector.py
File metadata and controls
48 lines (37 loc) · 1.11 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
import math
class Vector:
def __init__(self,vect):
self.vect = vect
def add(self,other):
for ind,val in enumerate(other.vect):
self.vect[ind] += val
def append(self,elem):
self.vect.append(elem)
def magnitudeSq(self):
summa = 0
for elem in self.vect:
summa += (elem**2)
return summa
def magnitude(self):
summa = 0
for elem in self.vect:
summa += (elem**2)
return math.sqrt(summa)
def div(self,n):
for ind,val in enumerate(self.vect):
self.vect[ind] /= n
def limit(self,lim):
if self.magnitudeSq() > lim*lim:
self.normalize()
self.mult(lim)
def mult(self,n):
for ind,val in enumerate(self.vect):
self.vect[ind] *= n
def sub(self,other):
if len(other.vect) == len(self.vect):
for ind,val in enumerate(self.vect):
self.vect[ind] -= other.vect[ind]
def normalize(self):
mag = self.magnitude()
if mag != 0 and mag != 0:
self.div(mag)