TheAlgorithms/Java · error · IllegalArgumentException

Max iterations must be positive.

Error message

Max iterations must be positive.

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/maths/ChebyshevIteration.java:111

            throw new IllegalArgumentException("Matrix A cannot be empty.");
        }
        if (n != a[0].length) {
            throw new IllegalArgumentException("Matrix A must be square.");
        }
        if (n != b.length) {
            throw new IllegalArgumentException("Matrix A and vector b dimensions do not match.");
        }
        if (n != x0.length) {
            throw new IllegalArgumentException("Matrix A and vector x0 dimensions do not match.");
        }
        if (minEigenvalue <= 0) {
            throw new IllegalArgumentException("Smallest eigenvalue must be positive (matrix must be positive-definite).");
        }
        if (maxEigenvalue <= minEigenvalue) {
            throw new IllegalArgumentException("Max eigenvalue must be strictly greater than min eigenvalue.");
        }
        if (maxIterations <= 0) {
            throw new IllegalArgumentException("Max iterations must be positive.");
        }
        if (tolerance <= 0) {
            throw new IllegalArgumentException("Tolerance must be positive.");
        }
    }

    // --- Vector/Matrix Helper Methods ---
    /**
     * Computes the product of a matrix A and a vector v (Av).
     */
    private static double[] matrixVectorMultiply(double[][] a, double[] v) {
        int n = a.length;
        double[] result = new double[n];
        for (int i = 0; i < n; i++) {
            double sum = 0;
            for (int j = 0; j < n; j++) {
                sum += a[i][j] * v[j];
            }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure maxIterations is a positive integer (e.g., 100, 1000) appropriate for the problem.
  2. Validate maxIterations > 0 before calling solve, using a sensible default if it comes from config.
  3. Check the parameter order to avoid passing tolerance or an eigenvalue in the maxIterations position.

Example fix

// before
ChebyshevIteration.solve(A, b, x0, 1, 5, 0, 1e-6);
// throws 'Max iterations must be positive.'

// after (set a reasonable iteration budget)
int maxIterations = Math.max(100, n * 10); // scale with problem size
ChebyshevIteration.solve(A, b, x0, minEig, maxEig, maxIterations, 1e-6);
Defensive patterns

Strategy: validation

Validate before calling

// Validate maxIterations before calling solve
if (maxIterations <= 0) {
    maxIterations = Math.max(100, a.length * 10); // sensible default
}
double[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIterations, tol);

Type guard

static boolean isValidIterationCount(int n) {
    return n > 0;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/11b98617aafdf3a2. Report an issue: GitHub.