{"record":{"id":"9d3206339908b168","repo":"TheAlgorithms/Python","slug":"unable-to-multiply-these-matrices-please-check-th","errorCode":null,"errorMessage":"Unable to multiply these matrices, please check the dimensions.\nMatrix A: {matrix1}\nMatrix B: {matrix2}","messagePattern":"Unable to multiply these matrices, please check the dimensions\\.\nMatrix A: (.+?)\nMatrix B: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"divide_and_conquer/strassen_matrix_multiplication.py","lineNumber":120,"sourceCode":"    for i in range(len(bot_right)):\n        new_matrix.append(bot_left[i] + bot_right[i])\n    return new_matrix\n\n\ndef strassen(matrix1: list, matrix2: list) -> list:\n    \"\"\"\n    >>> strassen([[2,1,3],[3,4,6],[1,4,2],[7,6,7]], [[4,2,3,4],[2,1,1,1],[8,6,4,2]])\n    [[34, 23, 19, 15], [68, 46, 37, 28], [28, 18, 15, 12], [96, 62, 55, 48]]\n    >>> strassen([[3,7,5,6,9],[1,5,3,7,8],[1,4,4,5,7]], [[2,4],[5,2],[1,7],[5,5],[7,8]])\n    [[139, 163], [121, 134], [100, 121]]\n    \"\"\"\n    if matrix_dimensions(matrix1)[1] != matrix_dimensions(matrix2)[0]:\n        msg = (\n            \"Unable to multiply these matrices, please check the dimensions.\\n\"\n            f\"Matrix A: {matrix1}\\n\"\n            f\"Matrix B: {matrix2}\"\n        )\n        raise Exception(msg)\n    dimension1 = matrix_dimensions(matrix1)\n    dimension2 = matrix_dimensions(matrix2)\n\n    if dimension1[0] == dimension1[1] and dimension2[0] == dimension2[1]:\n        return [matrix1, matrix2]\n\n    maximum = max(*dimension1, *dimension2)\n    maxim = int(math.pow(2, math.ceil(math.log2(maximum))))\n    new_matrix1 = matrix1\n    new_matrix2 = matrix2\n\n    # Adding zeros to the matrices to convert them both into square matrices of equal\n    # dimensions that are a power of 2\n    for i in range(maxim):\n        if i < dimension1[0]:\n            for _ in range(dimension1[1], maxim):\n                new_matrix1[i].append(0)\n        else:","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/divide_and_conquer/strassen_matrix_multiplication.py#L102-L138","documentation":"Raised by strassen() when the inner dimension mismatches: the column count of matrix1 (matrix_dimensions(matrix1)[1]) must equal the row count of matrix2 (matrix_dimensions(matrix2)[0]). This is the standard matrix-multiplication conformability rule; the error message embeds both matrices for debugging. It is raised as a bare Exception before any padding or recursion starts.","triggerScenarios":"strassen([[1,2],[3,4]], [[1,2,3]]) — a 2x2 times a 1x3 (2 columns vs 1 row); any A (mxn) times B (pxq) with n != p. Square matrices of equal size, and any n == p pair (including rectangulars like 4x3 * 3x4), pass this check.","commonSituations":"Transposed second operand (B.T vs B); loading matrices from files with wrong orientation; assuming the function requires square equal matrices (it does not — it pads automatically) and pre-transposing data incorrectly.","solutions":["Fix operand order/orientation so columns(A) == rows(B), e.g. pass b transposed if you computed b.T by mistake.","Add a precondition check: if len(a[0]) != len(b): raise/transpose before calling.","Inspect the printed matrices in the message to see which operand has the wrong shape."],"exampleFix":"# before\nproduct = strassen(a, b)  # a: 2x2, b: 3x2 -> Exception\n\n# after\nif len(a[0]) != len(b):\n    raise ValueError(f'incompatible: {len(a)}x{len(a[0])} @ {len(b)}x{len(b[0])}')\nproduct = strassen(a, b)","handlingStrategy":"validation","validationCode":"def multipliable(a, b) -> bool:\n    return all(len(row) == len(a[0]) for row in a) and len(a[0]) == len(b)\n\nif multipliable(matrix1, matrix2):\n    product = strassen(matrix1, matrix2)","typeGuard":"def matrices_conformable(a: list, b: list) -> bool:\n    return bool(a) and bool(b) and len(a[0]) == len(b)","tryCatchPattern":"try:\n    product = strassen(matrix1, matrix2)\nexcept Exception as exc:\n    if 'check the dimensions' in str(exc):\n        raise ValueError(f'cannot multiply {len(matrix1)}x{len(matrix1[0])} by {len(matrix2)}x...') from exc\n    raise","preventionTips":["Always verify columns(A) == rows(B) before any multiplication call.","Watch for transposition mistakes when loading matrices from files or frameworks.","Standardize matrices as list-of-rows with equal row lengths at ingestion."],"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"}