Skip to content

Commit ad02ebb

Browse files
A sum over a set: sum(f, x in S) (#1624)
* A sum over a set: sum(f, x in S) sum took a range and nothing else, so a sum over a set had no spelling: sum(f, x in S) was a parse error asking for the range form's four arguments. SumOverSetf reads the second argument as `variable in set` and binds the variable over the term and the set, as max(f, t in S) does; it prints \sum_{x \in S} f, and MathS.Sum(f, x, S) builds it. The range form is untouched. A sum over the roots of a polynomial is a sum over its set of roots, sum(f(w), w in { w : p(w) = 0 }), with no index and no order. The terms are added up where the set is finite and its members are known: a listed set of numbers, distinct by value, and the roots of a polynomial where the solver writes all of them -- counted against the degree of the square-free part, so a root the solver missed leaves the sum as written rather than a term short. The solver is asked only where every irreducible factor has degree at most four or is a binomial. Evaluated to a number, the roots it cannot write are found to the working precision by Durand-Kerner, one square-free factor at a time, all of them or none; the polynomial is read off the tree, so that holds with downcasting off. A set whose members may coincide, and one that is not finite, are left as written. Termwise differentiation where the set does not depend on the variable, zero in the bound name; SymPy export as a sum of substitutions or a RootSum of the square-free part. Part of #1285. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * Roots are written out only where they read as simply as the sum Rational roots, the roots of a quadratic and the roots of a binomial are written and added; the roots of an irreducible cubic or quartic are not. In radicals they read no more simply than the sum over them, and for a real cubic with three real roots Cardano's formula writes them with complex radicals, so sum(w^2, w in { w : w^3 + w + 1 = 0 }) is kept as it is and evaluates to -2. A numeric value goes to the numeric roots at once rather than asking the solver first, since a check evaluates at many points and the roots do not depend on them. SquareFreeParts and NumericRoots are internal, for the interval evaluation that reads them next. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent c7b18bb commit ad02ebb

22 files changed

Lines changed: 742 additions & 4 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,36 @@ the ends are about, `n >= 0`. With two names it is left as it was, `n >= m - 1`
16081608
|---|---|---|
16091609
| `"sum(2 k + 1, k, 0, n - 1)".Simplify()` | `piecewise((2 * n + (n - 1) ^ 2 - 1) provided (n - 1 >= -1), 0 provided True)` | `piecewise((n ^ 2) provided (n >= 0), 0 provided True)` |
16101610

1611+
### A sum over a set: `sum(f, x in S)`
1612+
1613+
`sum` took a range and nothing else, so a sum over a set had no spelling: `sum(f, x in S)` was a
1614+
parse error asking for the four arguments of the range form. `SumOverSetf` reads the second
1615+
argument as `variable in set` and binds the variable over the term and the set, as `max(f, t in S)`
1616+
does: spelt `sum(f, x in S)`, printed `\sum_{x \in S} f` in LaTeX, and built by
1617+
`MathS.Sum(f, x, S)`. The range form `sum(f, k, from, to)` is untouched. A sum over the roots of a
1618+
polynomial is a sum over its set of roots, `sum(f(w), w in { w : p(w) = 0 })`, with no index and no
1619+
order.
1620+
1621+
The terms are added up where the set is finite and its members are known: a listed set of numbers,
1622+
distinct by value, and the roots of a polynomial where every one is rational, a root of a quadratic
1623+
or a root of a binomial. That is checked by counting them against the degree of the square-free
1624+
part, so a root the solver missed leaves the sum as written rather than one term short. The roots of
1625+
an irreducible cubic or quartic are not written out in radicals, which read no more simply than the
1626+
sum. Evaluated to a number, the roots it cannot
1627+
write are found to the working precision by the Durand–Kerner iteration, all of them or none. A set
1628+
with a symbol among its members, whose members may coincide, and a set that is not finite are left
1629+
as written. [#1285](https://github.com/asc-community/AngouriMath/issues/1285). Both columns
1630+
measured on a build, `v2.5.0` against this change.
1631+
1632+
| | Was (2.5.0) | Is |
1633+
|---|---|---|
1634+
| `"sum(x^2, x in {1, 2, 3})".ToEntity()` | `FunctionArgumentCountException`: *sum should have exactly 4 arguments but 2 arguments are provided* | `14` once simplified |
1635+
| `"sum(w^2, w in { w : w^3 - 2w + 1 = 0 })".ToEntity().Simplify()` | the same exception | `4` |
1636+
| `"sum(w^5, w in { w : w^5 + w + 3 = 0 })".ToEntity().Evaled` | the same exception | `-15`, from the five roots found numerically |
1637+
| `"sum(k, k in ZZ intersect [1; 10])".ToEntity().Simplify()` | the same exception | `55`, as `sum(k, k, 1, 10)` is |
1638+
| `"sum(x, x in {a, b})".ToEntity().Simplify()` | the same exception | `sum(x, x in { a, b })`, since `a` and `b` may be equal |
1639+
| `"sum(k, k, 1, 10)".ToEntity().Simplify()` | `55` | the same |
1640+
16111641
### `(a + b asech(c x))/(d + e x)^2` is integrated, and a root written apart with `|x|` no longer needs a parity
16121642

16131643
Rubi's 7.5.1 with a symbolic linear below the bar ran for ten minutes without an answer. By

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
279279
[AngouriMath/Functions/Continuous/ExtremumOverSet.cs]
280280
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
281281

282+
[AngouriMath/Functions/Algebra/SumOverSet.cs]
283+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
284+
285+
[AngouriMath/Functions/Continuous/Solvers/NumericalSolving/DurandKerner.cs]
286+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
287+
282288
[Tests/UnitTests/Core/Transformations/LogarithmSpellingsTest.cs]
283289
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
284290

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7476,6 +7476,40 @@ public static Entity Limit(Entity expr, Entity var, Entity dest, ApproachFrom ap
74767476
public static Entity Sum(Entity expr, Entity var, Entity from, Entity to)
74777477
=> new Summationf(expr, var, from, to);
74787478

7479+
/// <summary>
7480+
/// The sum of <paramref name="expr"/> over the members of <paramref name="over"/> as
7481+
/// <paramref name="var"/> ranges over it, each member counted once: written
7482+
/// <c>sum(expr, var in over)</c>. A sum over the roots of a polynomial is a sum over its
7483+
/// set of roots, <c>sum(f(w), w in { w : p(w) = 0 })</c>, with no index and no order.
7484+
/// </summary>
7485+
/// <param name="expr">The term, as a function of <paramref name="var"/>.</param>
7486+
/// <param name="var">The name that ranges over the set, bound by the sum.</param>
7487+
/// <param name="over">The set.</param>
7488+
/// <returns>
7489+
/// The terms added up where the set is finite and its members are known: a listed set of
7490+
/// numbers, or roots of a polynomial that are rational, roots of a quadratic or roots of a
7491+
/// binomial -- those of an irreducible cubic or quartic are kept as the sum, radicals
7492+
/// reading no more simply than it. Evaluated to a
7493+
/// number, the roots it cannot write are found numerically, all of them or none. Left as
7494+
/// written otherwise: a set with a symbol among its members, which may coincide, or a set
7495+
/// that is not finite.
7496+
/// </returns>
7497+
/// <remarks><a href="https://github.com/asc-community/AngouriMath/issues/1285">#1285</a></remarks>
7498+
/// <example>
7499+
/// <code>
7500+
/// using System;
7501+
/// using static AngouriMath.MathS;
7502+
///
7503+
/// Console.WriteLine(Sum("w^2", "w", "{ w : w^3 - 2w + 1 = 0 }").Simplify());
7504+
/// </code>
7505+
/// Prints
7506+
/// <code>
7507+
/// 4
7508+
/// </code>
7509+
/// </example>
7510+
public static Entity Sum(Entity expr, Entity var, Entity over)
7511+
=> new SumOverSetf(expr, var, over);
7512+
74797513
/// <summary>
74807514
/// The largest value <paramref name="expr"/> takes as <paramref name="var"/> ranges over
74817515
/// <paramref name="over"/>, written <c>max(expr, var in over)</c>. Answered over a finite

‎Sources/AngouriMath/Core/Antlr/AngouriMath.g‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ atom returns[Entity value]
571571
| 'limit(' args = function_arguments ')' { Assert("limit", 3, $args.list.Count); $value = MathS.Limit($args.list[0], $args.list[1], $args.list[2]); }
572572
| 'limitleft(' args = function_arguments ')' { Assert("limitleft", 3, $args.list.Count); $value = MathS.Limit($args.list[0], $args.list[1], $args.list[2], AngouriMath.Core.ApproachFrom.Left); }
573573
| 'limitright(' args = function_arguments ')' { Assert("limitright", 3, $args.list.Count); $value = MathS.Limit($args.list[0], $args.list[1], $args.list[2], AngouriMath.Core.ApproachFrom.Right); }
574-
| 'sum(' args = function_arguments ')' { Assert("sum", 4, $args.list.Count); $value = MathS.Sum($args.list[0], $args.list[1], $args.list[2], $args.list[3]); }
574+
| 'sum(' args = function_arguments ')' { if ($args.list.Count == 2 && AngouriMath.Functions.ExtremumOverSet.AsRange($args.list[1]) is var (sumVar, sumOver)) $value = MathS.Sum($args.list[0], sumVar, sumOver); else { Assert("sum", 4, $args.list.Count); $value = MathS.Sum($args.list[0], $args.list[1], $args.list[2], $args.list[3]); } }
575575
| 'product(' args = function_arguments ')' { Assert("product", 4, $args.list.Count); $value = MathS.Product($args.list[0], $args.list[1], $args.list[2], $args.list[3]); }
576576
| 'signum(' args = function_arguments ')' { Assert("signum", 1, $args.list.Count); $value = MathS.Signum($args.list[0]); }
577577
| 'sgn(' args = function_arguments ')' { Assert("sgn", 1, $args.list.Count); $value = MathS.Signum($args.list[0]); }

‎Sources/AngouriMath/Core/Antlr/AngouriMathParser.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4449,7 +4449,7 @@ public AtomContext atom() {
44494449
_localctx.args = function_arguments();
44504450
State = 916;
44514451
Match(T__30);
4452-
Assert("sum", 4, _localctx.args.list.Count); _localctx.value = MathS.Sum(_localctx.args.list[0], _localctx.args.list[1], _localctx.args.list[2], _localctx.args.list[3]);
4452+
if (_localctx.args.list.Count == 2 && AngouriMath.Functions.ExtremumOverSet.AsRange(_localctx.args.list[1]) is var (sumVar, sumOver)) _localctx.value = MathS.Sum(_localctx.args.list[0], sumVar, sumOver); else { Assert("sum", 4, _localctx.args.list.Count); _localctx.value = MathS.Sum(_localctx.args.list[0], _localctx.args.list[1], _localctx.args.list[2], _localctx.args.list[3]); }
44534453
}
44544454
break;
44554455
case 100:

‎Sources/AngouriMath/Core/Domains.Classes.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ partial record Productf
280280
internal override Domain DefaultCodomain => Domain.Complex;
281281
}
282282

283+
partial record SumOverSetf
284+
{
285+
/// <inheritdoc/>
286+
public override Domain Codomain { get; protected init; } = Domain.Complex;
287+
/// <inheritdoc/>
288+
internal override Domain DefaultCodomain => Domain.Complex;
289+
}
290+
283291
partial record Maximumf
284292
{
285293
/// <inheritdoc/>

‎Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Calculus.Classes.cs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,29 @@ public override Entity Replace(Func<Entity, Entity> func) =>
119119
protected override Entity[] InitDirectChildren() => new[] { Expression, Var, From, To };
120120
}
121121

122+
/// <summary>
123+
/// A sum over a set: <c>sum(f(x), x in S)</c> is the sum of <c>f(x)</c> over the members
124+
/// <c>x</c> of <c>S</c>, each counted once. A binder, like <see cref="Summationf"/>, whose
125+
/// range is a set rather than two bounds, and so has no index and no order: the sum over
126+
/// the roots of a polynomial is <c>sum(f(w), w in { w : p(w) = 0 })</c>, which needs no
127+
/// first root.
128+
/// https://github.com/asc-community/AngouriMath/issues/1285
129+
/// </summary>
130+
public sealed partial record SumOverSetf(Entity Expression, Entity Var, Entity Over) : CalculusOperator(Expression, Var)
131+
{
132+
/// <summary>The set the variable ranges over.</summary>
133+
public Entity Over { get; init; } = Binding.Of(Var).In(Over);
134+
135+
private SumOverSetf New(Entity expression, Entity var, Entity over) =>
136+
ReferenceEquals(Expression, expression) && ReferenceEquals(Var, var) && ReferenceEquals(Over, over)
137+
? this : new(expression, var, over) { Codomain = Codomain };
138+
/// <inheritdoc/>
139+
public override Entity Replace(Func<Entity, Entity> func) =>
140+
func(New(Expression.Replace(func), Var, Over.Replace(func)));
141+
/// <inheritdoc/>
142+
protected override Entity[] InitDirectChildren() => new[] { Expression, Var, Over };
143+
}
144+
122145
/// <summary>
123146
/// A node of limit
124147
/// </summary>

‎Sources/AngouriMath/Core/Entity/Entity.Definition.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,9 @@ public IReadOnlyList<Variable> Vars
694694
=> BoundBy(index, body, from, to),
695695
Productf(var body, var index, var from, var to)
696696
=> BoundBy(index, body, from, to),
697+
// A sum over a set binds the name that ranges over the set:
698+
// sum(w * a, w in { w : w^2 = 2 }) is a function of a alone.
699+
SumOverSetf(var body, var bound, var over) => BoundBy(bound, body, over),
697700
// An extremum over a set binds the variable that ranges over the set:
698701
// max(t^2 + a, t in [0; 1]) is a function of a alone.
699702
Maximumf(var body, var bound, var over) => BoundBy(bound, body, over),

‎Sources/AngouriMath/Core/Serialization/Entity.Serialization.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ [EntityJsonConverter] partial record SpecialSet
135135
[EntityJsonConverter] partial record Sumf;
136136
[EntityJsonConverter] partial record Summationf;
137137
[EntityJsonConverter] partial record Maximumf;
138+
[EntityJsonConverter] partial record SumOverSetf;
138139
[EntityJsonConverter] partial record Minimumf;
139140
[EntityJsonConverter] partial record Argmaxf;
140141
[EntityJsonConverter] partial record Argminf;

‎Sources/AngouriMath/Docs/Usage/Syntax.md‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,9 @@ undefined at `0`; `Si(+oo)` is `pi / 2` and `Ci(+oo)` is `0`.
328328

329329
**Calculus** — `derivative(expr, var, order)`, `integral(expr, var)`,
330330
`integral(expr, var, from, to)`, `limit(expr, var, dest)`, `limitleft(...)`, `limitright(...)`;
331-
`max(expr, var in set)` and `min` for the extremum of an expression over a set, `argmax` and
332-
`argmin` for the set of points where it is taken — `max(a, b)` of two values is still the larger.
331+
`sum(expr, var in set)` for a sum over a set; `max(expr, var in set)` and `min` for the extremum of
332+
an expression over a set, `argmax` and `argmin` for the set of points where it is taken —
333+
`max(a, b)` of two values is still the larger.
333334
`min(S)` with one argument that is a set is its least member, `min(x, x in S)`, and the range
334335
may carry conditions, `min(x, x in PP and x > 14)` being the least of `{ x in PP : x > 14 }`;
335336
decided for `PP`, `ZZ+`, `ZZ*` and their cuts by an interval or bounds (`min(ZZ+)` is `1`,
@@ -487,6 +488,18 @@ at `c = 0`, where the empty product is `1`. Where the index appears in the body
487488
must be a concrete integer of at least one, `factorial` having no value at the negative integers;
488489
`product(k, k, 0, n)` therefore stays as written.
489490

491+
`sum(body, name in set)` is a sum **over a set**: the body at each member of the set, each member
492+
counted once, added up. `sum(x^2, x in {1, 2, 3})` is `14`. A sum over the roots of a polynomial is a
493+
sum over its set of roots, `sum(w^2, w in { w : w^3 - 2w + 1 = 0 })`, which is `4`, and it needs no
494+
index and no order, a set having neither. The terms are added up where the set is finite and its
495+
members are known: a listed set of numbers, or roots that are rational, roots of a quadratic or roots
496+
of a binomial, which is checked by counting them against the polynomial's number of distinct roots.
497+
The roots of an irreducible cubic or quartic are not written out in radicals, which read no more
498+
simply than the sum. A set whose members might coincide,
499+
such as `{ a, b }`, and a set that is not finite are left as written. Evaluated to a number, roots
500+
the solver cannot write are found numerically, all of them to the working precision or none, so
501+
`sum(w^5, w in { w : w^5 + w + 3 = 0 })` evaluates to `-15`.
502+
490503
**The declared name means the name, and only inside the operator that declares it.** Two names
491504
show this, because both mean something else in this language:
492505

0 commit comments

Comments
 (0)