{"record":{"id":"11b98617aafdf3a2","repo":"TheAlgorithms/Java","slug":"max-iterations-must-be-positive","errorCode":null,"errorMessage":"Max iterations must be positive.","messagePattern":"Max iterations must be positive\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/ChebyshevIteration.java","lineNumber":111,"sourceCode":"            throw new IllegalArgumentException(\"Matrix A cannot be empty.\");\n        }\n        if (n != a[0].length) {\n            throw new IllegalArgumentException(\"Matrix A must be square.\");\n        }\n        if (n != b.length) {\n            throw new IllegalArgumentException(\"Matrix A and vector b dimensions do not match.\");\n        }\n        if (n != x0.length) {\n            throw new IllegalArgumentException(\"Matrix A and vector x0 dimensions do not match.\");\n        }\n        if (minEigenvalue <= 0) {\n            throw new IllegalArgumentException(\"Smallest eigenvalue must be positive (matrix must be positive-definite).\");\n        }\n        if (maxEigenvalue <= minEigenvalue) {\n            throw new IllegalArgumentException(\"Max eigenvalue must be strictly greater than min eigenvalue.\");\n        }\n        if (maxIterations <= 0) {\n            throw new IllegalArgumentException(\"Max iterations must be positive.\");\n        }\n        if (tolerance <= 0) {\n            throw new IllegalArgumentException(\"Tolerance must be positive.\");\n        }\n    }\n\n    // --- Vector/Matrix Helper Methods ---\n    /**\n     * Computes the product of a matrix A and a vector v (Av).\n     */\n    private static double[] matrixVectorMultiply(double[][] a, double[] v) {\n        int n = a.length;\n        double[] result = new double[n];\n        for (int i = 0; i < n; i++) {\n            double sum = 0;\n            for (int j = 0; j < n; j++) {\n                sum += a[i][j] * v[j];\n            }","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/ChebyshevIteration.java#L93-L129","documentation":"Thrown by ChebyshevIteration.validateInputs when maxIterations <= 0. The solver loops for (int k = 0; k < maxIterations; k++); a non-positive maxIterations means the loop body never executes and the solver would return x0 unchanged without any iteration. This guard ensures at least one iteration is attempted.","triggerScenarios":"Calling solve with maxIterations = 0 or a negative value. For example: solve(A, b, x0, 1, 5, 0, 1e-6) or solve(A, b, x0, 1, 5, -10, 1e-6). Also triggered when maxIterations is computed from a config value that defaulted to 0.","commonSituations":"A configuration parameter for iteration count that was not set (defaults to 0). A computed iteration budget based on problem size that collapsed to zero for small inputs. Passing a convergence flag or boolean as maxIterations due to a parameter ordering mistake.","solutions":["Ensure maxIterations is a positive integer (e.g., 100, 1000) appropriate for the problem.","Validate maxIterations > 0 before calling solve, using a sensible default if it comes from config.","Check the parameter order to avoid passing tolerance or an eigenvalue in the maxIterations position."],"exampleFix":"// before\nChebyshevIteration.solve(A, b, x0, 1, 5, 0, 1e-6);\n// throws 'Max iterations must be positive.'\n\n// after (set a reasonable iteration budget)\nint maxIterations = Math.max(100, n * 10); // scale with problem size\nChebyshevIteration.solve(A, b, x0, minEig, maxEig, maxIterations, 1e-6);","handlingStrategy":"validation","validationCode":"// Validate maxIterations before calling solve\nif (maxIterations <= 0) {\n    maxIterations = Math.max(100, a.length * 10); // sensible default\n}\ndouble[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIterations, tol);","typeGuard":"static boolean isValidIterationCount(int n) {\n    return n > 0;\n}","tryCatchPattern":null,"preventionTips":["Set a positive default for maxIterations in config (e.g., 100 or 1000).","Scale maxIterations with problem size: n * 10 is a reasonable heuristic.","Check parameter order carefully — maxIterations is the 6th positional argument, easily confused with tolerance or eigenvalues."],"tags":["linear-algebra","input-validation","iterations","chebyshev-iteration","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}