-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector's_operations.py
34 lines (28 loc) · 1.1 KB
/
Vector's_operations.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
class Vector:
def __init__(self, args=[]):
self.args = args
def add(self, others):
if len(self.args) != len(others.args):
raise Exception('You can only add vectors of same dimension.')
result = []
for i in range(len(self.args)):
result.append(self.args[i] + others.args[i])
return result
def subtract(self, others):
if len(self.args) != len(others.args):
raise Exception('You can only subtract vectors of same dimension.')
result = []
for i in range(len(self.args)):
result.append(self.args[i] - others.args[i])
return result
def dot(self, others):
if len(self.args) != len(others.args):
raise Exception('You can only dot vectors of same dimension.')
result = []
for i in range(len(self.args)):
result.append(self.args[i] * others.args[i])
return result
def norm(self):
return (sum(self.args[i]**2 for i in range(len(self.args))))**0.5
def toString(self):
return '{}'.format(self.args)