TheAlgorithms/Python · error · Exception

Odd matrices are not supported!

Error message

Odd matrices are not supported!

What it means

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.

Source

Thrown at divide_and_conquer/strassen_matrix_multiplication.py:50

def split_matrix(a: list) -> tuple[list, list, list, list]:
    """
    Given an even length matrix, returns the top_left, top_right, bot_left, bot_right
    quadrant.

    >>> split_matrix([[4,3,2,4],[2,3,1,1],[6,5,4,3],[8,4,1,6]])
    ([[4, 3], [2, 3]], [[2, 4], [1, 1]], [[6, 5], [8, 4]], [[4, 3], [1, 6]])
    >>> split_matrix([
    ...     [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],
    ...     [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]
    ... ])  # doctest: +NORMALIZE_WHITESPACE
    ([[4, 3, 2, 4], [2, 3, 1, 1], [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4],
      [2, 3, 1, 1], [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4], [2, 3, 1, 1],
      [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4], [2, 3, 1, 1], [6, 5, 4, 3],
      [8, 4, 1, 6]])
    """
    if len(a) % 2 != 0 or len(a[0]) % 2 != 0:
        raise Exception("Odd matrices are not supported!")

    matrix_length = len(a)
    mid = matrix_length // 2

    top_right = [[a[i][j] for j in range(mid, matrix_length)] for i in range(mid)]
    bot_right = [
        [a[i][j] for j in range(mid, matrix_length)] for i in range(mid, matrix_length)
    ]

    top_left = [[a[i][j] for j in range(mid)] for i in range(mid)]
    bot_left = [[a[i][j] for j in range(mid)] for i in range(mid, matrix_length)]

    return top_left, top_right, bot_left, bot_right


def matrix_dimensions(matrix: list) -> tuple[int, int]:
    return len(matrix), len(matrix[0])

View on GitHub (pinned to f5988cc097)

Solutions

  1. Call strassen(a, b) instead of split_matrix; it pads to a power-of-two square automatically.
  2. If calling split_matrix yourself, pad odd dimensions with a zero row/column first so len(matrix) and len(matrix[0]) are even.
  3. Use int(2 ** math.ceil(math.log2(n))) to compute the padded size, mirroring strassen's logic.

Example fix

# before
a11, a12, a21, a22 = split_matrix(a)  # a is 3x3 -> Exception

# after
size = int(2 ** math.ceil(math.log2(max(len(a), len(a[0]), 1))))
padded = [row + [0] * (size - len(row)) for row in a] + [[0] * size for _ in range(size - len(a))]
a11, a12, a21, a22 = split_matrix(padded)
Defensive patterns

Strategy: validation

Validate before calling

def is_even_square_split(m) -> bool:
    return len(m) % 2 == 0 and len(m[0]) % 2 == 0

if is_even_square_split(a):
    quadrants = split_matrix(a)

Type guard

def has_even_dimensions(matrix: list) -> bool:
    return bool(matrix) and len(matrix) % 2 == 0 and len(matrix[0]) % 2 == 0

Try / catch

try:
    quadrants = split_matrix(a)
except Exception as exc:
    if 'Odd matrices' in str(exc):
        raise ValueError('pad matrix to even dimensions first') from exc
    raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/08afdf234f2c6fd5. Report an issue: GitHub.