TheAlgorithms/Java · error · IllegalArgumentException
Smallest eigenvalue must be positive (matrix must be positiv
Error message
Smallest eigenvalue must be positive (matrix must be positive-definite).
What it means
Thrown by ChebyshevIteration.validateInputs when minEigenvalue <= 0. The Chebyshev iteration method requires the matrix A to be symmetric positive-definite (SPD), which by definition has all eigenvalues strictly positive. The smallest eigenvalue (m(A)) is used to compute the iteration parameters d and c; a non-positive minEigenvalue would make these parameters invalid and prevent convergence.
Source
Thrown at src/main/java/com/thealgorithms/maths/ChebyshevIteration.java:105
/**
* Validates the inputs for the Chebyshev solver.
*/
private static void validateInputs(double[][] a, double[] b, double[] x0, double minEigenvalue, double maxEigenvalue, int maxIterations, double tolerance) {
int n = a.length;
if (n == 0) {
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;View on GitHub (pinned to fdfb9a395b)
Solutions
- Verify that matrix A is symmetric positive-definite (check eigenvalues with a library like EJML or Apache Commons Math).
- If A is not SPD, use a different solver (e.g., GMRES, BiCGSTAB) that does not require positive-definiteness.
- Ensure minEigenvalue is the smallest (most negative-closest-to-zero positive) eigenvalue — recompute it if the estimate is wrong.
- Check argument order: minEigenvalue must come before maxEigenvalue in the call.
Example fix
// before
ChebyshevIteration.solve(A, b, x0, 0.0, 5.0, 100, 1e-6);
// throws 'Smallest eigenvalue must be positive...'
// after (compute actual eigenvalues and verify SPD)
// Using a linear algebra library to find eigenvalues
double minEig = computeMinEigenvalue(A); // must be > 0 for SPD
double maxEig = computeMaxEigenvalue(A);
if (minEig > 0) {
double[] x = ChebyshevIteration.solve(A, b, x0, minEig, maxEig, maxIter, tol);
} else {
// use a solver for non-SPD systems
} Defensive patterns
Strategy: validation
Validate before calling
// Validate eigenvalues before calling solve
if (minEigenvalue <= 0) {
throw new IllegalArgumentException("minEigenvalue must be positive (A must be SPD)");
}
double[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);
// If A may not be SPD, verify with an eigenvalue computation:
// double[] eigs = computeEigenvalues(a);
// if (eigs[0] <= 0) use a different solver (GMRES, BiCGSTAB) Type guard
static boolean isSPD(double[][] a) {
// Requires an external eigenvalue computation
// Return true only if A is symmetric and all eigenvalues > 0
return isSquare(a) && isSymmetric(a) && minEigenvalue(a) > 0;
} Prevention
- Verify A is symmetric positive-definite before choosing Chebyshev iteration.
- Use a linear algebra library (EJML, Apache Commons Math) to compute eigenvalue bounds.
- If A is indefinite or non-symmetric, switch to GMRES, BiCGSTAB, or a direct solver.
- Double-check that minEigenvalue is the smallest algebraic eigenvalue, not the smallest magnitude.
When it happens
Trigger: Calling solve with minEigenvalue = 0, a negative minEigenvalue, or passing the wrong eigenvalue (e.g., swapping sign). For example: solve(A, b, x0, 0, 5, 100, 1e-6) or solve(A, b, x0, -1.5, 3, 100, 1e-6).
Common situations: Using a matrix A that is not positive-definite (e.g., indefinite or negative-definite), where the smallest eigenvalue is genuinely non-positive. Passing an eigenvalue estimate that was computed incorrectly (e.g., using the smallest-magnitude eigenvalue instead of the smallest algebraic eigenvalue). Swapping minEigenvalue and maxEigenvalue arguments.
Related errors
- Max eigenvalue must be strictly greater than min eigenvalue.
- Matrix A cannot be empty.
- Matrix A must be square.
- Matrix A and vector b dimensions do not match.
- Matrix A and vector x0 dimensions do not match.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/7d03c15f2e30b1be.
Report an issue: GitHub.