{"record":{"id":"abb9d0163c0894f6","repo":"TheAlgorithms/Java","slug":"matrix-a-and-vector-x0-dimensions-do-not-match","errorCode":null,"errorMessage":"Matrix A and vector x0 dimensions do not match.","messagePattern":"Matrix A and vector x0 dimensions do not match\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/ChebyshevIteration.java","lineNumber":102,"sourceCode":"        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.\");\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).","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/ChebyshevIteration.java#L84-L120","documentation":"Thrown by ChebyshevIteration.validateInputs when the initial guess vector x0 does not match the dimension n of matrix A (a.length). The solver clones x0 as the starting solution and updates it via x = x + alpha * p; if x0 has the wrong length, the vector add and the matrix-vector multiply would produce arrays of incompatible sizes.","triggerScenarios":"Calling solve with a 3x3 matrix A but x0.length != 3. For example, solve(A, b, new double[2], ...) where the initial guess has 2 elements instead of 3. Also common when x0 is initialized as new double[0] or defaults to null-derived zero-length.","commonSituations":"Using a zero-filled initial guess of the wrong size (e.g., new double[n-1] due to an off-by-one). Reusing x0 from a previous problem with a different dimension. Defaulting x0 to a cached vector that was computed for a different matrix size.","solutions":["Ensure x0.length == a.length before calling solve. The simplest valid initial guess is new double[n] (all zeros) where n = a.length.","Construct x0 in the same dimensional context as A and b.","Add a precondition: if (x0.length != a.length) throw or resize."],"exampleFix":"// before\nChebyshevIteration.solve(A, b, new double[2], 1, 2, 100, 1e-6); // A is 3x3\n// throws 'Matrix A and vector x0 dimensions do not match.'\n\n// after (use correctly-sized zero initial guess)\nint n = A.length;\ndouble[] x0 = new double[n]; // zero initial guess\ndouble[] x = ChebyshevIteration.solve(A, b, x0, minEig, maxEig, maxIter, tol);","handlingStrategy":"validation","validationCode":"// Validate x0 dimension matches A before calling solve\nint n = a.length;\nif (x0 == null || x0.length != n) {\n    x0 = new double[n]; // use zero initial guess as fallback\n}\ndouble[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);","typeGuard":"static boolean dimensionsMatch(double[][] a, double[] x0) {\n    return a != null && a.length > 0 && x0 != null && x0.length == a.length;\n}","tryCatchPattern":null,"preventionTips":["Use new double[n] (zero vector) as a safe default initial guess where n = a.length.","Do not reuse x0 across problems of different sizes.","Validate x0 alongside A and b in a single precondition block."],"tags":["linear-algebra","input-validation","dimension-mismatch","vector","chebyshev-iteration","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}