Skip to content

Commit

Permalink
Merge pull request #711 from c-utkarsh/add_matrix_exponentiation
Browse files Browse the repository at this point in the history
Added Matrix Exponentiation
  • Loading branch information
goswami-rahul authored Oct 4, 2020
2 parents f6224b2 + d1e1956 commit d04b9a5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ If you want to uninstall algorithms, it is as simple as:
- [bomb_enemy](algorithms/matrix/bomb_enemy.py)
- [copy_transform](algorithms/matrix/copy_transform.py)
- [count_paths](algorithms/matrix/count_paths.py)
- [matrix_exponentiation](algorithms/matrix/matrix_exponentiation.py)
- [matrix_rotation.txt](algorithms/matrix/matrix_rotation.txt)
- [matrix_inversion](algorithms/matrix/matrix_inversion.py)
- [matrix_multiplication](algorithms/matrix/multiply.py)
Expand Down
1 change: 0 additions & 1 deletion algorithms/matrix/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

41 changes: 41 additions & 0 deletions algorithms/matrix/matrix_exponentiation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
def multiply(matA: list, matB: list) -> list:
"""
Multiplies two square matrices matA and matB od size n x n
Time Complexity: O(n^3)
"""
n = len(matA)
matC = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
for j in range(n):
for k in range(n):
matC[i][j] += matA[i][k] * matB[k][j]

return matC

def identity(n: int) -> list:
"""
Returns the Identity matrix of size n x n
Time Complecity: O(n^2)
"""
I = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
I[i][i] = 1

return I

def matrix_exponentiation(mat: list, n: int) -> list:
"""
Calculates mat^n by repeated squaring
Time Complexity: O(d^3 log(n))
d: dimesion of the square matrix mat
n: power the matrix is raised to
"""
if n == 0:
return identity(len(mat))
elif n % 2 == 1:
return multiply(matrix_exponentiation(mat, n - 1), mat)
else:
tmp = matrix_exponentiation(mat, n // 2)
return multiply(tmp, tmp)
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 d04b9a5

Please sign in to comment.