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
6 changes: 5 additions & 1 deletion BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,10 @@ chain. `min(S)` with one argument that is a set is the least member of the set,
`min(PP)` was `PP` itself — a one-argument `min` was the argument — and `min(x, x in S and P)`
is the least member of `{ x in S : P }`, where it was left as written; both decided for `PP`,
`ZZ+`, `ZZ*` and their cuts by an interval or a bound, so `min(x, x in PP and x > 14)` is the next
prime, `17` ([#1450](https://github.com/asc-community/AngouriMath/issues/1450)).
prime, `17` ([#1450](https://github.com/asc-community/AngouriMath/issues/1450)); and for a condition
that is not a bound by trying the members in order from the least, so Sullivan and Mackey's Prob
8.9.3, the least `n` with `n binomial(n - 1, 2) >= 14`, is `5`
([#1409](https://github.com/asc-community/AngouriMath/issues/1409)).

| Input | Was (2.5.0) | Now |
|---|---|---|
Expand All @@ -918,6 +921,7 @@ prime, `17` ([#1450](https://github.com/asc-community/AngouriMath/issues/1450)).
| `"PP intersect [1; 30]".ToEntity().Evaled` | as written | `{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }` |
| `"min(ZZ+)".ToEntity().Evaled` | `ZZ+` | `1` |
| `"max(x, x in PP and x < 14)".ToEntity().Evaled` | as written | `13` |
| `"min(x, x in ZZ+ and x^2 > 50)".ToEntity().Evaled` | `UnhandledParseException` — `ZZ+` is new since | `8` |
| `"prime(25)".ToEntity().Evaled` | `prime * 25` — juxtaposition of a variable `prime` | `97`; `prime(n)` is the `n`-th prime, `NaN` off the positive whole numbers |
| `"valuation(12, 2)".ToEntity().Evaled` | `UnrecognizedFunctionParseException` | `2`; `valuation(n, p)` is the `p`-adic valuation, `+oo` at `0` and `NaN` off the primes |

Expand Down
36 changes: 35 additions & 1 deletion Sources/AngouriMath/Functions/Continuous/ExtremumOverSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,46 @@ private static (Entity value, List<Entity> points)? Find(Entity expression, Enti
return Integer.Create(first);
}
case ConditionalSet { DeclaredMembership: var (declared, rest), Var: Variable y } when declared.InnerSimplified is Set over:
return BoundsAsInterval(rest, y) is { } bounded ? LeastWholeMember(new Intersectionf(over, bounded)) : null;
return BoundsAsInterval(rest, y) is { } bounded ? LeastWholeMember(new Intersectionf(over, bounded)) : FirstThatHolds(over, y, rest);
default:
return null;
}
}

/// <summary>
/// The least member of the whole numbers <paramref name="over"/> at which
/// <paramref name="condition"/> evaluates to <c>True</c>, found by trying them in order from
/// the least up: every member before it was tried and evaluated to <c>False</c>, so it is the
/// least. Sullivan and Mackey's Prob 8.9.3,
/// <c>min(n, n in { n in ZZ+ : n binomial(n - 1, 2) &gt;= 14 })</c>, is 5. <see langword="null"/>
/// where a member's condition does not evaluate to a truth value, or none of the first
/// <see cref="MaxMembersSearched"/> holds. https://github.com/asc-community/AngouriMath/issues/1409
/// </summary>
private static Integer? FirstThatHolds(Set over, Variable y, Entity condition)
{
if (LeastWholeMember(over) is not { } least || over is not SpecialSet special)
return null;
var prime = special.ToDomain() == Core.Domain.Prime;
EInteger? member = least.EInteger;
for (var tried = 0; tried < MaxMembersSearched && member is { } current; tried++)
{
switch (condition.Substitute(y, Integer.Create(current)).Evaled)
{
case Entity.Boolean(true):
return Integer.Create(current);
case Entity.Boolean(false):
member = prime ? Primes.NextPrime(current + 1) : current + 1;
continue;
default:
return null;
}
}
return null;
}

/// <summary>How many members <see cref="FirstThatHolds"/> tries before declining.</summary>
private const int MaxMembersSearched = 4096;

/// <summary>
/// A conjunction of bounds on <paramref name="y"/> -- <c>y &gt; a</c>, <c>y &gt;= a</c>,
/// <c>a &lt; y</c>, <c>y &lt; b</c> and their kin -- as the interval they cut, or
Expand Down
6 changes: 6 additions & 0 deletions Sources/Tests/UnitTests/Core/Sets/PrimeSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public sealed class PrimeSetTest
[InlineData("min(x, x in ZZ+ and x >= 7/2)", "4")]
[InlineData("min(x, x in PP and x > 14 and x < 20)", "17")]
[InlineData("min(x, 3 < x and x in ZZ+ and x <= 5)", "4")]
// A condition that is not a bound, decided member by member from the least up: Sullivan
// and Mackey's Prob 8.9.3, the least n with n binomial(n - 1, 2) >= 14.
[InlineData("min(n, n in { n in ZZ+ : n * binomial(n - 1, 2) >= 14 })", "5")]
[InlineData("min(x, x in ZZ+ and x^2 > 50)", "8")]
[InlineData("min(p, p in PP and p^2 > 200)", "17")]
// The greatest member, and the previous prime before a bound.
[InlineData("max(x, x in PP and x < 14)", "13")]
[InlineData("max(x, x in PP and x <= 2)", "2")]
Expand Down Expand Up @@ -113,6 +118,7 @@ public void Decided(string input, string expected)
[InlineData("max(x, x in PP and x < 2)", typeof(Maximumf))]
[InlineData("max(x, x in ZZ+ and x < 1)", typeof(Maximumf))]
[InlineData("min(x, x in PP and x > 14 and x < 16)", typeof(Minimumf))]
[InlineData("min(x, x in ZZ+ and x^2 < 0)", typeof(Minimumf))]
[InlineData("PP intersect [1; 10^7]", typeof(Set.Intersectionf))]
[InlineData("prime(n)", typeof(Primef))]
[InlineData("prime(10^9)", typeof(Primef))]
Expand Down
Loading