5
5
from numpy import array
6
6
7
7
8
+ flags = {
9
+ 1 : False ,
10
+ 2 : False ,
11
+ 3 : False ,
12
+ 4 : False ,
13
+ 5 : False ,
14
+ 6 : False ,
15
+ 7 : False ,
16
+ }
17
+
18
+
8
19
def inverse_of_matrix (matrix : list [list [float ]]) -> list [list [float ]]:
9
20
"""
10
21
A matrix multiplied with its inverse gives the identity matrix.
@@ -65,29 +76,42 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]:
65
76
66
77
# Check if the provided matrix has 2 rows and 2 columns
67
78
# since this implementation only works for 2x2 matrices
68
- if len (matrix ) == 2 and len (matrix [0 ]) == 2 and len (matrix [1 ]) == 2 :
79
+ if len (matrix ) == 2 and len (matrix [0 ]) == 2 and len (matrix [1 ]) == 2 : # ID: 1
80
+ flags [1 ] = True
81
+
69
82
# Calculate the determinant of the matrix
70
83
determinant = float (
71
84
d (matrix [0 ][0 ]) * d (matrix [1 ][1 ]) - d (matrix [1 ][0 ]) * d (matrix [0 ][1 ])
72
85
)
73
- if determinant == 0 :
86
+ if determinant == 0 : # ID: 2
87
+ flags [2 ] = True
74
88
raise ValueError ("This matrix has no inverse." )
75
-
76
- # Creates a copy of the matrix with swapped positions of the elements
77
- swapped_matrix = [[0.0 , 0.0 ], [0.0 , 0.0 ]]
78
- swapped_matrix [0 ][0 ], swapped_matrix [1 ][1 ] = matrix [1 ][1 ], matrix [0 ][0 ]
79
- swapped_matrix [1 ][0 ], swapped_matrix [0 ][1 ] = - matrix [1 ][0 ], - matrix [0 ][1 ]
80
-
81
- # Calculate the inverse of the matrix
82
- return [
83
- [(float (d (n )) / determinant ) or 0.0 for n in row ] for row in swapped_matrix
84
- ]
85
- elif (
89
+ else : # ID: 3
90
+ flags [3 ] = True
91
+ # Creates a copy of the matrix with swapped positions of the elements
92
+ swapped_matrix = [[0.0 , 0.0 ], [0.0 , 0.0 ]]
93
+ swapped_matrix [0 ][0 ], swapped_matrix [1 ][1 ] = matrix [1 ][1 ], matrix [0 ][0 ]
94
+ swapped_matrix [1 ][0 ], swapped_matrix [0 ][1 ] = - matrix [1 ][0 ], - matrix [0 ][1 ]
95
+
96
+ # Calculate the inverse of the matrix
97
+ result = []
98
+ for row in swapped_matrix :
99
+
100
+ res_row = []
101
+ for n in row :
102
+ res_row .append ((float (d (n )) / determinant ) or 0.0 )
103
+
104
+ result .append (res_row )
105
+
106
+ return result
107
+
108
+ elif ( # ID: 4
86
109
len (matrix ) == 3
87
110
and len (matrix [0 ]) == 3
88
111
and len (matrix [1 ]) == 3
89
112
and len (matrix [2 ]) == 3
90
113
):
114
+ flags [4 ] = True
91
115
# Calculate the determinant of the matrix using Sarrus rule
92
116
determinant = float (
93
117
(
@@ -101,55 +125,68 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]:
101
125
+ (d (matrix [0 ][0 ]) * d (matrix [1 ][2 ]) * d (matrix [2 ][1 ]))
102
126
)
103
127
)
104
- if determinant == 0 :
128
+ if determinant == 0 : # ID: 5
129
+ flags [5 ] = True
105
130
raise ValueError ("This matrix has no inverse." )
106
131
107
- # Creating cofactor matrix
108
- cofactor_matrix = [
109
- [d (0.0 ), d (0.0 ), d (0.0 )],
110
- [d (0.0 ), d (0.0 ), d (0.0 )],
111
- [d (0.0 ), d (0.0 ), d (0.0 )],
112
- ]
113
- cofactor_matrix [0 ][0 ] = (d (matrix [1 ][1 ]) * d (matrix [2 ][2 ])) - (
114
- d (matrix [1 ][2 ]) * d (matrix [2 ][1 ])
115
- )
116
- cofactor_matrix [0 ][1 ] = - (
117
- (d (matrix [1 ][0 ]) * d (matrix [2 ][2 ])) - (d (matrix [1 ][2 ]) * d (matrix [2 ][0 ]))
118
- )
119
- cofactor_matrix [0 ][2 ] = (d (matrix [1 ][0 ]) * d (matrix [2 ][1 ])) - (
120
- d (matrix [1 ][1 ]) * d (matrix [2 ][0 ])
121
- )
122
- cofactor_matrix [1 ][0 ] = - (
123
- (d (matrix [0 ][1 ]) * d (matrix [2 ][2 ])) - (d (matrix [0 ][2 ]) * d (matrix [2 ][1 ]))
124
- )
125
- cofactor_matrix [1 ][1 ] = (d (matrix [0 ][0 ]) * d (matrix [2 ][2 ])) - (
126
- d (matrix [0 ][2 ]) * d (matrix [2 ][0 ])
127
- )
128
- cofactor_matrix [1 ][2 ] = - (
129
- (d (matrix [0 ][0 ]) * d (matrix [2 ][1 ])) - (d (matrix [0 ][1 ]) * d (matrix [2 ][0 ]))
130
- )
131
- cofactor_matrix [2 ][0 ] = (d (matrix [0 ][1 ]) * d (matrix [1 ][2 ])) - (
132
- d (matrix [0 ][2 ]) * d (matrix [1 ][1 ])
133
- )
134
- cofactor_matrix [2 ][1 ] = - (
135
- (d (matrix [0 ][0 ]) * d (matrix [1 ][2 ])) - (d (matrix [0 ][2 ]) * d (matrix [1 ][0 ]))
136
- )
137
- cofactor_matrix [2 ][2 ] = (d (matrix [0 ][0 ]) * d (matrix [1 ][1 ])) - (
138
- d (matrix [0 ][1 ]) * d (matrix [1 ][0 ])
139
- )
132
+ else : # ID: 6
133
+ flags [6 ] = True
134
+ # Creating cofactor matrix
135
+ cofactor_matrix = [
136
+ [d (0.0 ), d (0.0 ), d (0.0 )],
137
+ [d (0.0 ), d (0.0 ), d (0.0 )],
138
+ [d (0.0 ), d (0.0 ), d (0.0 )],
139
+ ]
140
+ cofactor_matrix [0 ][0 ] = (d (matrix [1 ][1 ]) * d (matrix [2 ][2 ])) - (
141
+ d (matrix [1 ][2 ]) * d (matrix [2 ][1 ])
142
+ )
143
+ cofactor_matrix [0 ][1 ] = - (
144
+ (d (matrix [1 ][0 ]) * d (matrix [2 ][2 ])) - (d (matrix [1 ][2 ]) * d (matrix [2 ][0 ]))
145
+ )
146
+ cofactor_matrix [0 ][2 ] = (d (matrix [1 ][0 ]) * d (matrix [2 ][1 ])) - (
147
+ d (matrix [1 ][1 ]) * d (matrix [2 ][0 ])
148
+ )
149
+ cofactor_matrix [1 ][0 ] = - (
150
+ (d (matrix [0 ][1 ]) * d (matrix [2 ][2 ])) - (d (matrix [0 ][2 ]) * d (matrix [2 ][1 ]))
151
+ )
152
+ cofactor_matrix [1 ][1 ] = (d (matrix [0 ][0 ]) * d (matrix [2 ][2 ])) - (
153
+ d (matrix [0 ][2 ]) * d (matrix [2 ][0 ])
154
+ )
155
+ cofactor_matrix [1 ][2 ] = - (
156
+ (d (matrix [0 ][0 ]) * d (matrix [2 ][1 ])) - (d (matrix [0 ][1 ]) * d (matrix [2 ][0 ]))
157
+ )
158
+ cofactor_matrix [2 ][0 ] = (d (matrix [0 ][1 ]) * d (matrix [1 ][2 ])) - (
159
+ d (matrix [0 ][2 ]) * d (matrix [1 ][1 ])
160
+ )
161
+ cofactor_matrix [2 ][1 ] = - (
162
+ (d (matrix [0 ][0 ]) * d (matrix [1 ][2 ])) - (d (matrix [0 ][2 ]) * d (matrix [1 ][0 ]))
163
+ )
164
+ cofactor_matrix [2 ][2 ] = (d (matrix [0 ][0 ]) * d (matrix [1 ][1 ])) - (
165
+ d (matrix [0 ][1 ]) * d (matrix [1 ][0 ])
166
+ )
140
167
141
- # Transpose the cofactor matrix (Adjoint matrix)
142
- adjoint_matrix = array (cofactor_matrix )
143
- for i in range (3 ):
144
- for j in range (3 ):
145
- adjoint_matrix [i ][j ] = cofactor_matrix [j ][i ]
146
-
147
- # Inverse of the matrix using the formula (1/determinant) * adjoint matrix
148
- inverse_matrix = array (cofactor_matrix )
149
- for i in range (3 ):
150
- for j in range (3 ):
151
- inverse_matrix [i ][j ] /= d (determinant )
152
-
153
- # Calculate the inverse of the matrix
154
- return [[float (d (n )) or 0.0 for n in row ] for row in inverse_matrix ]
155
- raise ValueError ("Please provide a matrix of size 2x2 or 3x3." )
168
+ # Transpose the cofactor matrix (Adjoint matrix)
169
+ adjoint_matrix = array (cofactor_matrix )
170
+ for i in range (3 ):
171
+ for j in range (3 ):
172
+ adjoint_matrix [i ][j ] = cofactor_matrix [j ][i ]
173
+
174
+ # Inverse of the matrix using the formula (1/determinant) * adjoint matrix
175
+ inverse_matrix = array (cofactor_matrix )
176
+ for i in range (3 ):
177
+ for j in range (3 ):
178
+ inverse_matrix [i ][j ] /= d (determinant )
179
+
180
+ # Calculate the inverse of the matrix
181
+ return [[float (d (n )) or 0.0 for n in row ] for row in inverse_matrix ]
182
+ else :
183
+ # ID: 7
184
+ flags [7 ] = True
185
+ raise ValueError ("Please provide a matrix of size 2x2 or 3x3." )
186
+
187
+
188
+ if __name__ == "__main__" :
189
+ import doctest
190
+ doctest .testmod ()
191
+
192
+ print (flags )
0 commit comments