MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / Matrix

Class Matrix

maths/matrix_exponentiation.py:13–28  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11
12
13class Matrix:
14 def __init__(self, arg: list[list] | int) -> None:
15 if isinstance(arg, list): # Initializes a matrix identical to the one provided.
16 self.t = arg
17 self.n = len(arg)
18 else: # Initializes a square matrix of the given size and set values to zero.
19 self.n = arg
20 self.t = [[0 for _ in range(self.n)] for _ in range(self.n)]
21
22 def __mul__(self, b: Matrix) -> Matrix:
23 matrix = Matrix(self.n)
24 for i in range(self.n):
25 for j in range(self.n):
26 for k in range(self.n):
27 matrix.t[i][j] += self.t[i][k] * b.t[k][j]
28 return matrix
29
30
31def modular_exponentiation(a: Matrix, b: int) -> Matrix:

Callers 3

__mul__Method · 0.70
modular_exponentiationFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected