Skip to content

SumAll, MultiplyAll and ToVector throw an internal bug exception on an empty sequence, where ToSet and ToPiecewise answer #1028

Description

@Rafael-SOWNet

Found while writing worked examples for every member of AngouriMath.Extensions (#585, #746 item 4).

Three extension methods refuse an empty sequence by throwing something the caller cannot act on, while their neighbours in the same file accept one and answer correctly.

Measured on a master build (6b93b40, v2.3.0):

new Entity[0].SumAll();       // AngouriBugException: At least 1 child required
new Entity[0].MultiplyAll();  // AngouriBugException: At least 1 child required
new Entity[0].ToVector();     // System.IndexOutOfRangeException
MathS.Vector();               // System.IndexOutOfRangeException

new Entity[0].ToSet();           // {  }
new Providedf[0].ToPiecewise();  // piecewise()

Two separate problems.

AngouriBugException is reachable from ordinary input. Its message ends "please report about it to the official repository", so an empty IEnumerable<Entity> — a list that a Where happened to filter to nothing — asks the user to file a bug report for their own data. It comes out of TreeAnalyzer.MultiHangBinary (Functions/TreeAnalyzer/Sort.Definition.cs:41), whose precondition is genuine; what is wrong is that SumAll/MultiplyAll pass an unchecked user array into it.

The mathematics gives the answer here rather than only the exception type: a fold over a monoid has an identity, so the empty sum is 0 and the empty product is 1. That is what Enumerable.Sum gives, what Aggregate with a seed gives, and what makes xs.Concat(ys).SumAll() == xs.SumAll() + ys.SumAll() hold for every pair including the empty one. Answering 0 and 1 is the fix I would expect; if that is unwanted, then an ArgumentException naming the empty argument, but not the internal one either way.

ToVector leaks a framework exception. System.IndexOutOfRangeException is not under AngouriMathBaseException, so it is outside the hierarchy Docs/Usage/Exceptions.md documents, and a caller who catches the documented base type does not catch this. It comes from MathS.Vector (Convenience/MathS.cs:4811) reading arr[0] before checking the length, so MathS.Vector() has it too and the extension only inherits it. A 0x1 matrix may or may not be a thing this library wants to have; either way the refusal should be an InvalidMatrixOperationException (or whichever member of the hierarchy fits) rather than an index error.

ToSet and ToPiecewise already take the empty sequence, which is why this reads as an oversight rather than a decision: the same argument gets three different receptions across one file.

Not fixed in the documentation PR that found it, because that PR must not change a single answer.

Activity

  1. Rafael-SOWNet commented on Aug 23, 2026

    @Rafael-SOWNet
    MemberAuthor

    The ToVector half is reachable from a parsed string, not only from the API: [] is a valid production of the grammar ('[' function_arguments ']' with an empty argument list), so

    MathS.FromString("[]");   // System.IndexOutOfRangeException

    on a build of this worktree's master (9b6fb5b). That makes it a string a caller can type rather than an empty IEnumerable a Where produced, and Docs/Usage/Exceptions.md promises everything a parse throws is under AngouriMathBaseException. The empty set {} parses and answers, which is the same contrast the issue already draws between ToSet and ToVector.

    Noted while writing down what the grammar accepts for #746 item 8; no fix here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions