{"record":{"id":"30b5acab9b178cd3","repo":"TheAlgorithms/Java","slug":"matrix-was-found-to-be-singular","errorCode":null,"errorMessage":"Matrix was found to be singular","messagePattern":"Matrix was found to be singular","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/SolveSystem.java","lineNumber":66,"sourceCode":"                for (int j = k + 1; j < matrix.length; j++) {\n                    matrix[i][j] -= matrix[i][k] * matrix[k][j];\n                }\n                constants[i] -= matrix[i][k] * constants[k];\n            }\n        }\n        // back substitution\n        double[] x = new double[constants.length];\n        System.arraycopy(constants, 0, x, 0, constants.length);\n        for (int i = matrix.length - 1; i >= 0; i--) {\n            double sum = 0;\n            for (int j = i + 1; j < matrix.length; j++) {\n                sum += matrix[i][j] * x[j];\n            }\n            x[i] = constants[i] - sum;\n            if (Math.abs(matrix[i][i]) > tol) {\n                x[i] /= matrix[i][i];\n            } else {\n                throw new IllegalArgumentException(\"Matrix was found to be singular\");\n            }\n        }\n        return x;\n    }\n}\n","sourceCodeStart":48,"sourceCodeEnd":72,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/SolveSystem.java#L48-L72","documentation":"Thrown by SolveSystem.solveSystem during back substitution when a diagonal pivot |matrix[i][i]| is at or below the tolerance 1e-8, meaning the matrix is (numerically) singular and Ax=b has no unique solution. Gaussian elimination with partial pivoting already ran; if a pivot still collapses to ~0 after elimination, the system is rank-deficient. Note solveSystem OVERWRITES the input matrix, so the singular state is post-elimination.","triggerScenarios":"Passing a singular matrix (determinant 0), e.g., two proportional rows/columns, an under-determined system, or a near-singular matrix whose tiny pivot falls below tol. Also reproducible with a non-square matrix shaped to look square but linearly dependent.","commonSituations":"Ill-conditioned systems from measurement noise, degenerate constraint sets, duplicate equations, or a system with more unknowns effectively than independent equations.","solutions":["Check the determinant or rank of A before calling solveSystem; if ~0, the system has no unique solution.","Use a least-squares / pseudo-inverse solver (SVD) for rank-deficient systems instead of exact Gaussian elimination.","Condition the matrix: remove linearly dependent rows/columns or add regularization (Tikhonov).","Increase numerical stability by scaling rows before elimination."],"exampleFix":"// before\ndouble[] x = SolveSystem.solveSystem(A, b); // throws if A singular\n\n// after\n// guard with a rank/determinant check\nif (Math.abs(determinant(A)) < 1e-8) {\n    // fall back to least-squares via pseudo-inverse\n    x = leastSquaresSolve(A, b);\n} else {\n    x = SolveSystem.solveSystem(A, b);\n}","handlingStrategy":"validation","validationCode":"double det = determinant(matrix);\nif (Math.abs(det) < 1e-8) {\n    throw new IllegalStateException(\"Matrix is singular (det=\" + det + \")\");\n}\ndouble[] x = SolveSystem.solveSystem(matrix, constants);","typeGuard":null,"tryCatchPattern":"try {\n    x = SolveSystem.solveSystem(A, b);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"singular\")) {\n        // fall back to least-squares / pseudo-inverse\n        x = leastSquaresSolve(A, b);\n    } else {\n        throw e;\n    }\n}","preventionTips":["Check matrix rank or determinant before solving.","Remove linearly dependent rows/columns or apply regularization for ill-conditioned systems.","Pass solveSystem a defensive copy — it overwrites its input."],"tags":["matrix","singular","linear-algebra","gaussian-elimination","numerical"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}