linear_algebra.matrix_inversion

Attributes

mat

Functions

invert_matrix(→ list[list[float]])

Returns the inverse of a square matrix using NumPy.

Module Contents

linear_algebra.matrix_inversion.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.

The exact floating-point representation returned by numpy.linalg.inv can vary slightly across platforms and BLAS/LAPACK backends (e.g. 0.6 vs 0.6000000000000001), so the doctests below round the result to make the expected output deterministic.

>>> [[round(x, 6) for x in row] for row in invert_matrix([[4.0, 7.0], [2.0, 6.0]])]
[[0.6, -0.7], [-0.2, 0.4]]
>>> [[round(x, 6) for x in row] for row in invert_matrix([[1.0, 0.0], [0.0, 2.0]])]
[[1.0, 0.0], [0.0, 0.5]]
>>> invert_matrix([[1.0, 2.0], [0.0, 0.0]])
Traceback (most recent call last):
    ...
ValueError: Matrix is not invertible
linear_algebra.matrix_inversion.mat = [[4.0, 7.0], [2.0, 6.0]]