TheAlgorithms/Python · error · ValueError
Iterations must be at least 1
Error message
Iterations must be at least 1
What it means
Thrown by jacobi_iteration_method() when iterations <= 0. The routine performs exactly the requested number of fixed-point sweeps (it does not test convergence), so zero or negative iterations would return the initial guess unchanged and create the illusion of a solved system; the guard makes that a hard error.
Source
Thrown at linear_algebra/jacobi_iteration_method.py:109
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 = 0
for col in range(cols):
if col == row:
denom = table[row][col]View on GitHub (pinned to f5988cc097)
Solutions
- Pass a positive iteration count (typically 100+ depending on needed accuracy).
- Short-circuit at the caller: if iterations <= 0, skip the call or raise your own 'converged/invalid' error with context.
- Do not use 0 as an 'auto' sentinel — pick an explicit large count.
Example fix
# before
jacobi_iteration_method(A, b, x0, remaining_iters) # may be 0
# after
if remaining_iters <= 0:
raise ValueError(f"non-positive iteration budget: {remaining_iters}")
jacobi_iteration_method(A, b, x0, remaining_iters) Defensive patterns
Strategy: validation
Validate before calling
if iterations <= 0:
raise ValueError(f"iteration budget must be positive, got {iterations}") Type guard
def is_positive_iterations(n: int) -> bool:
return isinstance(n, int) and n > 0 Try / catch
try:
x = jacobi_iteration_method(A, b, x0, iters)
except ValueError as e:
if "Iterations" in str(e):
x = jacobi_iteration_method(A, b, x0, max(iters, 100))
else:
raise Prevention
- Pass an explicit positive count (100+ for typical accuracy).
- Do not use 0 as an 'auto/converge' sentinel.
- Guard computed iteration budgets (remaining = target - used).
When it happens
Trigger: Calling jacobi_iteration_method(A, b, init_val, 0) or with a negative iteration count; also passing iterations computed as (target - current) that hit zero when convergence was already reached.
Common situations: Loop drivers that compute remaining iterations and hit 0; config defaults of 0 meaning 'auto'; refactoring from tolerance-based solvers where 0 meant 'iterate to convergence'.
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
- Number of initial values must be equal to number of rows in
- Coefficient matrix is not strictly diagonally dominant
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/3f41e6c84228b2eb.
Report an issue: GitHub.