{"record":{"id":"4b1c54ceb04f4bbe","repo":"TheAlgorithms/Python","slug":"subtraction-requires-matrices-of-the-same-order","errorCode":null,"errorMessage":"Subtraction requires matrices of the same order","messagePattern":"Subtraction requires matrices of the same order","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"matrix/matrix_class.py","lineNumber":310,"sourceCode":"    def __ne__(self, other: object) -> bool:\n        return not self == other\n\n    def __neg__(self) -> Matrix:\n        return self * -1\n\n    def __add__(self, other: Matrix) -> Matrix:\n        if self.order != other.order:\n            raise ValueError(\"Addition requires matrices of the same order\")\n        return Matrix(\n            [\n                [self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns)]\n                for i in range(self.num_rows)\n            ]\n        )\n\n    def __sub__(self, other: Matrix) -> Matrix:\n        if self.order != other.order:\n            raise ValueError(\"Subtraction requires matrices of the same order\")\n        return Matrix(\n            [\n                [self.rows[i][j] - other.rows[i][j] for j in range(self.num_columns)]\n                for i in range(self.num_rows)\n            ]\n        )\n\n    def __mul__(self, other: Matrix | float) -> Matrix:\n        if isinstance(other, (int, float)):\n            return Matrix(\n                [[int(element * other) for element in row] for row in self.rows]\n            )\n        elif isinstance(other, Matrix):\n            if self.num_columns != other.num_rows:\n                raise ValueError(\n                    \"The number of columns in the first matrix must \"\n                    \"be equal to the number of rows in the second\"\n                )","sourceCodeStart":292,"sourceCodeEnd":328,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/matrix/matrix_class.py#L292-L328","documentation":"Error \"Subtraction requires matrices of the same order\" thrown in TheAlgorithms/Python.","triggerScenarios":"Thrown at matrix/matrix_class.py:310 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Subtract only matrices with identical dimensions."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}