{"record":{"id":"dad83f2f043197a1","repo":"TheAlgorithms/Java","slug":"matrix-a-must-be-square","errorCode":null,"errorMessage":"Matrix A must be square.","messagePattern":"Matrix A must be square\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/ChebyshevIteration.java","lineNumber":96,"sourceCode":"\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.\");\n        }\n        if (tolerance <= 0) {\n            throw new IllegalArgumentException(\"Tolerance must be positive.\");","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/ChebyshevIteration.java#L78-L114","documentation":"Thrown by ChebyshevIteration.validateInputs when the matrix A is not square — specifically when a.length (number of rows) != a[0].length (number of columns in the first row). The Chebyshev iteration method requires a square matrix because it solves Ax = b where A maps R^n to R^n. Non-square matrices cannot have the eigenvalue spectrum this method depends on.","triggerScenarios":"Calling solve with a matrix like new double[][]{{1,0,0},{0,1}} (2 rows but row lengths differ) or new double[3][4] (3 rows, 4 columns). Also triggered if rows have ragged lengths where a[0].length happens to differ from a.length.","commonSituations":"Reading a matrix from a file or data structure where the dimensions were not validated for squareness. Constructing a matrix from a 2D data grid that was transposed or had extra columns. Using a coefficient matrix from a least-squares problem (rectangular) in a solver that requires square input.","solutions":["Ensure the matrix A is square: a.length == a[i].length for all rows i before calling solve.","If the system is over- or under-determined, use a different solver (least-squares, QR decomposition) instead of Chebyshev iteration.","Validate matrix dimensions at construction time or at the data boundary."],"exampleFix":"// before\nChebyshevIteration.solve(new double[2][3], b, x0, 1, 2, 100, 1e-6);\n// throws 'Matrix A must be square.'\n\n// after (ensure square matrix)\nif (isSquare(a)) {\n    double[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);\n}\n\nstatic boolean isSquare(double[][] m) {\n    for (double[] row : m) if (row.length != m.length) return false;\n    return true;\n}","handlingStrategy":"validation","validationCode":"// Validate matrix A is square before calling solve\nstatic boolean isSquare(double[][] m) {\n    if (m == null || m.length == 0) return false;\n    for (double[] row : m) {\n        if (row.length != m.length) return false;\n    }\n    return true;\n}\nif (!isSquare(a)) {\n    throw new IllegalArgumentException(\"Matrix A must be square\");\n}\ndouble[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);","typeGuard":"static boolean isSquare(double[][] m) {\n    if (m == null || m.length == 0) return false;\n    for (double[] row : m) if (row.length != m.length) return false;\n    return true;\n}","tryCatchPattern":null,"preventionTips":["Validate squareness at matrix construction time.","Also check for ragged arrays (rows of different lengths) — isSquare handles this.","If the system is rectangular, use a least-squares solver instead of Chebyshev iteration."],"tags":["linear-algebra","input-validation","matrix","square-matrix","chebyshev-iteration","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}