Skip to content

Commit 36dd293

Browse files
committed
Deduplicate euclidean length method for Vector
1 parent 768bda8 commit 36dd293

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

linear_algebra/src/lib.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,18 @@ def change_component(self, pos: int, value: float) -> None:
159159
def euclidean_length(self) -> float:
160160
"""
161161
returns the euclidean length of the vector
162-
"""
163-
squares = [c ** 2 for c in self.__components]
164-
return math.sqrt(sum(squares))
165-
166-
def magnitude(self) -> float:
167-
"""
168-
Magnitude of a Vector
169162
170-
>>> Vector([2, 3, 4]).magnitude()
163+
>>> Vector([2, 3, 4]).euclidean_length()
171164
5.385164807134504
172-
165+
>>> Vector([1]).euclidean_length()
166+
1
167+
>>> Vector([]).euclidean_length()
168+
Traceback (most recent call last):
169+
...
170+
Exception: Vector is empty
173171
"""
172+
if len(self.__components) == 0:
173+
raise Exception("Vector is empty")
174174
squares = [c ** 2 for c in self.__components]
175175
return math.sqrt(sum(squares))
176176

@@ -188,7 +188,7 @@ def angle(self, other: Vector, deg: bool = False) -> float:
188188
Exception: invalid operand!
189189
"""
190190
num = self * other
191-
den = self.magnitude() * other.magnitude()
191+
den = self.euclidean_length() * other.euclidean_length()
192192
if deg:
193193
return math.degrees(math.acos(num / den))
194194
else:
@@ -267,17 +267,15 @@ class Matrix:
267267

268268
def __init__(self, matrix: list[list[float]], w: int, h: int) -> None:
269269
"""
270-
simple constructor for initializing
271-
the matrix with components.
270+
simple constructor for initializing the matrix with components.
272271
"""
273272
self.__matrix = matrix
274273
self.__width = w
275274
self.__height = h
276275

277276
def __str__(self) -> str:
278277
"""
279-
returns a string representation of this
280-
matrix.
278+
returns a string representation of this matrix.
281279
"""
282280
ans = ""
283281
for i in range(self.__height):
@@ -291,7 +289,7 @@ def __str__(self) -> str:
291289

292290
def __add__(self, other: Matrix) -> Matrix:
293291
"""
294-
implements the matrix-addition.
292+
implements matrix addition.
295293
"""
296294
if self.__width == other.width() and self.__height == other.height():
297295
matrix = []
@@ -307,7 +305,7 @@ def __add__(self, other: Matrix) -> Matrix:
307305

308306
def __sub__(self, other: Matrix) -> Matrix:
309307
"""
310-
implements the matrix-subtraction.
308+
implements matrix subtraction.
311309
"""
312310
if self.__width == other.width() and self.__height == other.height():
313311
matrix = []

linear_algebra/src/test_linear_algebra.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ def test_size(self) -> None:
4242
x = Vector([1, 2, 3, 4])
4343
self.assertEqual(len(x), 4)
4444

45-
def test_euclidLength(self) -> None:
45+
def test_euclidean_length(self) -> None:
4646
"""
4747
test for method euclidean_length()
4848
"""
4949
x = Vector([1, 2])
50+
y = Vector([1, 2, 3, 4, 5])
5051
self.assertAlmostEqual(x.euclidean_length(), 2.236, 3)
52+
self.assertAlmostEqual(y.euclidean_length(), 7.416, 3)
5153

5254
def test_add(self) -> None:
5355
"""

0 commit comments

Comments
 (0)