Skip to content

Commit d5f5c04

Browse files
The least member of a set is found by search where its condition is not a bound (#1554)
min(x, x in S and P) was decided only where P is a bound. Where it is not, the members of ZZ+, ZZ* or PP are tried in order from the least, and the first whose condition evaluates to True is the least: Sullivan and Mackey's Prob 8.9.3, the least n with n binomial(n - 1, 2) >= 14, is 5. A condition that does not evaluate to a truth value, or none of the first 4096 members holding, leaves it as written. Part of #1409. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 5f9e3db commit d5f5c04

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,10 @@ chain. `min(S)` with one argument that is a set is the least member of the set,
919919
`min(PP)` was `PP` itself — a one-argument `min` was the argument — and `min(x, x in S and P)`
920920
is the least member of `{ x in S : P }`, where it was left as written; both decided for `PP`,
921921
`ZZ+`, `ZZ*` and their cuts by an interval or a bound, so `min(x, x in PP and x > 14)` is the next
922-
prime, `17` ([#1450](https://github.com/asc-community/AngouriMath/issues/1450)).
922+
prime, `17` ([#1450](https://github.com/asc-community/AngouriMath/issues/1450)); and for a condition
923+
that is not a bound by trying the members in order from the least, so Sullivan and Mackey's Prob
924+
8.9.3, the least `n` with `n binomial(n - 1, 2) >= 14`, is `5`
925+
([#1409](https://github.com/asc-community/AngouriMath/issues/1409)).
923926

924927
| Input | Was (2.5.0) | Now |
925928
|---|---|---|
@@ -931,6 +934,7 @@ prime, `17` ([#1450](https://github.com/asc-community/AngouriMath/issues/1450)).
931934
| `"PP intersect [1; 30]".ToEntity().Evaled` | as written | `{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }` |
932935
| `"min(ZZ+)".ToEntity().Evaled` | `ZZ+` | `1` |
933936
| `"max(x, x in PP and x < 14)".ToEntity().Evaled` | as written | `13` |
937+
| `"min(x, x in ZZ+ and x^2 > 50)".ToEntity().Evaled` | `UnhandledParseException` — `ZZ+` is new since | `8` |
934938
| `"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 |
935939
| `"valuation(12, 2)".ToEntity().Evaled` | `UnrecognizedFunctionParseException` | `2`; `valuation(n, p)` is the `p`-adic valuation, `+oo` at `0` and `NaN` off the primes |
936940

‎Sources/AngouriMath/Functions/Continuous/ExtremumOverSet.cs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,46 @@ private static (Entity value, List<Entity> points)? Find(Entity expression, Enti
161161
return Integer.Create(first);
162162
}
163163
case ConditionalSet { DeclaredMembership: var (declared, rest), Var: Variable y } when declared.InnerSimplified is Set over:
164-
return BoundsAsInterval(rest, y) is { } bounded ? LeastWholeMember(new Intersectionf(over, bounded)) : null;
164+
return BoundsAsInterval(rest, y) is { } bounded ? LeastWholeMember(new Intersectionf(over, bounded)) : FirstThatHolds(over, y, rest);
165165
default:
166166
return null;
167167
}
168168
}
169169

170+
/// <summary>
171+
/// The least member of the whole numbers <paramref name="over"/> at which
172+
/// <paramref name="condition"/> evaluates to <c>True</c>, found by trying them in order from
173+
/// the least up: every member before it was tried and evaluated to <c>False</c>, so it is the
174+
/// least. Sullivan and Mackey's Prob 8.9.3,
175+
/// <c>min(n, n in { n in ZZ+ : n binomial(n - 1, 2) &gt;= 14 })</c>, is 5. <see langword="null"/>
176+
/// where a member's condition does not evaluate to a truth value, or none of the first
177+
/// <see cref="MaxMembersSearched"/> holds. https://github.com/asc-community/AngouriMath/issues/1409
178+
/// </summary>
179+
private static Integer? FirstThatHolds(Set over, Variable y, Entity condition)
180+
{
181+
if (LeastWholeMember(over) is not { } least || over is not SpecialSet special)
182+
return null;
183+
var prime = special.ToDomain() == Core.Domain.Prime;
184+
EInteger? member = least.EInteger;
185+
for (var tried = 0; tried < MaxMembersSearched && member is { } current; tried++)
186+
{
187+
switch (condition.Substitute(y, Integer.Create(current)).Evaled)
188+
{
189+
case Entity.Boolean(true):
190+
return Integer.Create(current);
191+
case Entity.Boolean(false):
192+
member = prime ? Primes.NextPrime(current + 1) : current + 1;
193+
continue;
194+
default:
195+
return null;
196+
}
197+
}
198+
return null;
199+
}
200+
201+
/// <summary>How many members <see cref="FirstThatHolds"/> tries before declining.</summary>
202+
private const int MaxMembersSearched = 4096;
203+
170204
/// <summary>
171205
/// A conjunction of bounds on <paramref name="y"/> -- <c>y &gt; a</c>, <c>y &gt;= a</c>,
172206
/// <c>a &lt; y</c>, <c>y &lt; b</c> and their kin -- as the interval they cut, or

‎Sources/Tests/UnitTests/Core/Sets/PrimeSetTest.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public sealed class PrimeSetTest
6767
[InlineData("min(x, x in ZZ+ and x >= 7/2)", "4")]
6868
[InlineData("min(x, x in PP and x > 14 and x < 20)", "17")]
6969
[InlineData("min(x, 3 < x and x in ZZ+ and x <= 5)", "4")]
70+
// A condition that is not a bound, decided member by member from the least up: Sullivan
71+
// and Mackey's Prob 8.9.3, the least n with n binomial(n - 1, 2) >= 14.
72+
[InlineData("min(n, n in { n in ZZ+ : n * binomial(n - 1, 2) >= 14 })", "5")]
73+
[InlineData("min(x, x in ZZ+ and x^2 > 50)", "8")]
74+
[InlineData("min(p, p in PP and p^2 > 200)", "17")]
7075
// The greatest member, and the previous prime before a bound.
7176
[InlineData("max(x, x in PP and x < 14)", "13")]
7277
[InlineData("max(x, x in PP and x <= 2)", "2")]
@@ -113,6 +118,7 @@ public void Decided(string input, string expected)
113118
[InlineData("max(x, x in PP and x < 2)", typeof(Maximumf))]
114119
[InlineData("max(x, x in ZZ+ and x < 1)", typeof(Maximumf))]
115120
[InlineData("min(x, x in PP and x > 14 and x < 16)", typeof(Minimumf))]
121+
[InlineData("min(x, x in ZZ+ and x^2 < 0)", typeof(Minimumf))]
116122
[InlineData("PP intersect [1; 10^7]", typeof(Set.Intersectionf))]
117123
[InlineData("prime(n)", typeof(Primef))]
118124
[InlineData("prime(10^9)", typeof(Primef))]

0 commit comments

Comments
 (0)