mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
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 <johnlaw.po@gmail.com> * Fix punctuation and wording * Apply suggestions from code review Co-authored-by: John Law <johnlaw.po@gmail.com> * 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 <johnlaw.po@gmail.com>
This commit is contained in:
parent
508589e3fc
commit
a64c9f1e7c
@ -44,7 +44,6 @@ class Vector:
|
|||||||
component(i): gets the i-th component (0-indexed)
|
component(i): gets the i-th component (0-indexed)
|
||||||
change_component(pos: int, value: float): changes specified component
|
change_component(pos: int, value: float): changes specified component
|
||||||
euclidean_length(): returns the euclidean length of the vector
|
euclidean_length(): returns the euclidean length of the vector
|
||||||
magnitude(): returns the magnitude of the vector
|
|
||||||
angle(other: Vector, deg: bool): returns the angle between two vectors
|
angle(other: Vector, deg: bool): returns the angle between two vectors
|
||||||
TODO: compare-operator
|
TODO: compare-operator
|
||||||
"""
|
"""
|
||||||
@ -159,18 +158,20 @@ class Vector:
|
|||||||
def euclidean_length(self) -> float:
|
def euclidean_length(self) -> float:
|
||||||
"""
|
"""
|
||||||
returns the euclidean length of the vector
|
returns the euclidean length of the vector
|
||||||
"""
|
|
||||||
squares = [c ** 2 for c in self.__components]
|
|
||||||
return math.sqrt(sum(squares))
|
|
||||||
|
|
||||||
def magnitude(self) -> float:
|
>>> Vector([2, 3, 4]).euclidean_length()
|
||||||
"""
|
|
||||||
Magnitude of a Vector
|
|
||||||
|
|
||||||
>>> Vector([2, 3, 4]).magnitude()
|
|
||||||
5.385164807134504
|
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]
|
squares = [c ** 2 for c in self.__components]
|
||||||
return math.sqrt(sum(squares))
|
return math.sqrt(sum(squares))
|
||||||
|
|
||||||
@ -188,7 +189,7 @@ class Vector:
|
|||||||
Exception: invalid operand!
|
Exception: invalid operand!
|
||||||
"""
|
"""
|
||||||
num = self * other
|
num = self * other
|
||||||
den = self.magnitude() * other.magnitude()
|
den = self.euclidean_length() * other.euclidean_length()
|
||||||
if deg:
|
if deg:
|
||||||
return math.degrees(math.acos(num / den))
|
return math.degrees(math.acos(num / den))
|
||||||
else:
|
else:
|
||||||
@ -267,8 +268,7 @@ class Matrix:
|
|||||||
|
|
||||||
def __init__(self, matrix: list[list[float]], w: int, h: int) -> None:
|
def __init__(self, matrix: list[list[float]], w: int, h: int) -> None:
|
||||||
"""
|
"""
|
||||||
simple constructor for initializing
|
simple constructor for initializing the matrix with components.
|
||||||
the matrix with components.
|
|
||||||
"""
|
"""
|
||||||
self.__matrix = matrix
|
self.__matrix = matrix
|
||||||
self.__width = w
|
self.__width = w
|
||||||
@ -276,8 +276,7 @@ class Matrix:
|
|||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
"""
|
"""
|
||||||
returns a string representation of this
|
returns a string representation of this matrix.
|
||||||
matrix.
|
|
||||||
"""
|
"""
|
||||||
ans = ""
|
ans = ""
|
||||||
for i in range(self.__height):
|
for i in range(self.__height):
|
||||||
@ -291,7 +290,7 @@ class Matrix:
|
|||||||
|
|
||||||
def __add__(self, other: Matrix) -> Matrix:
|
def __add__(self, other: Matrix) -> Matrix:
|
||||||
"""
|
"""
|
||||||
implements the matrix-addition.
|
implements matrix addition.
|
||||||
"""
|
"""
|
||||||
if self.__width == other.width() and self.__height == other.height():
|
if self.__width == other.width() and self.__height == other.height():
|
||||||
matrix = []
|
matrix = []
|
||||||
@ -307,7 +306,7 @@ class Matrix:
|
|||||||
|
|
||||||
def __sub__(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():
|
if self.__width == other.width() and self.__height == other.height():
|
||||||
matrix = []
|
matrix = []
|
||||||
|
@ -42,12 +42,18 @@ class Test(unittest.TestCase):
|
|||||||
x = Vector([1, 2, 3, 4])
|
x = Vector([1, 2, 3, 4])
|
||||||
self.assertEqual(len(x), 4)
|
self.assertEqual(len(x), 4)
|
||||||
|
|
||||||
def test_euclidLength(self) -> None:
|
def test_euclidean_length(self) -> None:
|
||||||
"""
|
"""
|
||||||
test for method euclidean_length()
|
test for method euclidean_length()
|
||||||
"""
|
"""
|
||||||
x = Vector([1, 2])
|
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(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:
|
def test_add(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user