-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector_class.py
More file actions
27 lines (21 loc) · 1.02 KB
/
Copy pathVector_class.py
File metadata and controls
27 lines (21 loc) · 1.02 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
class Vector:
def __init__(self, list1):
self.coordinates = list(list1)
def __str__(self):
return str(tuple(self.coordinates)).replace(' ', '')
def add(self, other):
if len(self.coordinates) != len(other.coordinates):
raise ValueError("Vectors must be the same length")
return Vector([x + y for x, y in zip(self.coordinates, other.coordinates)])
def subtract(self, other):
if len(self.coordinates) != len(other.coordinates):
raise ValueError("Vectors must be the same length")
return Vector([x - y for x, y in zip(self.coordinates, other.coordinates)])
def dot(self, other):
if len(self.coordinates) != len(other.coordinates):
raise ValueError("Vectors must be the same length")
return sum(x * y for x, y in zip(self.coordinates, other.coordinates))
def norm(self):
return sum(el ** 2 for el in self.coordinates) ** 0.5
def equals(self, other):
return self.coordinates == other.coordinates