TheAlgorithms/Java · error · IllegalArgumentException
The input matrix cannot be jagged
Error message
The input matrix cannot be jagged
What it means
Thrown by QRDecomposition.validateInputMatrix when isJaggedMatrix detects that rows have differing lengths. QR decomposition assumes a rectangular matrix (every row the same column count); ragged rows break column indexing and dot-product loops that assume a fixed width. The check runs after null, empty, and row-validity checks.
Source
Thrown at src/main/java/com/thealgorithms/matrix/QRDecomposition.java:127
return result;
}
private static double norm(double[] v) {
return Math.sqrt(dotProduct(v, v));
}
private static void validateInputMatrix(double[][] matrix) {
if (matrix == null) {
throw new IllegalArgumentException("The input matrix cannot be null");
}
if (matrix.length == 0) {
throw new IllegalArgumentException("The input matrix cannot be empty");
}
if (!hasValidRows(matrix)) {
throw new IllegalArgumentException("The input matrix cannot have null or empty rows");
}
if (isJaggedMatrix(matrix)) {
throw new IllegalArgumentException("The input matrix cannot be jagged");
}
}
private static boolean hasValidRows(double[][] matrix) {
for (double[] row : matrix) {
if (row == null || row.length == 0) {
return false;
}
}
return true;
}
private static boolean isJaggedMatrix(double[][] matrix) {
int numColumns = matrix[0].length;
for (double[] row : matrix) {
if (row.length != numColumns) {
return true;
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Pad or truncate all rows to a uniform length before the call.
- Validate the parser ensures equal-length rows; reject ragged input upstream.
- Log each row's length to find the mismatched row index.
Example fix
// before
double[][] m = { {1,2}, {3,4,5} }; // jagged
QRDecomposition.decompose(m);
// after
int width = Arrays.stream(m).mapToInt(r -> r.length).max().getAsInt();
double[][] rect = new double[m.length][width];
for (int i=0;i<m.length;i++) System.arraycopy(m[i],0,rect[i],0,m[i].length);
QRDecomposition.decompose(rect); Defensive patterns
Strategy: validation
Validate before calling
int w = matrix[0].length;
for (double[] row : matrix) {
if (row.length != w) {
throw new IllegalStateException("Matrix is jagged");
}
} Type guard
static boolean isRectangular(double[][] m) {
if (m == null || m.length == 0) return false;
int w = m[0].length;
for (double[] row : m) if (row.length != w) return false;
return true;
} Prevention
- Enforce equal-length rows in your parser.
- When building from heterogeneous sources, normalize width before forming the matrix.
When it happens
Trigger: Passing a double[][] built from rows of different lengths (e.g., { {1,2}, {3,4,5} }), a CSV parser that did not pad short lines, or a manual matrix construction where one inner array was sized differently.
Common situations: Parsing inconsistent input where line lengths vary, copy errors during refactoring, or assembling a matrix from heterogeneous sources without normalizing width.
Related errors
- The input matrix cannot be jagged
- The input matrix cannot be null
- The input matrix cannot be empty
- The input matrix cannot have null or empty rows
- The input matrix cannot be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/7c2a4de83df2cb04.
Report an issue: GitHub.