Skip to content

Commit

Permalink
Added tests to tests/test_matrix and modified __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
c-utkarsh committed Oct 2, 2020
1 parent 0e61ad8 commit d1e1956
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
1 change: 0 additions & 1 deletion algorithms/matrix/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from .matrix_exponentiation import *
19 changes: 0 additions & 19 deletions algorithms/matrix/matrix_exponentiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,3 @@ def matrix_exponentiation(mat: list, n: int) -> list:
else:
tmp = matrix_exponentiation(mat, n // 2)
return multiply(tmp, tmp)

if __name__ == "__main__":
mat = [[1, 0, 2], [2, 1, 0], [0, 2, 1]]

res0 = matrix_exponentiation(mat, 0)
assert res0 == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
print(f"{mat}^0 = {res0}")

res1 = matrix_exponentiation(mat, 1)
assert res1 == [[1, 0, 2], [2, 1, 0], [0, 2, 1]]
print(f"{mat}^1 = {res1}")

res2 = matrix_exponentiation(mat, 2)
assert res2 == [[1, 4, 4], [4, 1, 4], [4, 4, 1]]
print(f"{mat}^2 = {res2}")

res5 = matrix_exponentiation(mat, 5)
assert res5 == [[81, 72, 90], [90, 81, 72], [72, 90, 81]]
print(f"{mat}^5 = {res5}")
24 changes: 24 additions & 0 deletions tests/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
copy_transform,
crout_matrix_decomposition,
cholesky_matrix_decomposition,
matrix_exponentiation,
matrix_inversion,
multiply,
rotate_image,
Expand Down Expand Up @@ -159,6 +160,29 @@ def test_inversion(self):
[Fraction(13, 53), Fraction(-22, 53), Fraction(5, 53)]])


class TestMatrixExponentiation(unittest.TestCase):
"""[summary]
Test for the file matrix_exponentiation.py
Arguments:
unittest {[type]} -- [description]
"""

def test_matrix_exponentiation(self):
mat = [[1, 0, 2], [2, 1, 0], [0, 2, 1]]

self.assertEqual(matrix_exponentiation.matrix_exponentiation(mat, 0),
[[1, 0, 0], [0, 1, 0], [0, 0, 1]])

self.assertEqual(matrix_exponentiation.matrix_exponentiation(mat, 1),
[[1, 0, 2], [2, 1, 0], [0, 2, 1]])

self.assertEqual(matrix_exponentiation.matrix_exponentiation(mat, 2),
[[1, 4, 4], [4, 1, 4], [4, 4, 1]])

self.assertEqual(matrix_exponentiation.matrix_exponentiation(mat, 5),
[[81, 72, 90], [90, 81, 72], [72, 90, 81]])


class TestMultiply(unittest.TestCase):
"""[summary]
Expand Down

0 comments on commit d1e1956

Please sign in to comment.