{"record":{"id":"851cafce2e6ac78f","repo":"TheAlgorithms/Python","slug":"matrix-is-not-square-851caf","errorCode":null,"errorMessage":"Matrix is not square","messagePattern":"Matrix is not square","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"linear_algebra/src/lib.py","lineNumber":388,"sourceCode":"            return self.__matrix[x][y]\n        else:\n            raise Exception(\"change_component: indices out of bounds\")\n\n    def change_component(self, x: int, y: int, value: float) -> None:\n        \"\"\"\n        changes the x-y component of this matrix\n        \"\"\"\n        if 0 <= x < self.__height and 0 <= y < self.__width:\n            self.__matrix[x][y] = value\n        else:\n            raise Exception(\"change_component: indices out of bounds\")\n\n    def minor(self, x: int, y: int) -> float:\n        \"\"\"\n        returns the minor along (x, y)\n        \"\"\"\n        if self.__height != self.__width:\n            raise Exception(\"Matrix is not square\")\n        minor = self.__matrix[:x] + self.__matrix[x + 1 :]\n        for i in range(len(minor)):\n            minor[i] = minor[i][:y] + minor[i][y + 1 :]\n        return Matrix(minor, self.__width - 1, self.__height - 1).determinant()\n\n    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        \"\"\"","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_algebra/src/lib.py#L370-L406","documentation":"Raised by Matrix.minor(x, y) in linear_algebra/src/lib.py:388 when the matrix is not square (height != width). A minor is the determinant of the submatrix obtained by deleting row x and column y, and the code immediately builds that submatrix and calls .determinant() — which also requires squareness — so non-square input is rejected up front with a bare Exception.","triggerScenarios":"Calling minor() on any m x n Matrix with m != n, e.g. Matrix([[1, 2, 3], [4, 5, 6]], 3, 2).minor(0, 0). Typically hit while computing cofactors/determinants of rectangular data, or when the constructor's width/height arguments were swapped, making an intentionally square matrix rectangular.","commonSituations":"Feeding rectangular data tables into determinant-style computations, or dimension-argument mix-ups at Matrix construction (signature is (matrix, width, height)).","solutions":["Check m.height() == m.width() before calling minor()/cofactor()/determinant().","Verify the (width, height) constructor arguments actually describe the nested list — swapped arguments make square data look rectangular.","For rectangular matrices, minors are undefined; compute rank/SVD or crop to a square submatrix explicitly if that is what you meant.","Catch Exception narrowly at boundaries."],"exampleFix":"// before\nm = Matrix([[1, 2, 3], [4, 5, 6]], 3, 2)\nm.minor(0, 0)  # Exception: Matrix is not square\n\n// after\nassert m.height() == m.width(), f\"minor needs a square matrix, got {m.height()}x{m.width()}\"\nm.minor(0, 0)","handlingStrategy":"validation","validationCode":"if m.height() != m.width():\n    raise ValueError(f\"minor requires a square matrix, got {m.height()}x{m.width()}\")\nvalue = m.minor(x, y)","typeGuard":"from linear_algebra.src.lib import Matrix\n\ndef is_square_matrix(m) -> bool:\n    return isinstance(m, Matrix) and m.height() == m.width()","tryCatchPattern":"try:\n    value = m.minor(x, y)\nexcept Exception as e:\n    if \"not square\" in str(e):\n        raise ValueError(f\"cannot take minor of {m.height()}x{m.width()} matrix\") from e\n    raise","preventionTips":["Guard every minor/cofactor/determinant entry point with a squareness check — all three require it.","Confirm the Matrix(data, width, height) constructor arguments match the nested list; swapped args fake rectangularity.","Keep determinant-family operations on a dedicated square-matrix type/path so rectangular data cannot reach them.","Validate at load time that row count equals column count when squareness is an invariant of your data."],"tags":["linear-algebra","matrix","minor","square-matrix","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}