{"record":{"id":"52cfceba490f9c66","repo":"TheAlgorithms/Python","slug":"matrices-are-not-2x2","errorCode":null,"errorMessage":"Matrices are not 2x2","messagePattern":"Matrices are not 2x2","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"divide_and_conquer/strassen_matrix_multiplication.py","lineNumber":11,"sourceCode":"from __future__ import annotations\n\nimport math\n\n\ndef default_matrix_multiplication(a: list, b: list) -> list:\n    \"\"\"\n    Multiplication only for 2x2 matrices\n    \"\"\"\n    if len(a) != 2 or len(a[0]) != 2 or len(b) != 2 or len(b[0]) != 2:\n        raise Exception(\"Matrices are not 2x2\")\n    new_matrix = [\n        [a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]],\n        [a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]],\n    ]\n    return new_matrix\n\n\ndef matrix_addition(matrix_a: list, matrix_b: list):\n    return [\n        [matrix_a[row][col] + matrix_b[row][col] for col in range(len(matrix_a[row]))]\n        for row in range(len(matrix_a))\n    ]\n\n\ndef matrix_subtraction(matrix_a: list, matrix_b: list):\n    return [\n        [matrix_a[row][col] - matrix_b[row][col] for col in range(len(matrix_a[row]))]\n        for row in range(len(matrix_a))","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/divide_and_conquer/strassen_matrix_multiplication.py#L1-L29","documentation":"Raised by default_matrix_multiplication in strassen_matrix_multiplication.py, a helper that only multiplies exactly 2x2 matrices. It checks len(a) == 2, len(a[0]) == 2 and the same for b, and raises a bare Exception otherwise. This helper is the recursion base case for Strassen's algorithm; only your own code should hit it if you call it directly with larger or ragged matrices.","triggerScenarios":"Calling default_matrix_multiplication with 1x1, 3x3, NxM matrices, or 2-row matrices whose rows are not length 2 (ragged input such as [[1,2,3],[4]]). The public strassen() entry point pads dimensions itself and does not raise this.","commonSituations":"Reusing this private helper as a general matrix multiplier; passing a matrix of floats/rows built by parsing CSV where row lengths differ; copying the base-case call into custom recursion that forgets to split to 2x2.","solutions":["Use the public strassen(matrix1, matrix2) function instead, which handles arbitrary rectangular dimensions via zero-padding.","If you must call this helper, first split/pad matrices to exactly 2x2 (e.g. via split_matrix or manual slicing).","Validate row lengths before the call: assert len(m) == 2 and all(len(r) == 2 for r in m)."],"exampleFix":"# before\nproduct = default_matrix_multiplication(a, b)  # a is 3x3 -> Exception\n\n# after\nfrom divide_and_conquer.strassen_matrix_multiplication import strassen\nproduct = strassen(a, b)","handlingStrategy":"validation","validationCode":"def is_2x2(m) -> bool:\n    return (\n        isinstance(m, list) and len(m) == 2\n        and all(isinstance(r, list) and len(r) == 2 for r in m)\n    )\n\nif is_2x2(a) and is_2x2(b):\n    product = default_matrix_multiplication(a, b)","typeGuard":"from typing import List, Union\nNumber = Union[int, float]\n\ndef is_matrix_2x2(m: object) -> bool:\n    return isinstance(m, list) and len(m) == 2 and all(isinstance(m[i], list) and len(m[i]) == 2 for i in range(2))","tryCatchPattern":"try:\n    product = default_matrix_multiplication(a, b)\nexcept Exception as exc:\n    if 'Matrices are not 2x2' in str(exc):\n        raise ValueError('reshape inputs to 2x2 or use strassen()') from exc\n    raise","preventionTips":["Prefer public strassen() over private base-case helpers.","Assert shape preconditions when reusing low-level numeric helpers.","Validate parsed matrices for rectangularity (equal row lengths) at load time."],"tags":["python","input-validation","matrix","divide-and-conquer"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}