TheAlgorithms/Python · error · Exception
Unable to multiply these matrices, please check the dimensio
Error message
Unable to multiply these matrices, please check the dimensions.
Matrix A: {matrix1}
Matrix B: {matrix2} What it means
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.
Source
Thrown at divide_and_conquer/strassen_matrix_multiplication.py:120
for i in range(len(bot_right)):
new_matrix.append(bot_left[i] + bot_right[i])
return new_matrix
def strassen(matrix1: list, matrix2: list) -> list:
"""
>>> 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]])
[[34, 23, 19, 15], [68, 46, 37, 28], [28, 18, 15, 12], [96, 62, 55, 48]]
>>> 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]])
[[139, 163], [121, 134], [100, 121]]
"""
if matrix_dimensions(matrix1)[1] != matrix_dimensions(matrix2)[0]:
msg = (
"Unable to multiply these matrices, please check the dimensions.\n"
f"Matrix A: {matrix1}\n"
f"Matrix B: {matrix2}"
)
raise Exception(msg)
dimension1 = matrix_dimensions(matrix1)
dimension2 = matrix_dimensions(matrix2)
if dimension1[0] == dimension1[1] and dimension2[0] == dimension2[1]:
return [matrix1, matrix2]
maximum = max(*dimension1, *dimension2)
maxim = int(math.pow(2, math.ceil(math.log2(maximum))))
new_matrix1 = matrix1
new_matrix2 = matrix2
# Adding zeros to the matrices to convert them both into square matrices of equal
# dimensions that are a power of 2
for i in range(maxim):
if i < dimension1[0]:
for _ in range(dimension1[1], maxim):
new_matrix1[i].append(0)
else:View on GitHub (pinned to f5988cc097)
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.
Example fix
# before
product = strassen(a, b) # a: 2x2, b: 3x2 -> Exception
# after
if len(a[0]) != len(b):
raise ValueError(f'incompatible: {len(a)}x{len(a[0])} @ {len(b)}x{len(b[0])}')
product = strassen(a, b) Defensive patterns
Strategy: validation
Validate before calling
def multipliable(a, b) -> bool:
return all(len(row) == len(a[0]) for row in a) and len(a[0]) == len(b)
if multipliable(matrix1, matrix2):
product = strassen(matrix1, matrix2) Type guard
def matrices_conformable(a: list, b: list) -> bool:
return bool(a) and bool(b) and len(a[0]) == len(b) Try / catch
try:
product = strassen(matrix1, matrix2)
except Exception as exc:
if 'check the dimensions' in str(exc):
raise ValueError(f'cannot multiply {len(matrix1)}x{len(matrix1[0])} by {len(matrix2)}x...') from exc
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Matrices are not 2x2
- Odd matrices are not supported!
- Expecting a list of points but got {points}
- the value of both inputs must be positive
- both inputs must be positive integers
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/9d3206339908b168.
Report an issue: GitHub.