TheAlgorithms/Python · error · ValueError
This matrix has no inverse.
Error message
This matrix has no inverse.
What it means
Error "This matrix has no inverse." thrown in TheAlgorithms/Python.
Source
Thrown at matrix/inverse_of_matrix.py:74
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]]
"""
d = Decimal
# Check if the provided matrix has 2 rows and 2 columns
# since this implementation only works for 2x2 matrices
if len(matrix) == 2 and len(matrix[0]) == 2 and len(matrix[1]) == 2:
# Calculate the determinant of the matrix
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.")
# Creates a copy of the matrix with swapped positions of the elements
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]
# Calculate the inverse of the matrix
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
):
# Calculate the determinant of the matrix using Sarrus rule
determinant = float(
View on GitHub (pinned to f5988cc097)
Solutions
- The matrix is singular (determinant 0); provide an invertible matrix.
When it happens
Trigger: Thrown at matrix/inverse_of_matrix.py:74 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/988ca5be6e0d90aa.
Report an issue: GitHub.