TheAlgorithms/Python · error · Exception
Matrix has no element
Error message
Matrix has no element
What it means
Raised by Matrix.determinant() in linear_algebra/src/lib.py:412 when the matrix has height < 1, i.e. it is a 0x0 (empty) Matrix. The Laplace expansion handles the 1x1 and 2x2 base cases explicitly, but an empty matrix has no elements to expand on, so the code treats it as invalid input and raises a bare Exception 'Matrix has no element' rather than returning the mathematically conventional 1 (empty product).
Source
Thrown at linear_algebra/src/lib.py:412
def cofactor(self, x: int, y: int) -> float:
"""
returns the cofactor (signed minor) along (x, y)
"""
if self.__height != self.__width:
raise Exception("Matrix is not square")
if 0 <= x < self.__height and 0 <= y < self.__width:
return (-1) ** (x + y) * self.minor(x, y)
else:
raise Exception("Indices out of bounds")
def determinant(self) -> float:
"""
returns the determinant of an nxn matrix using Laplace expansion
"""
if self.__height != self.__width:
raise Exception("Matrix is not square")
if self.__height < 1:
raise Exception("Matrix has no element")
elif self.__height == 1:
return self.__matrix[0][0]
elif self.__height == 2:
return (
self.__matrix[0][0] * self.__matrix[1][1]
- self.__matrix[0][1] * self.__matrix[1][0]
)
else:
cofactor_prods = [
self.__matrix[0][y] * self.cofactor(0, y) for y in range(self.__width)
]
return sum(cofactor_prods)
def square_zero_matrix(n: int) -> Matrix:
"""
returns a square zero-matrix of dimension NxN
"""View on GitHub (pinned to f5988cc097)
Solutions
- Check m.height() == 0 (or not any rows) before calling and return 1.0 / skip if the empty case is expected.
- Handle empty input where the Matrix is constructed — early return or default value instead of building an empty Matrix.
- Special-case n == 1 in recursive cofactor code so it never asks for a 0x0 determinant.
- Catch Exception narrowly around the call for defensive handling.
Example fix
// before det = Matrix([], 0, 0).determinant() # Exception: Matrix has no element // after det = 1.0 if m.height() == 0 else m.determinant() # empty product convention
Defensive patterns
Strategy: validation
Validate before calling
if m.height() == 0 or m.width() == 0:
det = 1.0 # empty-product convention, or raise ValueError with context
else:
det = m.determinant() Type guard
from linear_algebra.src.lib import Matrix
def is_nonempty_matrix(obj) -> bool:
return isinstance(obj, Matrix) and obj.height() > 0 and obj.width() > 0 Try / catch
try:
det = m.determinant()
except Exception as e:
if "no element" in str(e):
raise ValueError("determinant of an empty matrix; check upstream data loading") from e
raise Prevention
- Check height() > 0 before determinant(); decide the empty case policy (skip, 1.0, or error) once in a wrapper.
- Guard the data source: empty files/filters should short-circuit before a Matrix is ever constructed.
- In recursive cofactor code, special-case the 1x1 matrix so it never asks for a 0x0 minor's determinant.
- Note this error can also come from minor() on a 1x1 matrix (its submatrix is 0x0) — check the call chain.
When it happens
Trigger: Calling .determinant() on Matrix([], 0, 0) or on a Matrix built from an empty list of rows. Also reachable recursively via minor() when a 1x1 matrix's minor is taken (its submatrix is 0x0) — though the cofactor path stops at 1x1, direct minor() calls on a 1x1 matrix hit this.
Common situations: Empty result sets converted into matrices (no rows parsed from a file, filter matched nothing), or generic recursion code that does not special-case the 1x1 base case and descends to 0x0.
Related errors
- determinant modular {req_l} of encryption key({det}) is not
- 'table' has to be of square shaped array but got a {rows}x{c
- Vector is empty
- matrix must have the same dimension!
- matrices must have the same dimension!
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/29d942da4740d9f7.
Report an issue: GitHub.