Skip to content

Deduplicate euclidean_length method in Vector #5658

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 15 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from 13 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
32 changes: 16 additions & 16 deletions linear_algebra/src/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,20 @@ def change_component(self, pos: int, value: float) -> None:
def euclidean_length(self) -> float:
"""
returns the euclidean length of the vector
"""
squares = [c ** 2 for c in self.__components]
return math.sqrt(sum(squares))

def magnitude(self) -> float:
"""
Magnitude of a Vector

>>> Vector([2, 3, 4]).magnitude()
>>> Vector([2, 3, 4]).euclidean_length()
5.385164807134504

>>> Vector([1]).euclidean_length()
1.0
>>> Vector([0, -1, -2, -3, 4, 5, 6]).euclidean_length()
9.539392014169456
>>> Vector([]).euclidean_length()
Traceback (most recent call last):
...
Exception: Vector is empty
"""
if len(self.__components) == 0:
raise Exception("Vector is empty")
squares = [c ** 2 for c in self.__components]
return math.sqrt(sum(squares))

Expand All @@ -188,7 +190,7 @@ def angle(self, other: Vector, deg: bool = False) -> float:
Exception: invalid operand!
"""
num = self * other
den = self.magnitude() * other.magnitude()
den = self.euclidean_length() * other.euclidean_length()
if deg:
return math.degrees(math.acos(num / den))
else:
Expand Down Expand Up @@ -267,17 +269,15 @@ class Matrix:

def __init__(self, matrix: list[list[float]], w: int, h: int) -> None:
"""
simple constructor for initializing
the matrix with components.
simple constructor for initializing the matrix with components.
"""
self.__matrix = matrix
self.__width = w
self.__height = h

def __str__(self) -> str:
"""
returns a string representation of this
matrix.
returns a string representation of this matrix.
"""
ans = ""
for i in range(self.__height):
Expand All @@ -291,7 +291,7 @@ def __str__(self) -> str:

def __add__(self, other: Matrix) -> Matrix:
"""
implements the matrix-addition.
implements matrix addition.
"""
if self.__width == other.width() and self.__height == other.height():
matrix = []
Expand All @@ -307,7 +307,7 @@ def __add__(self, other: Matrix) -> Matrix:

def __sub__(self, other: Matrix) -> Matrix:
"""
implements the matrix-subtraction.
implements matrix subtraction.
"""
if self.__width == other.width() and self.__height == other.height():
matrix = []
Expand Down
8 changes: 7 additions & 1 deletion linear_algebra/src/test_linear_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ def test_size(self) -> None:
x = Vector([1, 2, 3, 4])
self.assertEqual(len(x), 4)

def test_euclidLength(self) -> None:
def test_euclidean_length(self) -> None:
"""
test for method euclidean_length()
"""
x = Vector([1, 2])
y = Vector([1, 2, 3, 4, 5])
z = Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
w = Vector([1, -1, 1, -1, 2, -3, 4, -5])
self.assertAlmostEqual(x.euclidean_length(), 2.236, 3)
self.assertAlmostEqual(y.euclidean_length(), 7.416, 3)
self.assertEqual(z.euclidean_length(), 0)
self.assertAlmostEqual(w.euclidean_length(), 7.616, 3)

def test_add(self) -> None:
"""
Expand Down