{"record":{"id":"05249ecb4800c6a9","repo":"TheAlgorithms/Java","slug":"matrix-a-cannot-be-empty","errorCode":null,"errorMessage":"Matrix A cannot be empty.","messagePattern":"Matrix A cannot be empty\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/ChebyshevIteration.java","lineNumber":93,"sourceCode":"\n            double[] xUpdate = scalarMultiply(alpha, p);\n            x = vectorAdd(x, xUpdate); // x = x + alpha * p\n\n            // Recompute residual for accuracy\n            r = vectorSubtract(b, matrixVectorMultiply(a, x));\n            alphaPrev = alpha;\n        }\n\n        return x; // Return best guess after maxIterations\n    }\n\n    /**\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.\");","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/ChebyshevIteration.java#L75-L111","documentation":"Thrown by ChebyshevIteration.validateInputs when the matrix A (double[][] a) has zero rows (a.length == 0). The solver operates on the dimension n = a.length, so an empty matrix has no system to solve. This is the first validation check, firing before any dimension-matching or eigenvalue checks.","triggerScenarios":"Calling ChebyshevIteration.solve(new double[0][], b, x0, ...) or passing a matrix that was constructed from an empty data source (empty list of rows converted to double[][]).","commonSituations":"A dynamically-constructed matrix from a data source (file, database, sensor readings) that returned zero rows. A filter or preprocessing step that removed all rows from the matrix. A matrix builder that defaulted to new double[0][] before data was loaded.","solutions":["Ensure the matrix A has at least one row before calling solve.","Validate the data pipeline that constructs the matrix — check for empty sources before conversion to double[][].","Add a precondition check: if (a == null || a.length == 0) throw or skip."],"exampleFix":"// before\nChebyshevIteration.solve(new double[0][], new double[0], new double[0], 1, 2, 100, 1e-6);\n// throws 'Matrix A cannot be empty.'\n\n// after\nif (a != null && a.length > 0 && b != null && x0 != null\n    && a.length == b.length && a.length == x0.length) {\n    double[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);\n}","handlingStrategy":"validation","validationCode":"// Validate matrix A is non-empty before calling solve\nif (a == null || a.length == 0) {\n    throw new IllegalArgumentException(\"Matrix A must be non-null and non-empty\");\n}\ndouble[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);","typeGuard":"static boolean isNonEmpty(double[][] m) {\n    return m != null && m.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Validate matrix dimensions at construction time, not just before solving.","Ensure the data source that builds the matrix returns at least one row.","Combine all matrix/vector/eigenvalue checks in a single precondition block before calling solve."],"tags":["linear-algebra","input-validation","matrix","chebyshev-iteration","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}