{"record":{"id":"7d03c15f2e30b1be","repo":"TheAlgorithms/Java","slug":"smallest-eigenvalue-must-be-positive-matrix-must","errorCode":null,"errorMessage":"Smallest eigenvalue must be positive (matrix must be positive-definite).","messagePattern":"Smallest eigenvalue must be positive \\(matrix must be positive-definite\\)\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/ChebyshevIteration.java","lineNumber":105,"sourceCode":"    /**\n     * Validates the inputs for the Chebyshev solver.\n     */\n    private static void validateInputs(double[][] a, double[] b, double[] x0, double minEigenvalue, double maxEigenvalue, int maxIterations, double tolerance) {\n        int n = a.length;\n        if (n == 0) {\n            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;","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/ChebyshevIteration.java#L87-L123","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nChebyshevIteration.solve(A, b, x0, 0.0, 5.0, 100, 1e-6);\n// throws 'Smallest eigenvalue must be positive...'\n\n// after (compute actual eigenvalues and verify SPD)\n// Using a linear algebra library to find eigenvalues\ndouble minEig = computeMinEigenvalue(A); // must be > 0 for SPD\ndouble maxEig = computeMaxEigenvalue(A);\nif (minEig > 0) {\n    double[] x = ChebyshevIteration.solve(A, b, x0, minEig, maxEig, maxIter, tol);\n} else {\n    // use a solver for non-SPD systems\n}","handlingStrategy":"validation","validationCode":"// Validate eigenvalues before calling solve\nif (minEigenvalue <= 0) {\n    throw new IllegalArgumentException(\"minEigenvalue must be positive (A must be SPD)\");\n}\ndouble[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);\n// If A may not be SPD, verify with an eigenvalue computation:\n// double[] eigs = computeEigenvalues(a);\n// if (eigs[0] <= 0) use a different solver (GMRES, BiCGSTAB)","typeGuard":"static boolean isSPD(double[][] a) {\n    // Requires an external eigenvalue computation\n    // Return true only if A is symmetric and all eigenvalues > 0\n    return isSquare(a) && isSymmetric(a) && minEigenvalue(a) > 0;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["linear-algebra","input-validation","eigenvalues","positive-definite","chebyshev-iteration","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}