{"record":{"id":"3f41e6c84228b2eb","repo":"TheAlgorithms/Python","slug":"iterations-must-be-at-least-1","errorCode":null,"errorMessage":"Iterations must be at least 1","messagePattern":"Iterations must be at least 1","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"linear_algebra/jacobi_iteration_method.py","lineNumber":109,"sourceCode":"        msg = f\"Constant matrix must be nx1 but received {rows2}x{cols2}\"\n        raise ValueError(msg)\n\n    if rows1 != rows2:\n        msg = (\n            \"Coefficient and constant matrices dimensions must be nxn and nx1 but \"\n            f\"received {rows1}x{cols1} and {rows2}x{cols2}\"\n        )\n        raise ValueError(msg)\n\n    if len(init_val) != rows1:\n        msg = (\n            \"Number of initial values must be equal to number of rows in coefficient \"\n            f\"matrix but received {len(init_val)} and {rows1}\"\n        )\n        raise ValueError(msg)\n\n    if iterations <= 0:\n        raise ValueError(\"Iterations must be at least 1\")\n\n    table: NDArray[float64] = np.concatenate(\n        (coefficient_matrix, constant_matrix), axis=1\n    )\n\n    rows, _cols = table.shape\n\n    strictly_diagonally_dominant(table)\n\n    \"\"\"\n    # Iterates the whole matrix for given number of times\n    for _ in range(iterations):\n        new_val = []\n        for row in range(rows):\n            temp = 0\n            for col in range(cols):\n                if col == row:\n                    denom = table[row][col]","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_algebra/jacobi_iteration_method.py#L91-L127","documentation":"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.","triggerScenarios":"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.","commonSituations":"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'.","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."],"exampleFix":"# before\njacobi_iteration_method(A, b, x0, remaining_iters)  # may be 0\n\n# after\nif remaining_iters <= 0:\n    raise ValueError(f\"non-positive iteration budget: {remaining_iters}\")\njacobi_iteration_method(A, b, x0, remaining_iters)","handlingStrategy":"validation","validationCode":"if iterations <= 0:\n    raise ValueError(f\"iteration budget must be positive, got {iterations}\")","typeGuard":"def is_positive_iterations(n: int) -> bool:\n    return isinstance(n, int) and n > 0","tryCatchPattern":"try:\n    x = jacobi_iteration_method(A, b, x0, iters)\nexcept ValueError as e:\n    if \"Iterations\" in str(e):\n        x = jacobi_iteration_method(A, b, x0, max(iters, 100))\n    else:\n        raise","preventionTips":["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)."],"tags":["linear-algebra","jacobi","validation","iterations"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}