{"record":{"id":"30070d3371ba4336","repo":"TheAlgorithms/C-Sharp","slug":"array-is-empty-softmax","errorCode":null,"errorMessage":"Array is empty.","messagePattern":"Array is empty\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/SoftMax.cs","lineNumber":25,"sourceCode":"///     to the exponentials of the input numbers. After softmax, the elements of the vector always sum up to 1.\n///     https://en.wikipedia.org/wiki/Softmax_function.\n/// </summary>\npublic static class SoftMax\n{\n    /// <summary>\n    ///    Compute the SoftMax function.\n    ///    The SoftMax function is defined as:\n    ///    softmax(x_i) = exp(x_i) / sum(exp(x_j)) for j = 1 to n\n    ///    where x_i is the i-th element of the input vector.\n    ///    The elements of the output vector are the probabilities of the input vector, the output sums up to 1.\n    /// </summary>\n    /// <param name=\"input\">The input vector of real numbers.</param>\n    /// <returns>The output vector of real numbers.</returns>\n    public static double[] Compute(double[] input)\n    {\n        if (input.Length == 0)\n        {\n            throw new ArgumentException(\"Array is empty.\");\n        }\n\n        var exponentVector = new double[input.Length];\n        var sum = 0.0;\n        for (var index = 0; index < input.Length; index++)\n        {\n            exponentVector[index] = Math.Exp(input[index]);\n            sum += exponentVector[index];\n        }\n\n        for (var index = 0; index < input.Length; index++)\n        {\n            exponentVector[index] /= sum;\n        }\n\n        return exponentVector;\n    }\n}","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/SoftMax.cs#L7-L43","documentation":"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.","triggerScenarios":"Calling SoftMax.Compute(Array.Empty<double>()) or Compute on an array produced by a filtering operation that removed all elements.","commonSituations":"Feeding an empty batch/feature slice from a data pipeline; a LINQ Where().ToArray() that returned nothing; deserialized JSON arrays that arrived empty.","solutions":["Check input.Length > 0 before calling Compute and skip or substitute a default when empty","Ensure upstream data production cannot yield empty vectors (validate batch size)","Catch ArgumentException around Compute and handle the empty-input case explicitly"],"exampleFix":"// before\nvar result = SoftMax.Compute(logits); // logits may be empty\n// after\nvar result = logits.Length > 0 ? SoftMax.Compute(logits) : Array.Empty<double>();","handlingStrategy":"validation","validationCode":"if (input is null || input.Length == 0) return Array.Empty<double>();","typeGuard":"static bool IsNonEmpty(double[]? a) => a is { Length: > 0 };","tryCatchPattern":"try { return SoftMax.Compute(input); }\ncatch (ArgumentException ex) when (ex.Message == \"Array is empty.\") { return Array.Empty<double>(); }","preventionTips":["Return Array.Empty<T>() instead of null from producers","Check vector length after any filter/Where before numeric processing","Validate batch sizes at pipeline boundaries"],"tags":["argument-validation","empty-array","numeric"],"backgroundTag":"empty-required-field","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}