Skip to content

Commit a64c9f1

Browse files
tianyizheng02poyea
andauthored
Deduplicate euclidean_length method in Vector (#5658)
* Rewrite parts of Vector and Matrix methods * Refactor determinant method and add unit tests Refactor determinant method to create separate minor and cofactor methods. Add respective unit tests for new methods. Rename methods using snake case to follow Python naming conventions. * Reorganize Vector and Matrix methods * Update linear_algebra/README.md Co-authored-by: John Law <[email protected]> * Fix punctuation and wording * Apply suggestions from code review Co-authored-by: John Law <[email protected]> * Deduplicate euclidean length method for Vector * Add more unit tests for Euclidean length method * Fix bug in unit test for euclidean_length * Remove old comments for magnitude method Co-authored-by: John Law <[email protected]>
1 parent 508589e commit a64c9f1

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

linear_algebra/src/lib.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class Vector:
4444
component(i): gets the i-th component (0-indexed)
4545
change_component(pos: int, value: float): changes specified component
4646
euclidean_length(): returns the euclidean length of the vector
47-
magnitude(): returns the magnitude of the vector
4847
angle(other: Vector, deg: bool): returns the angle between two vectors
4948
TODO: compare-operator
5049
"""
@@ -159,18 +158,20 @@ def change_component(self, pos: int, value: float) -> None:
159158
def euclidean_length(self) -> float:
160159
"""
161160
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
169161
170-
>>> Vector([2, 3, 4]).magnitude()
162+
>>> Vector([2, 3, 4]).euclidean_length()
171163
5.385164807134504
172-
164+
>>> Vector([1]).euclidean_length()
165+
1.0
166+
>>> Vector([0, -1, -2, -3, 4, 5, 6]).euclidean_length()
167+
9.539392014169456
168+
>>> Vector([]).euclidean_length()
169+
Traceback (most recent call last):
170+
...
171+
Exception: Vector is empty
173172
"""
173+
if len(self.__components) == 0:
174+
raise Exception("Vector is empty")
174175
squares = [c ** 2 for c in self.__components]
175176
return math.sqrt(sum(squares))
176177

@@ -188,7 +189,7 @@ def angle(self, other: Vector, deg: bool = False) -> float:
188189
Exception: invalid operand!
189190
"""
190191
num = self * other
191-
den = self.magnitude() * other.magnitude()
192+
den = self.euclidean_length() * other.euclidean_length()
192193
if deg:
193194
return math.degrees(math.acos(num / den))
194195
else:
@@ -267,17 +268,15 @@ class Matrix:
267268

268269
def __init__(self, matrix: list[list[float]], w: int, h: int) -> None:
269270
"""
270-
simple constructor for initializing
271-
the matrix with components.
271+
simple constructor for initializing the matrix with components.
272272
"""
273273
self.__matrix = matrix
274274
self.__width = w
275275
self.__height = h
276276

277277
def __str__(self) -> str:
278278
"""
279-
returns a string representation of this
280-
matrix.
279+
returns a string representation of this matrix.
281280
"""
282281
ans = ""
283282
for i in range(self.__height):
@@ -291,7 +290,7 @@ def __str__(self) -> str:
291290

292291
def __add__(self, other: Matrix) -> Matrix:
293292
"""
294-
implements the matrix-addition.
293+
implements matrix addition.
295294
"""
296295
if self.__width == other.width() and self.__height == other.height():
297296
matrix = []
@@ -307,7 +306,7 @@ def __add__(self, other: Matrix) -> Matrix:
307306

308307
def __sub__(self, other: Matrix) -> Matrix:
309308
"""
310-
implements the matrix-subtraction.
309+
implements matrix subtraction.
311310
"""
312311
if self.__width == other.width() and self.__height == other.height():
313312
matrix = []

linear_algebra/src/test_linear_algebra.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@ 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])
51+
z = Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
52+
w = Vector([1, -1, 1, -1, 2, -3, 4, -5])
5053
self.assertAlmostEqual(x.euclidean_length(), 2.236, 3)
54+
self.assertAlmostEqual(y.euclidean_length(), 7.416, 3)
55+
self.assertEqual(z.euclidean_length(), 0)
56+
self.assertAlmostEqual(w.euclidean_length(), 7.616, 3)
5157

5258
def test_add(self) -> None:
5359
"""

0 commit comments

Comments
 (0)