a matrix multiplied with its inverse gives the identity matrix this function finds the inverse of a 2x2 and 3x3 matrix if the determinant of a matrix is 0 its inverse does not exist sources for fixing inaccurate float arithmetic https stackoverflow comquestions6563058howdoiuseaccuratefloatarithmeticinpython https docs python org3librarydecimal html doctests for 2x2 inverseofmatrix2 5 2 0 0 0 0 5 0 2 0 2 inverseofmatrix2 5 5 1 2 traceback most recent call last valueerror this matrix has no inverse inverseofmatrix12 16 9 0 0 0 0 1111111111111111 0 0625 0 08333333333333333 inverseofmatrix12 3 16 8 0 16666666666666666 0 0625 0 3333333333333333 0 25 inverseofmatrix10 5 3 2 5 0 25 0 5 0 3 1 0 doctests for 3x3 inverseofmatrix2 5 7 2 0 1 1 2 3 2 0 5 0 4 0 1 0 1 0 1 0 5 0 12 0 10 0 inverseofmatrix1 2 2 1 2 2 3 2 1 traceback most recent call last valueerror this matrix has no inverse inverseofmatrix traceback most recent call last valueerror please provide a matrix of size 2x2 or 3x3 inverseofmatrix1 2 3 4 5 6 traceback most recent call last valueerror please provide a matrix of size 2x2 or 3x3 inverseofmatrix1 2 1 0 3 4 traceback most recent call last valueerror please provide a matrix of size 2x2 or 3x3 inverseofmatrix1 2 3 7 8 9 7 8 9 traceback most recent call last valueerror this matrix has no inverse inverseofmatrix1 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 check if the provided matrix has 2 rows and 2 columns since this implementation only works for 2x2 matrices calculate the determinant of the matrix creates a copy of the matrix with swapped positions of the elements calculate the inverse of the matrix calculate the determinant of the matrix using sarrus rule creating cofactor matrix transpose the cofactor matrix adjoint matrix inverse of the matrix using the formula 1determinant adjoint matrix calculate the inverse of the matrix a matrix multiplied with its inverse gives the identity matrix this function finds the inverse of a 2x2 and 3x3 matrix if the determinant of a matrix is 0 its inverse does not exist sources for fixing inaccurate float arithmetic https stackoverflow com questions 6563058 how do i use accurate float arithmetic in python https docs python org 3 library decimal html doctests for 2x2 inverse_of_matrix 2 5 2 0 0 0 0 5 0 2 0 2 inverse_of_matrix 2 5 5 1 2 traceback most recent call last valueerror this matrix has no inverse inverse_of_matrix 12 16 9 0 0 0 0 1111111111111111 0 0625 0 08333333333333333 inverse_of_matrix 12 3 16 8 0 16666666666666666 0 0625 0 3333333333333333 0 25 inverse_of_matrix 10 5 3 2 5 0 25 0 5 0 3 1 0 doctests for 3x3 inverse_of_matrix 2 5 7 2 0 1 1 2 3 2 0 5 0 4 0 1 0 1 0 1 0 5 0 12 0 10 0 inverse_of_matrix 1 2 2 1 2 2 3 2 1 traceback most recent call last valueerror this matrix has no inverse inverse_of_matrix traceback most recent call last valueerror please provide a matrix of size 2x2 or 3x3 inverse_of_matrix 1 2 3 4 5 6 traceback most recent call last valueerror please provide a matrix of size 2x2 or 3x3 inverse_of_matrix 1 2 1 0 3 4 traceback most recent call last valueerror please provide a matrix of size 2x2 or 3x3 inverse_of_matrix 1 2 3 7 8 9 7 8 9 traceback most recent call last valueerror this matrix has no inverse inverse_of_matrix 1 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 check if the provided matrix has 2 rows and 2 columns since this implementation only works for 2x2 matrices calculate the determinant of the matrix creates a copy of the matrix with swapped positions of the elements calculate the inverse of the matrix calculate the determinant of the matrix using sarrus rule creating cofactor matrix transpose the cofactor matrix adjoint matrix inverse of the matrix using the formula 1 determinant adjoint matrix calculate the inverse of the matrix
from __future__ import annotations

from decimal import Decimal

from numpy import array


def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]:
    

    d = Decimal

    
    
    if len(matrix) == 2 and len(matrix[0]) == 2 and len(matrix[1]) == 2:
        
        determinant = float(
            d(matrix[0][0]) * d(matrix[1][1]) - d(matrix[1][0]) * d(matrix[0][1])
        )
        if determinant == 0:
            raise ValueError("This matrix has no inverse.")

        
        swapped_matrix = [[0.0, 0.0], [0.0, 0.0]]
        swapped_matrix[0][0], swapped_matrix[1][1] = matrix[1][1], matrix[0][0]
        swapped_matrix[1][0], swapped_matrix[0][1] = -matrix[1][0], -matrix[0][1]

        
        return [
            [(float(d(n)) / determinant) or 0.0 for n in row] for row in swapped_matrix
        ]
    elif (
        len(matrix) == 3
        and len(matrix[0]) == 3
        and len(matrix[1]) == 3
        and len(matrix[2]) == 3
    ):
        
        determinant = float(
            (
                (d(matrix[0][0]) * d(matrix[1][1]) * d(matrix[2][2]))
                + (d(matrix[0][1]) * d(matrix[1][2]) * d(matrix[2][0]))
                + (d(matrix[0][2]) * d(matrix[1][0]) * d(matrix[2][1]))
            )
            - (
                (d(matrix[0][2]) * d(matrix[1][1]) * d(matrix[2][0]))
                + (d(matrix[0][1]) * d(matrix[1][0]) * d(matrix[2][2]))
                + (d(matrix[0][0]) * d(matrix[1][2]) * d(matrix[2][1]))
            )
        )
        if determinant == 0:
            raise ValueError("This matrix has no inverse.")

        
        cofactor_matrix = [
            [d(0.0), d(0.0), d(0.0)],
            [d(0.0), d(0.0), d(0.0)],
            [d(0.0), d(0.0), d(0.0)],
        ]
        cofactor_matrix[0][0] = (d(matrix[1][1]) * d(matrix[2][2])) - (
            d(matrix[1][2]) * d(matrix[2][1])
        )
        cofactor_matrix[0][1] = -(
            (d(matrix[1][0]) * d(matrix[2][2])) - (d(matrix[1][2]) * d(matrix[2][0]))
        )
        cofactor_matrix[0][2] = (d(matrix[1][0]) * d(matrix[2][1])) - (
            d(matrix[1][1]) * d(matrix[2][0])
        )
        cofactor_matrix[1][0] = -(
            (d(matrix[0][1]) * d(matrix[2][2])) - (d(matrix[0][2]) * d(matrix[2][1]))
        )
        cofactor_matrix[1][1] = (d(matrix[0][0]) * d(matrix[2][2])) - (
            d(matrix[0][2]) * d(matrix[2][0])
        )
        cofactor_matrix[1][2] = -(
            (d(matrix[0][0]) * d(matrix[2][1])) - (d(matrix[0][1]) * d(matrix[2][0]))
        )
        cofactor_matrix[2][0] = (d(matrix[0][1]) * d(matrix[1][2])) - (
            d(matrix[0][2]) * d(matrix[1][1])
        )
        cofactor_matrix[2][1] = -(
            (d(matrix[0][0]) * d(matrix[1][2])) - (d(matrix[0][2]) * d(matrix[1][0]))
        )
        cofactor_matrix[2][2] = (d(matrix[0][0]) * d(matrix[1][1])) - (
            d(matrix[0][1]) * d(matrix[1][0])
        )

        
        adjoint_matrix = array(cofactor_matrix)
        for i in range(3):
            for j in range(3):
                adjoint_matrix[i][j] = cofactor_matrix[j][i]

        
        inverse_matrix = array(cofactor_matrix)
        for i in range(3):
            for j in range(3):
                inverse_matrix[i][j] /= d(determinant)

        
        return [[float(d(n)) or 0.0 for n in row] for row in inverse_matrix]
    raise ValueError("Please provide a matrix of size 2x2 or 3x3.")