Skip to content

Commit 0748313

Browse files
codecaleccclauss
authored andcommitted
Added determinate function (TheAlgorithms#1429)
* Added determinate function * Changed determinate function name * Changed instance of .det() to .determinate() * Added force_test() function * Update tests.py
1 parent 7b3d385 commit 0748313

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

linear_algebra/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ This module contains some useful classes and functions for dealing with linear a
4545
- changeComponent(x,y,value) : changes the specified component.
4646
- component(x,y) : returns the specified component.
4747
- width() : returns the width of the matrix
48-
- height() : returns the height of the matrix
48+
- height() : returns the height of the matrix
49+
- determinate() : returns the determinate of the matrix if it is square
4950
- operator + : implements the matrix-addition.
5051
- operator - _ implements the matrix-subtraction
5152

linear_algebra/src/lib.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,33 @@ def height(self):
277277
"""
278278
return self.__height
279279

280+
def determinate(self) -> float:
281+
"""
282+
returns the determinate of an nxn matrix using Laplace expansion
283+
"""
284+
if self.__height == self.__width and self.__width >= 2:
285+
total = 0
286+
if self.__width > 2:
287+
for x in range(0, self.__width):
288+
for y in range(0, self.__height):
289+
total += (
290+
self.__matrix[x][y]
291+
* (-1) ** (x + y)
292+
* Matrix(
293+
self.__matrix[0:x] + self.__matrix[x + 1 :],
294+
self.__width - 1,
295+
self.__height - 1,
296+
).determinate()
297+
)
298+
else:
299+
return (
300+
self.__matrix[0][0] * self.__matrix[1][1]
301+
- self.__matrix[0][1] * self.__matrix[1][0]
302+
)
303+
return total
304+
else:
305+
raise Exception("matrix is not square")
306+
280307
def __mul__(self, other):
281308
"""
282309
implements the matrix-vector multiplication.

linear_algebra/src/tests.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ def test_str_matrix(self):
118118
A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
119119
self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n", str(A))
120120

121+
def test_determinate(self):
122+
"""
123+
test for determinate()
124+
"""
125+
A = Matrix([[1, 1, 4, 5], [3, 3, 3, 2], [5, 1, 9, 0], [9, 7, 7, 9]], 4, 4)
126+
self.assertEqual(-376, A.determinate())
127+
121128
def test__mul__matrix(self):
122129
A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3, 3)
123130
x = Vector([1, 2, 3])
@@ -149,6 +156,13 @@ def test_squareZeroMatrix(self):
149156
str(squareZeroMatrix(5)),
150157
)
151158

152-
159+
def force_test() -> None:
160+
"""
161+
This will ensure that pytest runs the unit tests above.
162+
To explore https://github.com/TheAlgorithms/Python/pull/1124 uncomment the line below.
163+
>>> # unittest.main()
164+
"""
165+
pass
166+
153167
if __name__ == "__main__":
154168
unittest.main()

0 commit comments

Comments
 (0)