{"record":{"id":"08afdf234f2c6fd5","repo":"TheAlgorithms/Python","slug":"odd-matrices-are-not-supported","errorCode":null,"errorMessage":"Odd matrices are not supported!","messagePattern":"Odd matrices are not supported!","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"divide_and_conquer/strassen_matrix_multiplication.py","lineNumber":50,"sourceCode":"\ndef split_matrix(a: list) -> tuple[list, list, list, list]:\n    \"\"\"\n    Given an even length matrix, returns the top_left, top_right, bot_left, bot_right\n    quadrant.\n\n    >>> split_matrix([[4,3,2,4],[2,3,1,1],[6,5,4,3],[8,4,1,6]])\n    ([[4, 3], [2, 3]], [[2, 4], [1, 1]], [[6, 5], [8, 4]], [[4, 3], [1, 6]])\n    >>> split_matrix([\n    ...     [4,3,2,4,4,3,2,4],[2,3,1,1,2,3,1,1],[6,5,4,3,6,5,4,3],[8,4,1,6,8,4,1,6],\n    ...     [4,3,2,4,4,3,2,4],[2,3,1,1,2,3,1,1],[6,5,4,3,6,5,4,3],[8,4,1,6,8,4,1,6]\n    ... ])  # doctest: +NORMALIZE_WHITESPACE\n    ([[4, 3, 2, 4], [2, 3, 1, 1], [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4],\n      [2, 3, 1, 1], [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4], [2, 3, 1, 1],\n      [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4], [2, 3, 1, 1], [6, 5, 4, 3],\n      [8, 4, 1, 6]])\n    \"\"\"\n    if len(a) % 2 != 0 or len(a[0]) % 2 != 0:\n        raise Exception(\"Odd matrices are not supported!\")\n\n    matrix_length = len(a)\n    mid = matrix_length // 2\n\n    top_right = [[a[i][j] for j in range(mid, matrix_length)] for i in range(mid)]\n    bot_right = [\n        [a[i][j] for j in range(mid, matrix_length)] for i in range(mid, matrix_length)\n    ]\n\n    top_left = [[a[i][j] for j in range(mid)] for i in range(mid)]\n    bot_left = [[a[i][j] for j in range(mid)] for i in range(mid, matrix_length)]\n\n    return top_left, top_right, bot_left, bot_right\n\n\ndef matrix_dimensions(matrix: list) -> tuple[int, int]:\n    return len(matrix), len(matrix[0])\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/divide_and_conquer/strassen_matrix_multiplication.py#L32-L68","documentation":"Raised by split_matrix when the input matrix has an odd number of rows (len(a) % 2 != 0) or its first row has odd length. Strassen's algorithm splits each matrix into four equal quadrants, which is impossible when a dimension is odd. The public strassen() function avoids this by zero-padding to the next power of two before splitting.","triggerScenarios":"Calling split_matrix directly with any NxN or NxM matrix where N or M is odd (e.g. 3x3, 5x4). Ragged matrices also surface here because only len(a[0]) is checked. Going through strassen() never triggers it.","commonSituations":"Bypassing the public API and driving split_matrix from custom recursion; hand-rolled padding that rounds down instead of up to a power of two; test fixtures with odd-sized matrices.","solutions":["Call strassen(a, b) instead of split_matrix; it pads to a power-of-two square automatically.","If calling split_matrix yourself, pad odd dimensions with a zero row/column first so len(matrix) and len(matrix[0]) are even.","Use int(2 ** math.ceil(math.log2(n))) to compute the padded size, mirroring strassen's logic."],"exampleFix":"# before\na11, a12, a21, a22 = split_matrix(a)  # a is 3x3 -> Exception\n\n# after\nsize = int(2 ** math.ceil(math.log2(max(len(a), len(a[0]), 1))))\npadded = [row + [0] * (size - len(row)) for row in a] + [[0] * size for _ in range(size - len(a))]\na11, a12, a21, a22 = split_matrix(padded)","handlingStrategy":"validation","validationCode":"def is_even_square_split(m) -> bool:\n    return len(m) % 2 == 0 and len(m[0]) % 2 == 0\n\nif is_even_square_split(a):\n    quadrants = split_matrix(a)","typeGuard":"def has_even_dimensions(matrix: list) -> bool:\n    return bool(matrix) and len(matrix) % 2 == 0 and len(matrix[0]) % 2 == 0","tryCatchPattern":"try:\n    quadrants = split_matrix(a)\nexcept Exception as exc:\n    if 'Odd matrices' in str(exc):\n        raise ValueError('pad matrix to even dimensions first') from exc\n    raise","preventionTips":["Use strassen() which pads to powers of two automatically.","When hand-rolling recursion, always pad before splitting, never after.","Compute padded sizes as int(2 ** math.ceil(math.log2(n))) to keep dimensions even."],"tags":["python","input-validation","matrix","divide-and-conquer"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}