Skip to content

Commit bf44644

Browse files
Update matrix_inversion.py
1 parent daea179 commit bf44644

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Diff for: linear_algebra/matrix_inversion.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@ def invert_matrix(matrix: list[list[float]]) -> list[list[float]]:
1010
1111
Returns:
1212
list[list[float]]: Inverted matrix if invertible, else raises error.
13+
14+
>>> invert_matrix([[4.0, 7.0], [2.0, 6.0]])
15+
[[4.0, 7.0], [2.0, 6.0]]
16+
>>> invert_matrix([[1.0, 2.0], [0.0, 0.0]])
17+
[[4.0, 7.0], [2.0, 6.0]]
1318
"""
19+
np_matrix = np.array(matrix)
20+
1421
try:
15-
np_matrix = np.array(matrix)
1622
inv_matrix = np.linalg.inv(np_matrix)
17-
return inv_matrix.tolist()
1823
except np.linalg.LinAlgError:
1924
raise ValueError("Matrix is not invertible")
2025

26+
return inv_matrix.tolist()
27+
2128

2229
if __name__ == "__main__":
2330
mat = [[4.0, 7.0], [2.0, 6.0]]

0 commit comments

Comments
 (0)