TheAlgorithms/Java · error · IllegalArgumentException
Matrix A must be square.
Error message
Matrix A must be square.
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/maths/ChebyshevIteration.java:96
// Recompute residual for accuracy
r = vectorSubtract(b, matrixVectorMultiply(a, x));
alphaPrev = alpha;
}
return x; // Return best guess after maxIterations
}
/**
* Validates the inputs for the Chebyshev solver.
*/
private static void validateInputs(double[][] a, double[] b, double[] x0, double minEigenvalue, double maxEigenvalue, int maxIterations, double tolerance) {
int n = a.length;
if (n == 0) {
throw new IllegalArgumentException("Matrix A cannot be empty.");
}
if (n != a[0].length) {
throw new IllegalArgumentException("Matrix A must be square.");
}
if (n != b.length) {
throw new IllegalArgumentException("Matrix A and vector b dimensions do not match.");
}
if (n != x0.length) {
throw new IllegalArgumentException("Matrix A and vector x0 dimensions do not match.");
}
if (minEigenvalue <= 0) {
throw new IllegalArgumentException("Smallest eigenvalue must be positive (matrix must be positive-definite).");
}
if (maxEigenvalue <= minEigenvalue) {
throw new IllegalArgumentException("Max eigenvalue must be strictly greater than min eigenvalue.");
}
if (maxIterations <= 0) {
throw new IllegalArgumentException("Max iterations must be positive.");
}
if (tolerance <= 0) {
throw new IllegalArgumentException("Tolerance must be positive.");View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
ChebyshevIteration.solve(new double[2][3], b, x0, 1, 2, 100, 1e-6);
// throws 'Matrix A must be square.'
// after (ensure square matrix)
if (isSquare(a)) {
double[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);
}
static boolean isSquare(double[][] m) {
for (double[] row : m) if (row.length != m.length) return false;
return true;
} Defensive patterns
Strategy: validation
Validate before calling
// Validate matrix A is square before calling solve
static boolean isSquare(double[][] m) {
if (m == null || m.length == 0) return false;
for (double[] row : m) {
if (row.length != m.length) return false;
}
return true;
}
if (!isSquare(a)) {
throw new IllegalArgumentException("Matrix A must be square");
}
double[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol); Type guard
static boolean isSquare(double[][] m) {
if (m == null || m.length == 0) return false;
for (double[] row : m) if (row.length != m.length) return false;
return true;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Matrix A cannot be empty.
- Matrix A and vector b dimensions do not match.
- Matrix A and vector x0 dimensions do not match.
- Smallest eigenvalue must be positive (matrix must be positiv
- Max eigenvalue must be strictly greater than min eigenvalue.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/dad83f2f043197a1.
Report an issue: GitHub.