-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #711 from c-utkarsh/add_matrix_exponentiation
Added Matrix Exponentiation
- Loading branch information
Showing
4 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters