Skip to content

Commit 5c5fcf2

Browse files
authored
compare-method added to Vector class in lib.py
1 parent f8e595e commit 5c5fcf2

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

linear_algebra/src/lib.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class Vector:
4646
change_component(pos: int, value: float): changes specified component
4747
euclidean_length(): returns the euclidean length of the vector
4848
angle(other: Vector, deg: bool): returns the angle between two vectors
49-
TODO: compare-operator
5049
"""
5150

5251
def __init__(self, components: Collection[float] | None = None) -> None:
@@ -182,6 +181,15 @@ def angle(self, other: Vector, deg: bool = False) -> float:
182181
return math.degrees(math.acos(num / den))
183182
else:
184183
return math.acos(num / den)
184+
def __eq__(self, vector: object) -> bool:
185+
"""
186+
performs the comparison between two vectors
187+
"""
188+
if not isinstance(vector, Vector):
189+
return NotImplemented
190+
if len(self) != len(vector):
191+
return False
192+
return all(self.component(i) == vector.component(i) for i in range(len(self)))
185193

186194

187195
def zero_vector(dimension: int) -> Vector:
@@ -433,3 +441,4 @@ def random_matrix(width: int, height: int, a: int, b: int) -> Matrix:
433441
[random.randint(a, b) for _ in range(width)] for _ in range(height)
434442
]
435443
return Matrix(matrix, width, height)
444+

0 commit comments

Comments
 (0)