Skip to content

Commit d04b9a5

Browse files
Merge pull request #711 from c-utkarsh/add_matrix_exponentiation
Added Matrix Exponentiation
2 parents f6224b2 + d1e1956 commit d04b9a5

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ If you want to uninstall algorithms, it is as simple as:
230230
- [bomb_enemy](algorithms/matrix/bomb_enemy.py)
231231
- [copy_transform](algorithms/matrix/copy_transform.py)
232232
- [count_paths](algorithms/matrix/count_paths.py)
233+
- [matrix_exponentiation](algorithms/matrix/matrix_exponentiation.py)
233234
- [matrix_rotation.txt](algorithms/matrix/matrix_rotation.txt)
234235
- [matrix_inversion](algorithms/matrix/matrix_inversion.py)
235236
- [matrix_multiplication](algorithms/matrix/multiply.py)

algorithms/matrix/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def multiply(matA: list, matB: list) -> list:
2+
"""
3+
Multiplies two square matrices matA and matB od size n x n
4+
Time Complexity: O(n^3)
5+
"""
6+
n = len(matA)
7+
matC = [[0 for i in range(n)] for j in range(n)]
8+
9+
for i in range(n):
10+
for j in range(n):
11+
for k in range(n):
12+
matC[i][j] += matA[i][k] * matB[k][j]
13+
14+
return matC
15+
16+
def identity(n: int) -> list:
17+
"""
18+
Returns the Identity matrix of size n x n
19+
Time Complecity: O(n^2)
20+
"""
21+
I = [[0 for i in range(n)] for j in range(n)]
22+
23+
for i in range(n):
24+
I[i][i] = 1
25+
26+
return I
27+
28+
def matrix_exponentiation(mat: list, n: int) -> list:
29+
"""
30+
Calculates mat^n by repeated squaring
31+
Time Complexity: O(d^3 log(n))
32+
d: dimesion of the square matrix mat
33+
n: power the matrix is raised to
34+
"""
35+
if n == 0:
36+
return identity(len(mat))
37+
elif n % 2 == 1:
38+
return multiply(matrix_exponentiation(mat, n - 1), mat)
39+
else:
40+
tmp = matrix_exponentiation(mat, n // 2)
41+
return multiply(tmp, tmp)

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)