{"record":{"id":"830fc7cdd377680d","repo":"rohitg00/ai-engineering-from-scratch","slug":"matrix-is-singular-no-inverse-exists","errorCode":null,"errorMessage":"Matrix is singular, no inverse exists","messagePattern":"Matrix is singular, no inverse exists","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/01-math-foundations/02-vectors-matrices-operations/code/matrices.py","lineNumber":140,"sourceCode":"        if self.shape == (1, 1):\n            return self.data[0][0]\n        if self.shape == (2, 2):\n            return self.data[0][0] * self.data[1][1] - self.data[0][1] * self.data[1][0]\n        det = 0\n        for j in range(self.cols):\n            minor = Matrix([\n                [self.data[i][k] for k in range(self.cols) if k != j]\n                for i in range(1, self.rows)\n            ])\n            det += ((-1) ** j) * self.data[0][j] * minor.determinant()\n        return det\n\n    def inverse_2x2(self):\n        if self.shape != (2, 2):\n            raise ValueError(\"This method only works for 2x2 matrices\")\n        det = self.determinant()\n        if abs(det) < 1e-10:\n            raise ValueError(\"Matrix is singular, no inverse exists\")\n        return Matrix([\n            [self.data[1][1] / det, -self.data[0][1] / det],\n            [-self.data[1][0] / det, self.data[0][0] / det]\n        ])\n\n    @staticmethod\n    def identity(n):\n        return Matrix([\n            [1 if i == j else 0 for j in range(n)]\n            for i in range(n)\n        ])\n\n    @staticmethod\n    def zeros(rows, cols):\n        return Matrix([[0] * cols for _ in range(rows)])\n\n    @staticmethod\n    def random(rows, cols, low=-1.0, high=1.0):","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/01-math-foundations/02-vectors-matrices-operations/code/matrices.py#L122-L158","documentation":"Error \"Matrix is singular, no inverse exists\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/01-math-foundations/02-vectors-matrices-operations/code/matrices.py:140 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}