{"record":{"id":"aa1573a450a56961","repo":"rohitg00/ai-engineering-from-scratch","slug":"cannot-add-shapes-self-shape-and-other-shape","errorCode":null,"errorMessage":"Cannot add shapes {self.shape} and {other.shape}","messagePattern":"Cannot add shapes (.+?) and (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/01-math-foundations/02-vectors-matrices-operations/code/matrices.py","lineNumber":72,"sourceCode":"\n    def __add__(self, other):\n        if isinstance(other, Matrix):\n            if other.shape == self.shape:\n                return Matrix([\n                    [self.data[i][j] + other.data[i][j] for j in range(self.cols)]\n                    for i in range(self.rows)\n                ])\n            if other.rows == 1 and other.cols == self.cols:\n                return Matrix([\n                    [self.data[i][j] + other.data[0][j] for j in range(self.cols)]\n                    for i in range(self.rows)\n                ])\n            if other.cols == 1 and other.rows == self.rows:\n                return Matrix([\n                    [self.data[i][j] + other.data[i][0] for j in range(self.cols)]\n                    for i in range(self.rows)\n                ])\n        raise ValueError(f\"Cannot add shapes {self.shape} and {other.shape}\")\n\n    def __sub__(self, other):\n        return Matrix([\n            [self.data[i][j] - other.data[i][j] for j in range(self.cols)]\n            for i in range(self.rows)\n        ])\n\n    def scalar_multiply(self, scalar):\n        return Matrix([\n            [self.data[i][j] * scalar for j in range(self.cols)]\n            for i in range(self.rows)\n        ])\n\n    def element_wise_multiply(self, other):\n        return Matrix([\n            [self.data[i][j] * other.data[i][j] for j in range(self.cols)]\n            for i in range(self.rows)\n        ])","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/01-math-foundations/02-vectors-matrices-operations/code/matrices.py#L54-L90","documentation":"Error \"Cannot add shapes {self.shape} and {other.shape}\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/01-math-foundations/02-vectors-matrices-operations/code/matrices.py:72 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"}