Skip to content

Commit

Permalink
Added matrix_exponentiation in algorithms/matrix
Browse files Browse the repository at this point in the history
c-utkarsh committed Oct 2, 2020
1 parent a68ec8a commit c5d3e2d
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -209,6 +209,7 @@ If you want to uninstall algorithms, it is as simple as:
- [gcd/lcm](algorithms/maths/gcd.py)
- [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
- [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)
- [matrix_exponentiation](algorithms/matrix/matrix_exponentiation.py)
- [modular_exponential](algorithms/maths/modular_exponential.py)
- [next_bigger](algorithms/maths/next_bigger.py)
- [next_perfect_square](algorithms/maths/next_perfect_square.py)
2 changes: 1 addition & 1 deletion algorithms/matrix/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@

from .matrix_exponentiation import *
60 changes: 60 additions & 0 deletions algorithms/matrix/matrix_exponentiation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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)

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}")

0 comments on commit c5d3e2d

Please sign in to comment.