-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquareMatrix.py
More file actions
202 lines (161 loc) · 4.21 KB
/
Copy pathSquareMatrix.py
File metadata and controls
202 lines (161 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""
Contains the class SquareMatrix
"""
from Matrix import *
from itertools import permutations # for det method
from typing import Iterable # for function annotation
class SquareMatrix(Matrix):
"""
A class for 2D square matrices supporting basic square matrix operations.
Attributes:
_rows (int)
_cols (int): always equal to _rows
_grid (list of lists): contains the elements of the matrix
"""
# Constructor overloading using decorators
# from third party library called multimethod
@multimethod
def __init__(self,
rows: int,
fill_value = 0):
"""
Constructor #1
Args:
rows (int): no. of rows
fill_value (int, optional): value to populate matrix with; defaults to 0
"""
super().__init__(rows, rows, fill_value)
@__init__.register
def __init__(self,
mat: list):
"""
Constructor #2
Args:
mat (list): a list of lists containing elements
Raises:
ValueError: Input is not a square matrix
"""
super().__init__(mat)
if self._rows != self._cols:
raise ValueError("Input is not a square matrix")
@classmethod
def __idx_of_minimum(cls, lst: list) -> int:
"""
Utility method for det
Args:
lst (list)
Returns:
int: index of minimum
"""
return lst.index(min(lst))
@classmethod
def __parity_of_permutation(cls, lst: Iterable) -> int:
"""
Utility method for det
Args:
lst (Iterable): a permutation
Returns:
int: +1 or -1 depending on the permutation `lst`
"""
parity = 1
lst = list(lst)
for i in range(0, len(lst) - 1):
if lst[i] != i:
parity *= -1
mn = SquareMatrix.__idx_of_minimum(lst[i:]) + i
lst[i], lst[mn] = lst[mn], lst[i]
return parity
def det(self):
"""
Determinant using Leibnitz formula
https://en.wikipedia.org/wiki/Leibniz_formula_for_determinants
Returns:
number
"""
rows = self._rows
sign = +1
summ = 0
for perm in permutations(range(rows), rows):
mul = 1
sign = SquareMatrix.__parity_of_permutation(perm)
for i in range(rows):
mul *= self[i][perm[i]]
summ += sign * mul
return summ
def __pow__(self, exponent: int):
"""
Power using matrix multiplication
Args:
exponent (int)
Returns:
SquareMatrix
Raises:
ValueError: Negative powers not supported
"""
if exponent < 0:
raise ValueError("Negative powers not supported")
elif exponent == 0:
return SquareMatrix(self._rows, 1)
else:
res = self
for i in range(1, exponent):
res *= self
return res
def trace(self):
"""
Returns:
number: sum of diagonal elements
"""
summ = 0
for i in range(self._rows):
summ += self._grid[i][i]
return summ
def minor(self, row_number: int, col_number: int):
"""
Minor of the matrix about (row_number, col_number)
Args:
row_number (int): row to delete
col_number (int): col to delete
Returns:
SquareMatrix: of row size one less than self
Raises:
IndexError: One or both indices out of range
"""
if not(0 <= row_number < self._rows) or \
not(0 <= col_number < self._cols):
raise IndexError("One or both indices out of range.")
submat = SquareMatrix(self._rows - 1)
# submat = self without row `row_number` and col `col_number`
included_row_nos = list(range(self._rows)); del included_row_nos[row_number]
included_col_nos = list(range(self._cols)); del included_col_nos[col_number]
for i_submat, i_mat in zip(range(submat._rows), iter(included_row_nos)):
for j_submat, j_mat in zip(range(submat._rows), iter(included_col_nos)):
submat[i_submat][j_submat] = self[i_mat][j_mat]
return submat.det()
def adj(self):
"""
Adjugate of the matrix
res[i][j] = ((-1) ** (i + j)) * mat.minor(j, i)
Returns:
SquareMatrix
"""
res = SquareMatrix(self._rows)
for i in range(self._rows):
for j in range(self._rows):
res[i][j] = ((-1) ** (i + j)) * self.minor(j, i)
return res
def inv(self):
"""
Inverse of the matrix = Adjugate / Determinant
Returns:
SquareMatrix
Raises:
ValueError: Not Invertible (when determinant = 0)
"""
determinant = self.det()
if determinant:
return self.adj() / determinant
else:
raise ValueError("Not Invertible")
if __name__ == '__main__':
help(SquareMatrix.trace)