mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-05-06 20:33:58 +00:00
Add matrix inversion algorithm using NumPy (#12657)
* Create matrix_inversion.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update matrix_inversion.py * Update matrix_inversion.py * Update matrix_inversion.py * Update matrix_inversion.py * Update matrix_inversion.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
This commit is contained in:
parent
d123cbc649
commit
42820634f3
36
linear_algebra/matrix_inversion.py
Normal file
36
linear_algebra/matrix_inversion.py
Normal file
@ -0,0 +1,36 @@
|
||||
import numpy as np
|
||||
|
||||
|
||||
def invert_matrix(matrix: list[list[float]]) -> list[list[float]]:
|
||||
"""
|
||||
Returns the inverse of a square matrix using NumPy.
|
||||
|
||||
Parameters:
|
||||
matrix (list[list[float]]): A square matrix.
|
||||
|
||||
Returns:
|
||||
list[list[float]]: Inverted matrix if invertible, else raises error.
|
||||
|
||||
>>> invert_matrix([[4.0, 7.0], [2.0, 6.0]])
|
||||
[[0.6000000000000001, -0.7000000000000001], [-0.2, 0.4]]
|
||||
>>> invert_matrix([[1.0, 2.0], [0.0, 0.0]])
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Matrix is not invertible
|
||||
"""
|
||||
np_matrix = np.array(matrix)
|
||||
|
||||
try:
|
||||
inv_matrix = np.linalg.inv(np_matrix)
|
||||
except np.linalg.LinAlgError:
|
||||
raise ValueError("Matrix is not invertible")
|
||||
|
||||
return inv_matrix.tolist()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
mat = [[4.0, 7.0], [2.0, 6.0]]
|
||||
print("Original Matrix:")
|
||||
print(mat)
|
||||
print("Inverted Matrix:")
|
||||
print(invert_matrix(mat))
|
Loading…
x
Reference in New Issue
Block a user