Skip to content

Commit 3abd4d9

Browse files
A product over a set: product(f, x in S) (#2467)
product took a range and nothing else, so product(f, x in S) threw an arity error where sum(f, x in S) worked. ProductOverSetf is the sum over a set's counterpart: the variable is bound over the factor and the set (FreeVariables, Substitute, DomainCondition scoping), it prints product(f, x in S) and \prod_{x \in S} f, MathS.Product(f, x, S) builds it, and the compilers refuse it as they refuse the sum. Its factors are multiplied out exactly where the sum adds its terms up -- a listed set of numbers distinct by value, the roots of a polynomial the solver writes all of, numerically the Durand-Kerner roots, and the interval evaluator's enclosed roots -- and it is left as written where the sum is. The empty product is 1. SymPy export as a Mul over the listed members or the CRootOf roots of the square-free part. product(p, p in PP /\ [1; 30]) is 6469693230 and product(1 - 1/p^2, p in {2, 3, 5}) is 16/25. The derivative is the product rule over the set, sum(f'(m) product(f(n), n in S \ { m }), m in S), which holds where a factor is zero. For that to evaluate over roots, a set of roots with listed members taken out is now read by the sum and the product alike: a member taken out is found as written, a number whose value is none of them takes nothing out, and anything else is undecided. That changes sum(x, x in { w : w^2 - 2 = 0 } \ { sqrt(2) }) from unevaluated to -sqrt(2); recorded in BREAKING-CHANGES.md with the parse error that became an answer, as #2455 recorded its refusals. Grammar: one action, the product( rule, takes the sum( rule's two-argument branch; the unmodified grammar regenerated to an empty diff first. Part of #2449 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dd5cc83 commit 3abd4d9

26 files changed

Lines changed: 433 additions & 13 deletions

‎BREAKING-CHANGES.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,29 @@ dropped ([#2458](https://github.com/asc-community/AngouriMath/issues/2458)).
240240

241241
---
242242

243+
### A product over a set: `product(f, x in S)`
244+
245+
**Not silent: what was refused now parses.** `product` took a range and nothing else, so `product(f, x in S)` threw
246+
`FunctionArgumentCountException`. It is now the node `ProductOverSetf`, the product of `f` over the members of `S`, each
247+
counted once, built by `MathS.Product(f, x, S)` and printed `\prod_{x \in S} f`. Its factors are multiplied out exactly
248+
where `sum(f, x in S)` adds its terms up, and it is left as written where the sum is; the product over no members is
249+
`1`. The range form is unchanged
250+
([#2449](https://github.com/asc-community/AngouriMath/issues/2449)).
251+
252+
**Silent, for the sum too:** a set of roots with listed members taken out, `{ w : p(w) = 0 } \ { m }`, is now read,
253+
since the product rule over a set writes one. A member taken out is found as it was written; a number whose value is
254+
none of the roots takes nothing out; anything else, a symbol among them, leaves the sum as written.
255+
256+
| Input | Was (2.7.0) | Now |
257+
|---|---|---|
258+
| `"product(p, p in PP /\\ [1; 30])".ToEntity().Simplify()` | throws `FunctionArgumentCountException` | `6469693230` |
259+
| `"product(1 - 1/p^2, p in {2, 3, 5})".ToEntity().Simplify()` | throws `FunctionArgumentCountException` | `16/25` |
260+
| `"product(x, x in {})".ToEntity().Simplify()` | throws `FunctionArgumentCountException` | `1` |
261+
| `"product(x, x in {a, b})".ToEntity().Simplify()` | throws `FunctionArgumentCountException` | `product(x, x in { a, b })`, left |
262+
| `"product(w + y, w in { w : w^3 + w + 1 = 0 })".ToEntity().Differentiate("y")` at `y = 1`, `.Evaled` | throws `FunctionArgumentCountException` | `4` |
263+
| `"sum(x, x in { w : w^2 - 2 = 0 } \\ { sqrt(2) })".ToEntity().Simplify()` | `sum(x, x in { w : w ^ 2 - 2 = 0 } \ { sqrt(2) })` | `-sqrt(2)` |
264+
| `"sum(x, x in { w : w^2 - 3w + 2 = 0 } \\ { 1 })".ToEntity().Simplify()` | `sum(x, x in { w : w ^ 2 - 3 * w + 2 = 0 } \ { 1 })` | `2` |
265+
243266
### A system row that is not a solution declines the system
244267

245268
**Silent: a wrong row is now no answer.** With `P + Q sqrt(2) = (1 + sqrt(2))^70`, the coefficient `P - Q y` is

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,3 +1114,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
11141114

11151115
[Tests/UnitTests/Core/ANumberEvaluatesToItselfTest.cs]
11161116
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
1117+
1118+
[Tests/UnitTests/Calculus/ProductOverSetTest.cs]
1119+
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

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7899,6 +7899,35 @@ public static Entity Sum(Entity expr, Entity var, Entity from, Entity to)
78997899
public static Entity Sum(Entity expr, Entity var, Entity over)
79007900
=> new SumOverSetf(expr, var, over);
79017901

7902+
/// <summary>
7903+
/// The product of <paramref name="expr"/> over the members of <paramref name="over"/> as
7904+
/// <paramref name="var"/> ranges over it, each member counted once: written
7905+
/// <c>product(expr, var in over)</c>. The product over no members is 1.
7906+
/// </summary>
7907+
/// <param name="expr">The factor, as a function of <paramref name="var"/>.</param>
7908+
/// <param name="var">The name that ranges over the set, bound by the product.</param>
7909+
/// <param name="over">The set.</param>
7910+
/// <returns>
7911+
/// The factors multiplied out where <see cref="Sum(Entity, Entity, Entity)"/> would add its
7912+
/// terms up: a listed set of numbers, or roots of a polynomial that are rational, roots of
7913+
/// a quadratic or roots of a binomial. Left as written otherwise.
7914+
/// </returns>
7915+
/// <remarks><a href="https://github.com/asc-community/AngouriMath/issues/2449">#2449</a></remarks>
7916+
/// <example>
7917+
/// <code>
7918+
/// using System;
7919+
/// using static AngouriMath.MathS;
7920+
///
7921+
/// Console.WriteLine(Product("p", "p", "PP /\\ [1; 10]").Simplify());
7922+
/// </code>
7923+
/// Prints
7924+
/// <code>
7925+
/// 210
7926+
/// </code>
7927+
/// </example>
7928+
public static Entity Product(Entity expr, Entity var, Entity over)
7929+
=> new ProductOverSetf(expr, var, over);
7930+
79027931
/// <summary>
79037932
/// The largest value <paramref name="expr"/> takes as <paramref name="var"/> ranges over
79047933
/// <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
@@ -597,7 +597,7 @@ atom returns[Entity value]
597597
| '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); }
598598
| '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); }
599599
| '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]); } }
600-
| '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]); }
600+
| 'product(' args = function_arguments ')' { if ($args.list.Count == 2 && AngouriMath.Functions.ExtremumOverSet.AsRange($args.list[1]) is var (productVar, productOver)) $value = MathS.Product($args.list[0], productVar, productOver); else { Assert("product", 4, $args.list.Count); $value = MathS.Product($args.list[0], $args.list[1], $args.list[2], $args.list[3]); } }
601601
| 'signum(' args = function_arguments ')' { Assert("signum", 1, $args.list.Count); $value = MathS.Signum($args.list[0]); }
602602
| 'sgn(' args = function_arguments ')' { Assert("sgn", 1, $args.list.Count); $value = MathS.Signum($args.list[0]); }
603603
| 'sign(' args = function_arguments ')' { Assert("sign", 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
@@ -4835,7 +4835,7 @@ public AtomContext atom() {
48354835
_localctx.args = function_arguments();
48364836
State = 932;
48374837
Match(T__34);
4838-
Assert("product", 4, _localctx.args.list.Count); _localctx.value = MathS.Product(_localctx.args.list[0], _localctx.args.list[1], _localctx.args.list[2], _localctx.args.list[3]);
4838+
if (_localctx.args.list.Count == 2 && AngouriMath.Functions.ExtremumOverSet.AsRange(_localctx.args.list[1]) is var (productVar, productOver)) _localctx.value = MathS.Product(_localctx.args.list[0], productVar, productOver); else { Assert("product", 4, _localctx.args.list.Count); _localctx.value = MathS.Product(_localctx.args.list[0], _localctx.args.list[1], _localctx.args.list[2], _localctx.args.list[3]); }
48394839
}
48404840
break;
48414841
case 101:

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,14 @@ partial record SumOverSetf
400400
internal override Domain DefaultCodomain => Domain.Complex;
401401
}
402402

403+
partial record ProductOverSetf
404+
{
405+
/// <inheritdoc/>
406+
public override Domain Codomain { get; protected init; } = Domain.Complex;
407+
/// <inheritdoc/>
408+
internal override Domain DefaultCodomain => Domain.Complex;
409+
}
410+
403411
partial record Maximumf
404412
{
405413
/// <inheritdoc/>

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,27 @@ public override Entity Replace(Func<Entity, Entity> func) =>
142142
protected override Entity[] InitDirectChildren() => new[] { Expression, Var, Over };
143143
}
144144

145+
/// <summary>
146+
/// A product over a set: <c>product(f(x), x in S)</c> is the product of <c>f(x)</c> over
147+
/// the members <c>x</c> of <c>S</c>, each counted once. See <see cref="SumOverSetf"/>, which
148+
/// it mirrors: <c>product(p, p in PP /\ [1; 30])</c> is the primorial of 29.
149+
/// https://github.com/asc-community/AngouriMath/issues/2449
150+
/// </summary>
151+
public sealed partial record ProductOverSetf(Entity Expression, Entity Var, Entity Over) : CalculusOperator(Expression, Var)
152+
{
153+
/// <summary>The set the variable ranges over.</summary>
154+
public Entity Over { get; init; } = Binding.Of(Var).In(Over);
155+
156+
private ProductOverSetf New(Entity expression, Entity var, Entity over) =>
157+
ReferenceEquals(Expression, expression) && ReferenceEquals(Var, var) && ReferenceEquals(Over, over)
158+
? this : new(expression, var, over) { Codomain = Codomain };
159+
/// <inheritdoc/>
160+
public override Entity Replace(Func<Entity, Entity> func) =>
161+
func(New(Expression.Replace(func), Var, Over.Replace(func)));
162+
/// <inheritdoc/>
163+
protected override Entity[] InitDirectChildren() => new[] { Expression, Var, Over };
164+
}
165+
145166
/// <summary>
146167
/// A node of limit
147168
/// </summary>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ public IReadOnlyList<Variable> Vars
697697
// A sum over a set binds the name that ranges over the set:
698698
// sum(w * a, w in { w : w^2 = 2 }) is a function of a alone.
699699
SumOverSetf(var body, var bound, var over) => BoundBy(bound, body, over),
700+
ProductOverSetf(var body, var bound, var over) => BoundBy(bound, body, over),
700701
// An extremum over a set binds the variable that ranges over the set:
701702
// max(t^2 + a, t in [0; 1]) is a function of a alone.
702703
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
@@ -159,6 +159,7 @@ [EntityJsonConverter] partial record SpecialSet
159159
[EntityJsonConverter] partial record Summationf;
160160
[EntityJsonConverter] partial record Maximumf;
161161
[EntityJsonConverter] partial record SumOverSetf;
162+
[EntityJsonConverter] partial record ProductOverSetf;
162163
[EntityJsonConverter] partial record Minimumf;
163164
[EntityJsonConverter] partial record Argmaxf;
164165
[EntityJsonConverter] partial record Argminf;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,9 @@ A whole `a >= 1` is elementary, `gamma(3, x)` being `2 * e ^ (-x) * (1 + x + x ^
470470

471471
**Calculus** — `derivative(expr, var, order)`, `integral(expr, var)`,
472472
`integral(expr, var, from, to)`, `limit(expr, var, dest)`, `limitleft(...)`, `limitright(...)`;
473-
`sum(expr, var in set)` for a sum over a set; `max(expr, var in set)` and `min` for the extremum of
473+
`sum(expr, var in set)` for a sum over a set and `product(expr, var in set)` for a product over one,
474+
each member counted once (`product(p, p in PP /\ [1; 30])` is `6469693230`, the empty product `1`);
475+
`max(expr, var in set)` and `min` for the extremum of
474476
an expression over a set, `argmax` and `argmin` for the set of points where it is taken —
475477
`max(a, b)` of two values is still the larger.
476478
`min(S)` with one argument that is a set is its least member, `min(x, x in S)`, and the range

0 commit comments

Comments
 (0)