TheAlgorithms/Java · error · IllegalArgumentException

Matrix was found to be singular

Error message

Matrix was found to be singular

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/matrix/SolveSystem.java:66

                for (int j = k + 1; j < matrix.length; j++) {
                    matrix[i][j] -= matrix[i][k] * matrix[k][j];
                }
                constants[i] -= matrix[i][k] * constants[k];
            }
        }
        // back substitution
        double[] x = new double[constants.length];
        System.arraycopy(constants, 0, x, 0, constants.length);
        for (int i = matrix.length - 1; i >= 0; i--) {
            double sum = 0;
            for (int j = i + 1; j < matrix.length; j++) {
                sum += matrix[i][j] * x[j];
            }
            x[i] = constants[i] - sum;
            if (Math.abs(matrix[i][i]) > tol) {
                x[i] /= matrix[i][i];
            } else {
                throw new IllegalArgumentException("Matrix was found to be singular");
            }
        }
        return x;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check the determinant or rank of A before calling solveSystem; if ~0, the system has no unique solution.
  2. Use a least-squares / pseudo-inverse solver (SVD) for rank-deficient systems instead of exact Gaussian elimination.
  3. Condition the matrix: remove linearly dependent rows/columns or add regularization (Tikhonov).
  4. Increase numerical stability by scaling rows before elimination.

Example fix

// before
double[] x = SolveSystem.solveSystem(A, b); // throws if A singular

// after
// guard with a rank/determinant check
if (Math.abs(determinant(A)) < 1e-8) {
    // fall back to least-squares via pseudo-inverse
    x = leastSquaresSolve(A, b);
} else {
    x = SolveSystem.solveSystem(A, b);
}
Defensive patterns

Strategy: validation

Validate before calling

double det = determinant(matrix);
if (Math.abs(det) < 1e-8) {
    throw new IllegalStateException("Matrix is singular (det=" + det + ")");
}
double[] x = SolveSystem.solveSystem(matrix, constants);

Try / catch

try {
    x = SolveSystem.solveSystem(A, b);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("singular")) {
        // fall back to least-squares / pseudo-inverse
        x = leastSquaresSolve(A, b);
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: Ill-conditioned systems from measurement noise, degenerate constraint sets, duplicate equations, or a system with more unknowns effectively than independent equations.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/30b5acab9b178cd3. Report an issue: GitHub.