{"record":{"id":"b74ec7675fa73dae","repo":"TheAlgorithms/Java","slug":"matrix-a-and-vector-b-dimensions-do-not-match","errorCode":null,"errorMessage":"Matrix A and vector b dimensions do not match.","messagePattern":"Matrix A and vector b dimensions do not match\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/ChebyshevIteration.java","lineNumber":99,"sourceCode":"            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.\");\n        }\n        if (tolerance <= 0) {\n            throw new IllegalArgumentException(\"Tolerance must be positive.\");\n        }\n    }\n","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/ChebyshevIteration.java#L81-L117","documentation":"Thrown by ChebyshevIteration.validateInputs when the dimension of vector b does not match the dimension n of matrix A (a.length). The linear system Ax = b requires b to be in R^n where A is n x n. A dimension mismatch means the matrix-vector multiply Av in the residual computation (b - Ax) would fail or produce a meaningless result.","triggerScenarios":"Calling solve with a 3x3 matrix A but b.length == 2 or b.length == 4. For example: solve(new double[3][3], new double[2], new double[3], ...) where b has 2 elements but A has 3 rows.","commonSituations":"Assembling b from a different data source than A, leading to mismatched dimensions. A preprocessing step that resized b (e.g., adding or removing a boundary condition) without a corresponding change to A. Copy-paste errors where b was built for a different problem size.","solutions":["Ensure b.length == a.length before calling solve.","Construct b and A from the same dimensional context or validate both against a shared n value.","Add a precondition: assert b.length == a.length or throw a descriptive error before the call."],"exampleFix":"// before\nChebyshevIteration.solve(new double[3][3], new double[2], new double[3], 1, 2, 100, 1e-6);\n// throws 'Matrix A and vector b dimensions do not match.'\n\n// after\nint n = a.length;\nif (b.length == n) {\n    double[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);\n}","handlingStrategy":"validation","validationCode":"// Validate b dimension matches A before calling solve\nint n = a.length;\nif (b == null || b.length != n) {\n    throw new IllegalArgumentException(\"b.length must equal A's dimension (\" + n + \")\");\n}\ndouble[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);","typeGuard":"static boolean dimensionsMatch(double[][] a, double[] b) {\n    return a != null && a.length > 0 && b != null && b.length == a.length;\n}","tryCatchPattern":null,"preventionTips":["Construct A and b from the same dimensional context or validate both against a shared n.","Check b after any preprocessing that might resize it.","Use a combined precondition check for A, b, and x0 dimensions in one place."],"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"}