Skip to content

Commit d1e1956

Browse files
committed
Added tests to tests/test_matrix and modified __init__.py
1 parent 0e61ad8 commit d1e1956

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

algorithms/matrix/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
from .matrix_exponentiation import *

algorithms/matrix/matrix_exponentiation.py

-19
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,3 @@ def matrix_exponentiation(mat: list, n: int) -> list:
3939
else:
4040
tmp = matrix_exponentiation(mat, n // 2)
4141
return multiply(tmp, tmp)
42-
43-
if __name__ == "__main__":
44-
mat = [[1, 0, 2], [2, 1, 0], [0, 2, 1]]
45-
46-
res0 = matrix_exponentiation(mat, 0)
47-
assert res0 == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
48-
print(f"{mat}^0 = {res0}")
49-
50-
res1 = matrix_exponentiation(mat, 1)
51-
assert res1 == [[1, 0, 2], [2, 1, 0], [0, 2, 1]]
52-
print(f"{mat}^1 = {res1}")
53-
54-
res2 = matrix_exponentiation(mat, 2)
55-
assert res2 == [[1, 4, 4], [4, 1, 4], [4, 4, 1]]
56-
print(f"{mat}^2 = {res2}")
57-
58-
res5 = matrix_exponentiation(mat, 5)
59-
assert res5 == [[81, 72, 90], [90, 81, 72], [72, 90, 81]]
60-
print(f"{mat}^5 = {res5}")

tests/test_matrix.py

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
copy_transform,
44
crout_matrix_decomposition,
55
cholesky_matrix_decomposition,
6+
matrix_exponentiation,
67
matrix_inversion,
78
multiply,
89
rotate_image,
@@ -159,6 +160,29 @@ def test_inversion(self):
159160
[Fraction(13, 53), Fraction(-22, 53), Fraction(5, 53)]])
160161

161162

163+
class TestMatrixExponentiation(unittest.TestCase):
164+
"""[summary]
165+
Test for the file matrix_exponentiation.py
166+
167+
Arguments:
168+
unittest {[type]} -- [description]
169+
"""
170+
171+
def test_matrix_exponentiation(self):
172+
mat = [[1, 0, 2], [2, 1, 0], [0, 2, 1]]
173+
174+
self.assertEqual(matrix_exponentiation.matrix_exponentiation(mat, 0),
175+
[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
176+
177+
self.assertEqual(matrix_exponentiation.matrix_exponentiation(mat, 1),
178+
[[1, 0, 2], [2, 1, 0], [0, 2, 1]])
179+
180+
self.assertEqual(matrix_exponentiation.matrix_exponentiation(mat, 2),
181+
[[1, 4, 4], [4, 1, 4], [4, 4, 1]])
182+
183+
self.assertEqual(matrix_exponentiation.matrix_exponentiation(mat, 5),
184+
[[81, 72, 90], [90, 81, 72], [72, 90, 81]])
185+
162186

163187
class TestMultiply(unittest.TestCase):
164188
"""[summary]

0 commit comments

Comments
 (0)