TheAlgorithms/Python · error · ValueError
Number of initial values must be equal to number of rows in
Error message
Number of initial values must be equal to number of rows in coefficient matrix but received {len(init_val)} and {rows1} What it means
Thrown by jacobi_iteration_method() when len(init_val) != rows1 — the initial guess vector does not have one entry per unknown. The iteration indexes x_old[i] for every row i; a shorter guess raises IndexError and a longer one indicates a wrong system, so the function validates the count upfront.
Source
Thrown at linear_algebra/jacobi_iteration_method.py:106
raise ValueError(msg)
if cols2 != 1:
msg = f"Constant matrix must be nx1 but received {rows2}x{cols2}"
raise ValueError(msg)
if rows1 != rows2:
msg = (
"Coefficient and constant matrices dimensions must be nxn and nx1 but "
f"received {rows1}x{cols1} and {rows2}x{cols2}"
)
raise ValueError(msg)
if len(init_val) != rows1:
msg = (
"Number of initial values must be equal to number of rows in coefficient "
f"matrix but received {len(init_val)} and {rows1}"
)
raise ValueError(msg)
if iterations <= 0:
raise ValueError("Iterations must be at least 1")
table: NDArray[float64] = np.concatenate(
(coefficient_matrix, constant_matrix), axis=1
)
rows, _cols = table.shape
strictly_diagonally_dominant(table)
"""
# Iterates the whole matrix for given number of times
for _ in range(iterations):
new_val = []
for row in range(rows):
temp = 0View on GitHub (pinned to f5988cc097)
Solutions
- Always derive the guess from the matrix: init_val = np.zeros(coefficient_matrix.shape[0]).
- Build A, b, and init_val from the same n variable in one place.
- Prefer letting the function's own default/zero start be used rather than hand-building guesses.
Example fix
# before init_val = [0.0, 0.0] # but A is 3x3 # after init_val = np.zeros(coefficient_matrix.shape[0])
Defensive patterns
Strategy: validation
Validate before calling
x0 = np.zeros(A.shape[0]) # derive guess length from the matrix
Type guard
def is_matching_guess(A: np.ndarray, x0: np.ndarray) -> bool:
return A.ndim == 2 and x0.shape[0] == A.shape[0] Try / catch
try:
x = jacobi_iteration_method(A, b, x0, iters)
except ValueError as e:
if "initial values" in str(e):
x = jacobi_iteration_method(A, b, np.zeros(A.shape[0]), iters)
else:
raise Prevention
- Never hardcode guess lengths; use np.zeros(A.shape[0]).
- Derive A, b, and x0 from one n variable.
- Rebuild guesses when problem size changes.
When it happens
Trigger: Calling with init_val = [0, 0] for a 3x3 system, or passing a 1-D numpy array of a different length (e.g. built with np.zeros(n) where n was hardcoded or from a previous problem size).
Common situations: Reusing an initial guess across problems of different dimensionality; hardcoded np.zeros(3) copied from an example; n changed in the system builder but not in the guess construction.
Related errors
- Coefficient matrix dimensions must be nxn but received {rows
- Constant matrix must be nx1 but received {rows2}x{cols2}
- Coefficient and constant matrices dimensions must be nxn and
- Iterations must be at least 1
- Coefficient matrix is not strictly diagonally dominant
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/db472c4baba824e1.
Report an issue: GitHub.