{"record":{"id":"7c2a4de83df2cb04","repo":"TheAlgorithms/Java","slug":"the-input-matrix-cannot-be-jagged","errorCode":null,"errorMessage":"The input matrix cannot be jagged","messagePattern":"The input matrix cannot be jagged","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/QRDecomposition.java","lineNumber":127,"sourceCode":"        return result;\n    }\n\n    private static double norm(double[] v) {\n        return Math.sqrt(dotProduct(v, v));\n    }\n\n    private static void validateInputMatrix(double[][] matrix) {\n        if (matrix == null) {\n            throw new IllegalArgumentException(\"The input matrix cannot be null\");\n        }\n        if (matrix.length == 0) {\n            throw new IllegalArgumentException(\"The input matrix cannot be empty\");\n        }\n        if (!hasValidRows(matrix)) {\n            throw new IllegalArgumentException(\"The input matrix cannot have null or empty rows\");\n        }\n        if (isJaggedMatrix(matrix)) {\n            throw new IllegalArgumentException(\"The input matrix cannot be jagged\");\n        }\n    }\n\n    private static boolean hasValidRows(double[][] matrix) {\n        for (double[] row : matrix) {\n            if (row == null || row.length == 0) {\n                return false;\n            }\n        }\n        return true;\n    }\n\n    private static boolean isJaggedMatrix(double[][] matrix) {\n        int numColumns = matrix[0].length;\n        for (double[] row : matrix) {\n            if (row.length != numColumns) {\n                return true;\n            }","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/QRDecomposition.java#L109-L145","documentation":"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.","triggerScenarios":"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.","commonSituations":"Parsing inconsistent input where line lengths vary, copy errors during refactoring, or assembling a matrix from heterogeneous sources without normalizing width.","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."],"exampleFix":"// before\ndouble[][] m = { {1,2}, {3,4,5} }; // jagged\nQRDecomposition.decompose(m);\n\n// after\nint width = Arrays.stream(m).mapToInt(r -> r.length).max().getAsInt();\ndouble[][] rect = new double[m.length][width];\nfor (int i=0;i<m.length;i++) System.arraycopy(m[i],0,rect[i],0,m[i].length);\nQRDecomposition.decompose(rect);","handlingStrategy":"validation","validationCode":"int w = matrix[0].length;\nfor (double[] row : matrix) {\n    if (row.length != w) {\n        throw new IllegalStateException(\"Matrix is jagged\");\n    }\n}","typeGuard":"static boolean isRectangular(double[][] m) {\n    if (m == null || m.length == 0) return false;\n    int w = m[0].length;\n    for (double[] row : m) if (row.length != w) return false;\n    return true;\n}","tryCatchPattern":null,"preventionTips":["Enforce equal-length rows in your parser.","When building from heterogeneous sources, normalize width before forming the matrix."],"tags":["matrix","precondition","jagged-array","linear-algebra"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}