{"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":"Raised in Matrix.__add__ when the right-hand matrix matches neither the same shape nor the broadcastable row-vector (1 x cols) or column-vector (rows x 1) forms. Element-wise addition is undefined for these incompatible shapes, and Python has no silent broadcasting fallback in this hand-rolled implementation.","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":["Reshape or transpose one operand so both matrices share the same shape","Wrap the addition in a shape check and fall back to explicit broadcasting when a 1xN or Nx1 matrix is intended","Fix upstream math that produced an unexpectedly shaped matrix (e.g. a missing transpose)"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}