From 8671d2fc8aaabf2e4cc03c490964a3896d88556f Mon Sep 17 00:00:00 2001 From: Codecalec Date: Thu, 24 Oct 2019 09:22:09 +0200 Subject: [PATCH 1/5] Added determinate function --- linear_algebra/README.md | 3 ++- linear_algebra/src/lib.py | 27 +++++++++++++++++++++++++++ linear_algebra/src/tests.py | 7 +++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/linear_algebra/README.md b/linear_algebra/README.md index f1b554e139de..72f46829cbac 100644 --- a/linear_algebra/README.md +++ b/linear_algebra/README.md @@ -45,7 +45,8 @@ This module contains some useful classes and functions for dealing with linear a - changeComponent(x,y,value) : changes the specified component. - component(x,y) : returns the specified component. - width() : returns the width of the matrix - - height() : returns the height of the matrix + - height() : returns the height of the matrix + - det() : returns the determinate of the matrix if it is square - operator + : implements the matrix-addition. - operator - _ implements the matrix-subtraction diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index bf9e0d302a89..1ef107666d34 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -277,6 +277,33 @@ def height(self): """ return self.__height + def det(self): + """ + returns the determinate of an nxn matrix using Laplace expansion + """ + if self.__height == self.__width and self.__width >= 2: + total = 0 + if self.__width > 2: + for x in range(0, self.__width): + for y in range(0, self.__height): + total += ( + self.__matrix[x][y] + * (-1) ** (x + y) + * Matrix( + self.__matrix[0:x] + self.__matrix[x + 1 :], + self.__width - 1, + self.__height - 1, + ).det() + ) + else: + return ( + self.__matrix[0][0] * self.__matrix[1][1] + - self.__matrix[0][1] * self.__matrix[1][0] + ) + return total + else: + raise Exception("matrix is not square") + def __mul__(self, other): """ implements the matrix-vector multiplication. diff --git a/linear_algebra/src/tests.py b/linear_algebra/src/tests.py index b63f2ae8c2db..97b514fa9f7a 100644 --- a/linear_algebra/src/tests.py +++ b/linear_algebra/src/tests.py @@ -118,6 +118,13 @@ def test_str_matrix(self): A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n", str(A)) + def test_det(self): + """ + test for det() + """ + A = Matrix([[1, 1, 4, 5], [3, 3, 3, 2], [5, 1, 9, 0], [9, 7, 7, 9]], 4, 4) + self.assertEqual(-376, A.det()) + def test__mul__matrix(self): A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3, 3) x = Vector([1, 2, 3]) From 6e2b2d17e9d1426db2738f74acd64cc91eee1afe Mon Sep 17 00:00:00 2001 From: Codecalec Date: Thu, 24 Oct 2019 10:23:21 +0200 Subject: [PATCH 2/5] Changed determinate function name --- linear_algebra/README.md | 2 +- linear_algebra/src/lib.py | 2 +- linear_algebra/src/tests.py | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/linear_algebra/README.md b/linear_algebra/README.md index 72f46829cbac..169cd074d396 100644 --- a/linear_algebra/README.md +++ b/linear_algebra/README.md @@ -46,7 +46,7 @@ This module contains some useful classes and functions for dealing with linear a - component(x,y) : returns the specified component. - width() : returns the width of the matrix - height() : returns the height of the matrix - - det() : returns the determinate of the matrix if it is square + - determinate() : returns the determinate of the matrix if it is square - operator + : implements the matrix-addition. - operator - _ implements the matrix-subtraction diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 1ef107666d34..813e574ec86a 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -277,7 +277,7 @@ def height(self): """ return self.__height - def det(self): + def determinate(self) -> float: """ returns the determinate of an nxn matrix using Laplace expansion """ diff --git a/linear_algebra/src/tests.py b/linear_algebra/src/tests.py index 97b514fa9f7a..108a5832edf7 100644 --- a/linear_algebra/src/tests.py +++ b/linear_algebra/src/tests.py @@ -118,12 +118,12 @@ def test_str_matrix(self): A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n", str(A)) - def test_det(self): + def test_determinate(self): """ - test for det() + test for determinate() """ A = Matrix([[1, 1, 4, 5], [3, 3, 3, 2], [5, 1, 9, 0], [9, 7, 7, 9]], 4, 4) - self.assertEqual(-376, A.det()) + self.assertEqual(-376, A.determinate()) def test__mul__matrix(self): A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3, 3) From e659a77c09eee4802e56273aca62ac2eda05b9fc Mon Sep 17 00:00:00 2001 From: Codecalec Date: Thu, 24 Oct 2019 10:45:01 +0200 Subject: [PATCH 3/5] Changed instance of .det() to .determinate() --- linear_algebra/src/lib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 813e574ec86a..2f7a1775371f 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -293,7 +293,7 @@ def determinate(self) -> float: self.__matrix[0:x] + self.__matrix[x + 1 :], self.__width - 1, self.__height - 1, - ).det() + ).determinate() ) else: return ( From b9da8f6027ee893a9cc46b1d953a963678a6f1ca Mon Sep 17 00:00:00 2001 From: Codecalec Date: Thu, 24 Oct 2019 10:48:55 +0200 Subject: [PATCH 4/5] Added force_test() function --- linear_algebra/src/tests.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/linear_algebra/src/tests.py b/linear_algebra/src/tests.py index 108a5832edf7..c9bbca534f3d 100644 --- a/linear_algebra/src/tests.py +++ b/linear_algebra/src/tests.py @@ -156,6 +156,12 @@ def test_squareZeroMatrix(self): str(squareZeroMatrix(5)), ) - +def force_test() -> None: + """ + This will ensure that pytest runs the unit tests above. + >>> unittest.main() + """ + pass + if __name__ == "__main__": unittest.main() From 04d60d86b0fb79783f0bd36d46647687ae9caf04 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 24 Oct 2019 11:05:41 +0200 Subject: [PATCH 5/5] Update tests.py --- linear_algebra/src/tests.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/linear_algebra/src/tests.py b/linear_algebra/src/tests.py index c9bbca534f3d..4123a7c9e663 100644 --- a/linear_algebra/src/tests.py +++ b/linear_algebra/src/tests.py @@ -159,7 +159,8 @@ def test_squareZeroMatrix(self): def force_test() -> None: """ This will ensure that pytest runs the unit tests above. - >>> unittest.main() + To explore https://github.com/TheAlgorithms/Python/pull/1124 uncomment the line below. + >>> # unittest.main() """ pass