Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,29 @@ dropped ([#2458](https://github.com/asc-community/AngouriMath/issues/2458)).

---

### A product over a set: `product(f, x in S)`

**Not silent: what was refused now parses.** `product` took a range and nothing else, so `product(f, x in S)` threw
`FunctionArgumentCountException`. It is now the node `ProductOverSetf`, the product of `f` over the members of `S`, each
counted once, built by `MathS.Product(f, x, S)` and printed `\prod_{x \in S} f`. Its factors are multiplied out exactly
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
`1`. The range form is unchanged
([#2449](https://github.com/asc-community/AngouriMath/issues/2449)).

**Silent, for the sum too:** a set of roots with listed members taken out, `{ w : p(w) = 0 } \ { m }`, is now read,
since the product rule over a set writes one. A member taken out is found as it was written; a number whose value is
none of the roots takes nothing out; anything else, a symbol among them, leaves the sum as written.

| Input | Was (2.7.0) | Now |
|---|---|---|
| `"product(p, p in PP /\\ [1; 30])".ToEntity().Simplify()` | throws `FunctionArgumentCountException` | `6469693230` |
| `"product(1 - 1/p^2, p in {2, 3, 5})".ToEntity().Simplify()` | throws `FunctionArgumentCountException` | `16/25` |
| `"product(x, x in {})".ToEntity().Simplify()` | throws `FunctionArgumentCountException` | `1` |
| `"product(x, x in {a, b})".ToEntity().Simplify()` | throws `FunctionArgumentCountException` | `product(x, x in { a, b })`, left |
| `"product(w + y, w in { w : w^3 + w + 1 = 0 })".ToEntity().Differentiate("y")` at `y = 1`, `.Evaled` | throws `FunctionArgumentCountException` | `4` |
| `"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)` |
| `"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` |

### A system row that is not a solution declines the system

**Silent: a wrong row is now no answer.** With `P + Q sqrt(2) = (1 + sqrt(2))^70`, the coefficient `P - Q y` is
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1114,3 +1114,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed

[Tests/UnitTests/Core/ANumberEvaluatesToItselfTest.cs]
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

[Tests/UnitTests/Calculus/ProductOverSetTest.cs]
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
29 changes: 29 additions & 0 deletions Sources/AngouriMath/Convenience/MathS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7899,6 +7899,35 @@ public static Entity Sum(Entity expr, Entity var, Entity from, Entity to)
public static Entity Sum(Entity expr, Entity var, Entity over)
=> new SumOverSetf(expr, var, over);

/// <summary>
/// The product of <paramref name="expr"/> over the members of <paramref name="over"/> as
/// <paramref name="var"/> ranges over it, each member counted once: written
/// <c>product(expr, var in over)</c>. The product over no members is 1.
/// </summary>
/// <param name="expr">The factor, as a function of <paramref name="var"/>.</param>
/// <param name="var">The name that ranges over the set, bound by the product.</param>
/// <param name="over">The set.</param>
/// <returns>
/// The factors multiplied out where <see cref="Sum(Entity, Entity, Entity)"/> would add its
/// terms up: a listed set of numbers, or roots of a polynomial that are rational, roots of
/// a quadratic or roots of a binomial. Left as written otherwise.
/// </returns>
/// <remarks><a href="https://github.com/asc-community/AngouriMath/issues/2449">#2449</a></remarks>
/// <example>
/// <code>
/// using System;
/// using static AngouriMath.MathS;
///
/// Console.WriteLine(Product("p", "p", "PP /\\ [1; 10]").Simplify());
/// </code>
/// Prints
/// <code>
/// 210
/// </code>
/// </example>
public static Entity Product(Entity expr, Entity var, Entity over)
=> new ProductOverSetf(expr, var, over);

/// <summary>
/// The largest value <paramref name="expr"/> takes as <paramref name="var"/> ranges over
/// <paramref name="over"/>, written <c>max(expr, var in over)</c>. Answered over a finite
Expand Down
2 changes: 1 addition & 1 deletion Sources/AngouriMath/Core/Antlr/AngouriMath.g
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ atom returns[Entity value]
| '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); }
| '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); }
| '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]); } }
| '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]); }
| '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]); } }
| 'signum(' args = function_arguments ')' { Assert("signum", 1, $args.list.Count); $value = MathS.Signum($args.list[0]); }
| 'sgn(' args = function_arguments ')' { Assert("sgn", 1, $args.list.Count); $value = MathS.Signum($args.list[0]); }
| 'sign(' args = function_arguments ')' { Assert("sign", 1, $args.list.Count); $value = MathS.Signum($args.list[0]); }
Expand Down
2 changes: 1 addition & 1 deletion Sources/AngouriMath/Core/Antlr/AngouriMathParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4835,7 +4835,7 @@ public AtomContext atom() {
_localctx.args = function_arguments();
State = 932;
Match(T__34);
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]);
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]); }
}
break;
case 101:
Expand Down
8 changes: 8 additions & 0 deletions Sources/AngouriMath/Core/Domains.Classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@ partial record SumOverSetf
internal override Domain DefaultCodomain => Domain.Complex;
}

partial record ProductOverSetf
{
/// <inheritdoc/>
public override Domain Codomain { get; protected init; } = Domain.Complex;
/// <inheritdoc/>
internal override Domain DefaultCodomain => Domain.Complex;
}

partial record Maximumf
{
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ public override Entity Replace(Func<Entity, Entity> func) =>
protected override Entity[] InitDirectChildren() => new[] { Expression, Var, Over };
}

/// <summary>
/// A product over a set: <c>product(f(x), x in S)</c> is the product of <c>f(x)</c> over
/// the members <c>x</c> of <c>S</c>, each counted once. See <see cref="SumOverSetf"/>, which
/// it mirrors: <c>product(p, p in PP /\ [1; 30])</c> is the primorial of 29.
/// https://github.com/asc-community/AngouriMath/issues/2449
/// </summary>
public sealed partial record ProductOverSetf(Entity Expression, Entity Var, Entity Over) : CalculusOperator(Expression, Var)
{
/// <summary>The set the variable ranges over.</summary>
public Entity Over { get; init; } = Binding.Of(Var).In(Over);

private ProductOverSetf New(Entity expression, Entity var, Entity over) =>
ReferenceEquals(Expression, expression) && ReferenceEquals(Var, var) && ReferenceEquals(Over, over)
? this : new(expression, var, over) { Codomain = Codomain };
/// <inheritdoc/>
public override Entity Replace(Func<Entity, Entity> func) =>
func(New(Expression.Replace(func), Var, Over.Replace(func)));
/// <inheritdoc/>
protected override Entity[] InitDirectChildren() => new[] { Expression, Var, Over };
}

/// <summary>
/// A node of limit
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions Sources/AngouriMath/Core/Entity/Entity.Definition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ public IReadOnlyList<Variable> Vars
// A sum over a set binds the name that ranges over the set:
// sum(w * a, w in { w : w^2 = 2 }) is a function of a alone.
SumOverSetf(var body, var bound, var over) => BoundBy(bound, body, over),
ProductOverSetf(var body, var bound, var over) => BoundBy(bound, body, over),
// An extremum over a set binds the variable that ranges over the set:
// max(t^2 + a, t in [0; 1]) is a function of a alone.
Maximumf(var body, var bound, var over) => BoundBy(bound, body, over),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ [EntityJsonConverter] partial record SpecialSet
[EntityJsonConverter] partial record Summationf;
[EntityJsonConverter] partial record Maximumf;
[EntityJsonConverter] partial record SumOverSetf;
[EntityJsonConverter] partial record ProductOverSetf;
[EntityJsonConverter] partial record Minimumf;
[EntityJsonConverter] partial record Argmaxf;
[EntityJsonConverter] partial record Argminf;
Expand Down
4 changes: 3 additions & 1 deletion Sources/AngouriMath/Docs/Usage/Syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,9 @@ A whole `a >= 1` is elementary, `gamma(3, x)` being `2 * e ^ (-x) * (1 + x + x ^

**Calculus** — `derivative(expr, var, order)`, `integral(expr, var)`,
`integral(expr, var, from, to)`, `limit(expr, var, dest)`, `limitleft(...)`, `limitright(...)`;
`sum(expr, var in set)` for a sum over a set; `max(expr, var in set)` and `min` for the extremum of
`sum(expr, var in set)` for a sum over a set and `product(expr, var in set)` for a product over one,
each member counted once (`product(p, p in PP /\ [1; 30])` is `6469693230`, the empty product `1`);
`max(expr, var in set)` and `min` for the extremum of
an expression over a set, `argmax` and `argmin` for the set of points where it is taken —
`max(a, b)` of two values is still the larger.
`min(S)` with one argument that is a set is its least member, `min(x, x in S)`, and the range
Expand Down
44 changes: 39 additions & 5 deletions Sources/AngouriMath/Functions/Algebra/SumOverSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ namespace AngouriMath.Functions
internal static class SumOverSet
{
/// <summary>
/// The sum, or <see langword="null"/> where the members of the set are not known or not
/// finitely many.
/// The sum, or with <paramref name="product"/> the product, or <see langword="null"/> where
/// the members of the set are not known or not finitely many. A product over a set needs
/// its members known and distinct exactly as a sum does, and over no members it is 1 as
/// the sum is 0. https://github.com/asc-community/AngouriMath/issues/2449
/// </summary>
internal static Entity? Value(Entity expression, Entity var, Entity over, bool isExact)
internal static Entity? Value(Entity expression, Entity var, Entity over, bool isExact, bool product = false)
{
if (var is not Variable x || over.InnerSimplified is not Set set)
return null;
Expand All @@ -60,9 +62,9 @@ internal static class SumOverSet
foreach (var member in members)
{
var term = expression.Substitute(x, member);
sum = sum is null ? term : sum + term;
sum = sum is null ? term : product ? sum * term : sum + term;
}
sum ??= Integer.Zero;
sum ??= product ? Integer.One : Integer.Zero;
return isExact ? sum.InnerSimplified : sum.Evaled;
}

Expand All @@ -79,9 +81,40 @@ internal static class SumOverSet
ConditionalSet { Var: Variable w } builder when builder.DeclaredMembership is (Set universe, Equalsf(var left, var right))
=> Roots(left - right, w) is { } roots ? Within(roots, universe) : null,
ConditionalSet { Var: Variable w, Predicate: Equalsf(var left, var right) } => Roots(left - right, w),
SetMinusf(Set from, FiniteSet removed) => Members(from) is { } members ? Without(members, removed) : null,
_ => null,
};

/// <summary>
/// <paramref name="members"/> without the elements of <paramref name="removed"/>, or
/// <see langword="null"/> where it is not decided whether one of those is a member. An
/// element written as a member is that member; one whose value differs from every
/// member's is none of them, compared as <see cref="Distinct"/> compares; and one that
/// agrees in value with a member it is not written as is undecided. The product rule over
/// a set writes <c>S \ { m }</c> with <c>m</c> a member of <c>S</c>, and a member is
/// substituted for <c>m</c> as it was listed, so it is found as written.
/// https://github.com/asc-community/AngouriMath/issues/2449
/// </summary>
private static IReadOnlyList<Entity>? Without(IReadOnlyList<Entity> members, FiniteSet removed)
{
var kept = new List<Entity>(members);
foreach (var element in removed.Elements)
{
var at = kept.IndexOf(element);
if (at >= 0)
{
kept.RemoveAt(at);
continue;
}
if (element.Evaled is not Complex value || !value.IsFinite)
return null;
foreach (var member in kept)
if (member.Evaled is not Complex seen || seen == value)
return null;
}
return kept;
}

/// <summary>
/// The members of <paramref name="members"/> that are in <paramref name="universe"/>, or
/// <see langword="null"/> where that is not decided for one of them.
Expand Down Expand Up @@ -162,6 +195,7 @@ internal static class SumOverSet
ConditionalSet { Var: Variable w } builder when builder.DeclaredMembership is (Set universe, Equalsf(var left, var right))
=> NumericRoots(left - right, w) is { } roots ? Within(roots, universe) : null,
ConditionalSet { Var: Variable w, Predicate: Equalsf(var left, var right) } => NumericRoots(left - right, w),
SetMinusf(Set from, FiniteSet removed) => NumericMembers(from) is { } members ? Without(members, removed) : null,
_ => null,
};

Expand Down
1 change: 1 addition & 0 deletions Sources/AngouriMath/Functions/Compilation/Binders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ internal static void Check(Entity expression)
Summationf => "a sum",
Productf => "a product",
SumOverSetf => "a sum over a set",
ProductOverSetf => "a product over a set",
Maximumf => "a maximum over a set",
Minimumf => "a minimum over a set",
Argmaxf => "the set of points where a maximum is taken",
Expand Down
23 changes: 23 additions & 0 deletions Sources/AngouriMath/Functions/Continuous/Differentiation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,29 @@ protected override Entity InnerDifferentiate(Variable variable) =>
: New(Expression.DerivativeOver(variable), Var, Over);
}

partial record ProductOverSetf
{
/// <inheritdoc/>
/// <remarks>
/// The product rule over finitely many factors, where the set does not depend on the
/// variable: each factor differentiated in turn, times the product of the others,
/// <c>sum(f'(m) product(f(n), n in S \ { m }), m in S)</c>. It holds where a factor is
/// zero, which <c>product(f) sum(f'/f)</c> does not. Zero in the bound name.
/// https://github.com/asc-community/AngouriMath/issues/2449
/// </remarks>
protected override Entity InnerDifferentiate(Variable variable)
{
if (Var == variable)
return Integer.Zero;
if (Over.ContainsNode(variable) || Var is not Variable member)
return base.InnerDifferentiate(variable);
var other = Variable.CreateUnique(this + variable, member.Name);
var others = new ProductOverSetf(Expression.Substitute(member, other), other,
new Set.SetMinusf(Over, new Set.FiniteSet(member)));
return new SumOverSetf(Expression.DerivativeOver(variable) * others, member, Over);
}
}

partial record Limitf
{
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,13 @@ partial record SumOverSetf
null;
}

partial record ProductOverSetf
{
// The unknown sits under a binder; see Summationf below.
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x) =>
null;
}

partial record Maximumf
{
// The unknown sits under a binder; see Summationf below.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,25 @@ protected override Entity InnerSimplify(bool isExact)
?? New(Expression.InnerSimplified, Var, Over.InnerSimplified);
}

partial record ProductOverSetf
{
/// <summary>
/// A product over a set is a well-formed expression whatever the set, as a sum over a set
/// is; multiplying its factors out is an evaluation, not a claim about where it is defined.
/// </summary>
private protected override Entity IntrinsicCondition => Boolean.True;
/// <inheritdoc/>
/// <remarks>
/// The factors are multiplied out where the sum's terms would be added up -- a listed set
/// of numbers, distinct by value, and the roots of a polynomial the solver writes all of
/// -- and the product is left as written, in its simplest parts, where they would not.
/// https://github.com/asc-community/AngouriMath/issues/2449
/// </remarks>
protected override Entity InnerSimplify(bool isExact)
=> Functions.SumOverSet.Value(Expression, Var, Over, isExact, product: true)
?? New(Expression.InnerSimplified, Var, Over.InnerSimplified);
}

// TODO: rewrite this part too
public partial record Summationf
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ private static Entity ScopedToItsBinder(Entity binder, Entity condition)
(Entity Name, Entity? Range)? scope = binder switch
{
SumOverSetf(_, var name, var over) => (name, over),
ProductOverSetf(_, var name, var over) => (name, over),
Maximumf(_, var name, var over) => (name, over),
Minimumf(_, var name, var over) => (name, over),
Argmaxf(_, var name, var over) => (name, over),
Expand Down
Loading
Loading