{"record":{"id":"29d942da4740d9f7","repo":"TheAlgorithms/Python","slug":"matrix-has-no-element","errorCode":null,"errorMessage":"Matrix has no element","messagePattern":"Matrix has no element","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"linear_algebra/src/lib.py","lineNumber":412,"sourceCode":"    def cofactor(self, x: int, y: int) -> float:\n        \"\"\"\n        returns the cofactor (signed minor) along (x, y)\n        \"\"\"\n        if self.__height != self.__width:\n            raise Exception(\"Matrix is not square\")\n        if 0 <= x < self.__height and 0 <= y < self.__width:\n            return (-1) ** (x + y) * self.minor(x, y)\n        else:\n            raise Exception(\"Indices out of bounds\")\n\n    def determinant(self) -> float:\n        \"\"\"\n        returns the determinant of an nxn matrix using Laplace expansion\n        \"\"\"\n        if self.__height != self.__width:\n            raise Exception(\"Matrix is not square\")\n        if self.__height < 1:\n            raise Exception(\"Matrix has no element\")\n        elif self.__height == 1:\n            return self.__matrix[0][0]\n        elif self.__height == 2:\n            return (\n                self.__matrix[0][0] * self.__matrix[1][1]\n                - self.__matrix[0][1] * self.__matrix[1][0]\n            )\n        else:\n            cofactor_prods = [\n                self.__matrix[0][y] * self.cofactor(0, y) for y in range(self.__width)\n            ]\n            return sum(cofactor_prods)\n\n\ndef square_zero_matrix(n: int) -> Matrix:\n    \"\"\"\n    returns a square zero-matrix of dimension NxN\n    \"\"\"","sourceCodeStart":394,"sourceCodeEnd":430,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_algebra/src/lib.py#L394-L430","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ndet = Matrix([], 0, 0).determinant()  # Exception: Matrix has no element\n\n// after\ndet = 1.0 if m.height() == 0 else m.determinant()  # empty product convention","handlingStrategy":"validation","validationCode":"if m.height() == 0 or m.width() == 0:\n    det = 1.0  # empty-product convention, or raise ValueError with context\nelse:\n    det = m.determinant()","typeGuard":"from linear_algebra.src.lib import Matrix\n\ndef is_nonempty_matrix(obj) -> bool:\n    return isinstance(obj, Matrix) and obj.height() > 0 and obj.width() > 0","tryCatchPattern":"try:\n    det = m.determinant()\nexcept Exception as e:\n    if \"no element\" in str(e):\n        raise ValueError(\"determinant of an empty matrix; check upstream data loading\") from e\n    raise","preventionTips":["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."],"tags":["linear-algebra","matrix","determinant","empty-input","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}