TheAlgorithms/C-Sharp · error · ArgumentException

Array is empty.

Error message

Array is empty.

What it means

SoftMax.Compute normalizes an input vector by summing exponentials, which is undefined for a zero-length array. The method throws ArgumentException when input.Length == 0 rather than returning an empty or degenerate result.

Solutions

  1. Check input.Length > 0 before calling Compute and skip or substitute a default when empty
  2. Ensure upstream data production cannot yield empty vectors (validate batch size)
  3. Catch ArgumentException around Compute and handle the empty-input case explicitly

Example fix

// before
var result = SoftMax.Compute(logits); // logits may be empty
// after
var result = logits.Length > 0 ? SoftMax.Compute(logits) : Array.Empty<double>();
Defensive patterns

Strategy: validation

Validate before calling

if (input is null || input.Length == 0) return Array.Empty<double>();

Type guard

static bool IsNonEmpty(double[]? a) => a is { Length: > 0 };

Try / catch

try { return SoftMax.Compute(input); }
catch (ArgumentException ex) when (ex.Message == "Array is empty.") { return Array.Empty<double>(); }

Prevention

When it happens

Trigger: Calling SoftMax.Compute(Array.Empty<double>()) or Compute on an array produced by a filtering operation that removed all elements.

Common situations: Feeding an empty batch/feature slice from a data pipeline; a LINQ Where().ToArray() that returned nothing; deserialized JSON arrays that arrived empty.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/30070d3371ba4336. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Numeric/SoftMax.cs:25

///     to the exponentials of the input numbers. After softmax, the elements of the vector always sum up to 1.
///     https://en.wikipedia.org/wiki/Softmax_function.
/// </summary>
public static class SoftMax
{
    /// <summary>
    ///    Compute the SoftMax function.
    ///    The SoftMax function is defined as:
    ///    softmax(x_i) = exp(x_i) / sum(exp(x_j)) for j = 1 to n
    ///    where x_i is the i-th element of the input vector.
    ///    The elements of the output vector are the probabilities of the input vector, the output sums up to 1.
    /// </summary>
    /// <param name="input">The input vector of real numbers.</param>
    /// <returns>The output vector of real numbers.</returns>
    public static double[] Compute(double[] input)
    {
        if (input.Length == 0)
        {
            throw new ArgumentException("Array is empty.");
        }

        var exponentVector = new double[input.Length];
        var sum = 0.0;
        for (var index = 0; index < input.Length; index++)
        {
            exponentVector[index] = Math.Exp(input[index]);
            sum += exponentVector[index];
        }

        for (var index = 0; index < input.Length; index++)
        {
            exponentVector[index] /= sum;
        }

        return exponentVector;
    }
}

View on GitHub (pinned to 96e2905cab)