{"record":{"id":"bf15811e934cb90b","repo":"TheAlgorithms/Java","slug":"matrix-is-rank-deficient-cannot-perform-qr-decomp","errorCode":null,"errorMessage":"Matrix is rank deficient. Cannot perform QR decomposition.","messagePattern":"Matrix is rank deficient\\. Cannot perform QR decomposition\\.","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/QRDecomposition.java","lineNumber":64,"sourceCode":"\n        int m = matrix.length;\n        int n = matrix[0].length;\n\n        double[][] q = new double[m][n];\n        double[][] r = new double[n][n];\n\n        for (int j = 0; j < n; j++) {\n            double[] v = getColumn(matrix, j);\n\n            for (int i = 0; i < j; i++) {\n                double[] qi = getColumn(q, i);\n                r[i][j] = dotProduct(qi, v);\n                v = subtractVectors(v, scalarMultiply(qi, r[i][j]));\n            }\n\n            r[j][j] = norm(v);\n            if (r[j][j] == 0) {\n                throw new ArithmeticException(\"Matrix is rank deficient. Cannot perform QR decomposition.\");\n            }\n            double[] qj = scalarMultiply(v, 1.0 / r[j][j]);\n            setColumn(q, j, qj);\n        }\n\n        return new QR(q, r);\n    }\n\n    private static double[] getColumn(double[][] matrix, int col) {\n        int m = matrix.length;\n        double[] column = new double[m];\n        for (int i = 0; i < m; i++) {\n            column[i] = matrix[i][col];\n        }\n        return column;\n    }\n\n    private static void setColumn(double[][] matrix, int col, double[] column) {","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/QRDecomposition.java#L46-L82","documentation":"Thrown by QRDecomposition.decompose (as an ArithmeticException, not IllegalArgumentException) when the Gram-Schmidt process computes a zero norm for the residual of a column (r[j][j] == 0). A zero residual means that column is linearly dependent on the already-orthonormalized earlier columns, so the matrix is rank-deficient and no valid orthogonal Q exists; dividing by r[j][j] would also divide by zero.","triggerScenarios":"Call decompose on a matrix with a duplicate column, a column that is a linear combination of previous columns, an all-zero column, or a wide/ill-conditioned matrix where numerical cancellation drives the residual to (exactly) 0.0.","commonSituations":"Underdetermined systems, data matrices with collinear features (common in regression datasets), a column of constants combined with another constant column, or float inputs that happen to cancel exactly.","solutions":["Pre-check rank (e.g., via SVD or by computing column independence) and drop/recombine dependent columns before decompose.","Switch to a rank-revealing QR (column pivoting) or SVD-based decomposition that tolerates rank deficiency.","Add tiny regularization or perturbation if near-deficiency is causing exact-zero residuals in float data."],"exampleFix":"// before\nQRDecomposition.QR qr = QRDecomposition.depose(matrix); // typo aside\n\n// after\n// remove linearly dependent columns first (e.g., via a rank check)\ndouble[][] fullRank = removeDependentColumns(matrix);\nQRDecomposition.QR qr = QRDecomposition.decompose(fullRank);","handlingStrategy":"validation","validationCode":"// approximate rank check via independent columns\nboolean fullRank = hasIndependentColumns(matrix); // your rank-revealing helper\nif (!fullRank) throw new ArithmeticException(\"matrix is rank deficient\");\nQRDecomposition.QR qr = QRDecomposition.decompose(matrix);","typeGuard":null,"tryCatchPattern":"try {\n    QRDecomposition.QR qr = QRDecomposition.decompose(matrix);\n} catch (ArithmeticException e) {\n    // fall back to a rank-revealing decomposition (pivoted QR / SVD)\n    qr = pivotedQrOrSvd(matrix);\n}","preventionTips":["Audit feature matrices for collinearity before decomposing.","Prefer rank-revealing decompositions (column-pivoted QR, SVD) for data known to be near rank-deficient.","Remember this util throws ArithmeticException, not IllegalArgumentException, unlike the other guards here."],"tags":["matrix","linear-algebra","qr-decomposition","rank-deficient","gram-schmidt"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}