TheAlgorithms/Java · error · IllegalArgumentException

Matrix A cannot be empty.

Error message

Matrix A cannot be empty.

What it means

Thrown by ChebyshevIteration.validateInputs when the matrix A (double[][] a) has zero rows (a.length == 0). The solver operates on the dimension n = a.length, so an empty matrix has no system to solve. This is the first validation check, firing before any dimension-matching or eigenvalue checks.

Source

Thrown at src/main/java/com/thealgorithms/maths/ChebyshevIteration.java:93

            double[] xUpdate = scalarMultiply(alpha, p);
            x = vectorAdd(x, xUpdate); // x = x + alpha * p

            // Recompute residual for accuracy
            r = vectorSubtract(b, matrixVectorMultiply(a, x));
            alphaPrev = alpha;
        }

        return x; // Return best guess after maxIterations
    }

    /**
     * Validates the inputs for the Chebyshev solver.
     */
    private static void validateInputs(double[][] a, double[] b, double[] x0, double minEigenvalue, double maxEigenvalue, int maxIterations, double tolerance) {
        int n = a.length;
        if (n == 0) {
            throw new IllegalArgumentException("Matrix A cannot be empty.");
        }
        if (n != a[0].length) {
            throw new IllegalArgumentException("Matrix A must be square.");
        }
        if (n != b.length) {
            throw new IllegalArgumentException("Matrix A and vector b dimensions do not match.");
        }
        if (n != x0.length) {
            throw new IllegalArgumentException("Matrix A and vector x0 dimensions do not match.");
        }
        if (minEigenvalue <= 0) {
            throw new IllegalArgumentException("Smallest eigenvalue must be positive (matrix must be positive-definite).");
        }
        if (maxEigenvalue <= minEigenvalue) {
            throw new IllegalArgumentException("Max eigenvalue must be strictly greater than min eigenvalue.");
        }
        if (maxIterations <= 0) {
            throw new IllegalArgumentException("Max iterations must be positive.");

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure the matrix A has at least one row before calling solve.
  2. Validate the data pipeline that constructs the matrix — check for empty sources before conversion to double[][].
  3. Add a precondition check: if (a == null || a.length == 0) throw or skip.

Example fix

// before
ChebyshevIteration.solve(new double[0][], new double[0], new double[0], 1, 2, 100, 1e-6);
// throws 'Matrix A cannot be empty.'

// after
if (a != null && a.length > 0 && b != null && x0 != null
    && a.length == b.length && a.length == x0.length) {
    double[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate matrix A is non-empty before calling solve
if (a == null || a.length == 0) {
    throw new IllegalArgumentException("Matrix A must be non-null and non-empty");
}
double[] x = ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, tol);

Type guard

static boolean isNonEmpty(double[][] m) {
    return m != null && m.length > 0;
}

Prevention

When it happens

Trigger: Calling ChebyshevIteration.solve(new double[0][], b, x0, ...) or passing a matrix that was constructed from an empty data source (empty list of rows converted to double[][]).

Common situations: A dynamically-constructed matrix from a data source (file, database, sensor readings) that returned zero rows. A filter or preprocessing step that removed all rows from the matrix. A matrix builder that defaulted to new double[0][] before data was loaded.

Related errors


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