Skip to content

Commit

Permalink
compare-method added to Vector class in lib.py (#12448)
Browse files Browse the repository at this point in the history
* compare-method added to Vector class in lib.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Updated lib.py with suggestions

* Updated lib.py with suggestions

* Updated lib.py with __eq__ method

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Andrwaa and pre-commit-ci[bot] authored Dec 28, 2024
1 parent 1909f22 commit 2b58ab0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion linear_algebra/src/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class Vector:
change_component(pos: int, value: float): changes specified component
euclidean_length(): returns the euclidean length of the vector
angle(other: Vector, deg: bool): returns the angle between two vectors
TODO: compare-operator
"""

def __init__(self, components: Collection[float] | None = None) -> None:
Expand Down Expand Up @@ -96,6 +95,16 @@ def __sub__(self, other: Vector) -> Vector:
else: # error case
raise Exception("must have the same size")

def __eq__(self, other: object) -> bool:
"""
performs the comparison between two vectors
"""
if not isinstance(other, Vector):
return NotImplemented
if len(self) != len(other):
return False
return all(self.component(i) == other.component(i) for i in range(len(self)))

@overload
def __mul__(self, other: float) -> Vector: ...

Expand Down

0 comments on commit 2b58ab0

Please sign in to comment.