{"record":{"id":"988ca5be6e0d90aa","repo":"TheAlgorithms/Python","slug":"this-matrix-has-no-inverse","errorCode":null,"errorMessage":"This matrix has no inverse.","messagePattern":"This matrix has no inverse\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"matrix/inverse_of_matrix.py","lineNumber":74,"sourceCode":"    Traceback (most recent call last):\r\n        ...\r\n    ValueError: This matrix has no inverse.\r\n\r\n    >>> inverse_of_matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])\r\n    [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]\r\n    \"\"\"\r\n\r\n    d = Decimal\r\n\r\n    # Check if the provided matrix has 2 rows and 2 columns\r\n    # since this implementation only works for 2x2 matrices\r\n    if len(matrix) == 2 and len(matrix[0]) == 2 and len(matrix[1]) == 2:\r\n        # Calculate the determinant of the matrix\r\n        determinant = float(\r\n            d(matrix[0][0]) * d(matrix[1][1]) - d(matrix[1][0]) * d(matrix[0][1])\r\n        )\r\n        if determinant == 0:\r\n            raise ValueError(\"This matrix has no inverse.\")\r\n\r\n        # Creates a copy of the matrix with swapped positions of the elements\r\n        swapped_matrix = [[0.0, 0.0], [0.0, 0.0]]\r\n        swapped_matrix[0][0], swapped_matrix[1][1] = matrix[1][1], matrix[0][0]\r\n        swapped_matrix[1][0], swapped_matrix[0][1] = -matrix[1][0], -matrix[0][1]\r\n\r\n        # Calculate the inverse of the matrix\r\n        return [\r\n            [(float(d(n)) / determinant) or 0.0 for n in row] for row in swapped_matrix\r\n        ]\r\n    elif (\r\n        len(matrix) == 3\r\n        and len(matrix[0]) == 3\r\n        and len(matrix[1]) == 3\r\n        and len(matrix[2]) == 3\r\n    ):\r\n        # Calculate the determinant of the matrix using Sarrus rule\r\n        determinant = float(\r","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/matrix/inverse_of_matrix.py#L56-L92","documentation":"Error \"This matrix has no inverse.\" thrown in TheAlgorithms/Python.","triggerScenarios":"Thrown at matrix/inverse_of_matrix.py:74 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["The matrix is singular (determinant 0); provide an invertible matrix."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}