Skip to content

compare-method added to Vector class in lib.py #12448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading