diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 2d8c7ac2b..15d44781b 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -1283,10 +1283,14 @@ and every unit is the residue of infinitely many primes, by Dirichlet's theorem. `forall p in PP : p > 3 implies 24 divides p^2 - 1` is `True` while the same from `p > 2` is `False` at `3`, Sullivan and Mackey's Prob 6.7.5 and its erratum, and `exists p in PP : p > 10 and p = 3 (mod 4)` is `True` ([#1409](https://github.com/asc-community/AngouriMath/issues/1409)). +A comparison keeps one truth value past its root, so the primes below the last root are asked +one by one, and a comparison may stand anywhere in the statement: every prime is `2`, or `1` or `3` +modulo `4`. | Input | Was (2.5.0) | Now | |---|---|---| | `forall p in PP : p > 3 implies 24 divides p^2 - 1` | `UnhandledParseException` (left as written when `PP` arrived) | `True` | +| `forall p in PP : p = 1 (mod 4) or p = 3 (mod 4) or p = 2` | `UnhandledParseException` (left as written when `PP` arrived) | `True` | ### `atanh(tanh(a + b x))` under an integral is linear in `x`, and was an atom no rule read diff --git a/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs b/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs index 99e45226e..c4bc8a3fe 100644 --- a/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs +++ b/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs @@ -310,10 +310,14 @@ private static Entity Quantified(Kind kind, Entity var, Entity over, Entity body // the residue of infinitely many primes (Dirichlet), so a statement that repeats modulo // M is decided over the primes by M's own prime factors, one by one, and by the units // modulo M: p^2 - 1 is a multiple of 24 for every prime past 3, though not for 3 -- - // Sullivan and Mackey's Prob 6.7.5. https://github.com/asc-community/AngouriMath/issues/1409 - if (set is SpecialSet.Primes && kind != Kind.Unique && OverThePrimes(kind, x, body, isExact) is { } overThePrimes) - { - by = ("the primes past the modulus's prime factors are its units, and each unit is the residue of infinitely many primes", "Nat.setOf_prime_and_eq_mod_infinite"); + // Sullivan and Mackey's Prob 6.7.5. A comparison keeps one truth value past its root, + // so the primes below the last root are asked one by one: every prime is 2, or 1 or 3 + // modulo 4. https://github.com/asc-community/AngouriMath/issues/1409 + if (set is SpecialSet.Primes && kind != Kind.Unique && OverThePrimes(kind, x, body, isExact) is var (overThePrimes, threshold)) + { + by = (threshold.CompareTo(EInteger.FromInt32(2)) > 0 + ? $"the primes below {threshold} one by one; from {threshold} the comparisons have settled, the primes past the modulus's prime factors are its units, and each unit is the residue of infinitely many primes" + : "the primes past the modulus's prime factors are its units, and each unit is the residue of infinitely many primes", "Nat.setOf_prime_and_eq_mod_infinite"); return overThePrimes; } if (set is FiniteSet finite) @@ -969,22 +973,44 @@ private static (EInteger Period, EInteger Threshold)? Settled(Entity body, Varia any = true; continue; } - if (LinearComparison(atom, x) is not (var slope, var rest) || rest.Evaled is not Real { IsNaN: false } intercept - || !slope.IsFinite) + if (SettlesAt(atom, x) is not { } settlesAt) return null; any = true; - // Flat, or against an infinity -- x < +oo -- it has one truth value throughout. - if (slope.IsZero || !intercept.IsFinite) - continue; - // Past floor(-b / a) + 1 the sign of a x + b no longer changes. - var root = intercept.EDecimal.Negate().Divide(slope.EDecimal, PeterO.Numbers.EContext.ForPrecision(64)); - var settlesAt = root.RoundToExponent(0, PeterO.Numbers.ERounding.Floor).ToEInteger().Add(EInteger.One); if (settlesAt.CompareTo(threshold) > 0) threshold = settlesAt; } return any ? (period, threshold) : null; } + /// + /// The whole number from which a comparison linear in , with numbers + /// for coefficients, keeps one truth value, or for an atom that is + /// not one: past its root the sign of a x + b no longer changes, so from + /// floor(-b / a) + 1 on. Zero for a comparison that never changes. + /// + private static EInteger? SettlesAt(Entity atom, Variable x) + { + if (LinearComparison(atom, x) is not (var slope, var rest) || rest.Evaled is not Real { IsNaN: false } intercept + || !slope.IsFinite) + return null; + // Flat, or against an infinity -- x < +oo -- it has one truth value throughout. + if (slope.IsZero || !intercept.IsFinite) + return EInteger.Zero; + // Exactly where both are rational, so that where it settles does not depend on the + // decimal precision: at 16 or 30 digits 2/3 rounds up, which put the root of 2/3 n - 2 + // below 3, and 3 was taken for a member past it. + if (slope is Rational { ERational: var a } && intercept is Rational { ERational: var b }) + { + var root = b.Negate().Divide(a); + var floor = root.ToEInteger(); + if (root.CompareTo(ERational.FromEInteger(floor)) < 0) + floor -= EInteger.One; + return floor.Add(EInteger.One); + } + var approximated = intercept.EDecimal.Negate().Divide(slope.EDecimal, EContext.ForPrecision(64)); + return approximated.RoundToExponent(0, ERounding.Floor).ToEInteger().Add(EInteger.One); + } + /// /// The statement decided at every member from up to, and not /// including, . @@ -1069,36 +1095,49 @@ private static Entity OneOfTheFirst(Variable x, EInteger from, int count, Entity return any ?? Entity.Boolean.False; } - /// - /// The period of the body in over the whole numbers, where it has - /// one: a divisibility by a whole number, or a congruence modulo one, of polynomials - /// with whole coefficients, joined by the connectives; the least common multiple of the - /// parts' periods. + /// The statement over the primes, where past a threshold it repeats modulo some M, + /// and the threshold, or where what is left does not repeat or a + /// member does not decide it. A comparison linear in keeps one truth + /// value past its root, so the primes below the last root are asked one by one. Past it + /// the statement is its value at each prime factor of M there, and at each unit + /// modulo M, which stands for the infinitely many primes of its class there. /// - /// - /// The statement over the primes, where past a lower bound on them it repeats modulo some - /// M: at each prime factor of M past the bound, and at each unit modulo - /// M, which stands for the infinitely many primes of its class. - /// where the statement does not repeat, or a residue does not decide it. - /// - private static Entity? OverThePrimes(Kind kind, Variable x, Entity body, bool isExact) + private static (Entity Verdict, EInteger Threshold)? OverThePrimes(Kind kind, Variable x, Entity body, bool isExact) { - // The primes asked about: those past a bound the hypothesis sets, or all of them. - var above = EInteger.One; - var claim = body; - if (kind == Kind.All && body is Impliesf(var assumption, var conclusion) && LowerBound(assumption, x) is { } bound) - (above, claim) = (bound, conclusion); - else if (kind == Kind.Some && body is Andf(var first, var second)) - { - if (LowerBound(first, x) is { } firstBound) - (above, claim) = (firstBound, second); - else if (LowerBound(second, x) is { } secondBound) - (above, claim) = (secondBound, first); - } - if (!claim.ContainsNode(x) || Period(claim, x, (SpecialSet)MathS.Sets.Z) is not { } modulus - || modulus.CompareTo(EInteger.FromInt32(LargestPeriod)) > 0) + // The comparisons, and the least number from which every one of them has settled. + var atoms = new List(); + CollectAtoms(body, atoms); + var two = EInteger.FromInt32(2); + var threshold = two; + var comparisons = new List(); + foreach (var atom in atoms) + if (atom.ContainsNode(x) && SettlesAt(atom, x) is { } settlesAt) + { + comparisons.Add(atom); + if (settlesAt.CompareTo(threshold) > 0) + threshold = settlesAt; + } + if (threshold.CompareTo(EInteger.FromInt32(LargestPrefix)) > 0) return null; + // From the threshold on, each comparison is the truth value it has there. + var settled = new Dictionary(); + foreach (var comparison in comparisons) + { + if (comparison.Substitute(x, Integer.Create(threshold)).Evaled is not Entity.Boolean value) + return null; + settled[comparison] = value; + } + var past = settled.Count == 0 ? body + : body.Replace(node => settled.TryGetValue(node, out var value) ? value : node).InnerSimplified(isExact); + // What is left repeats, or is one truth value, which repeats modulo 1. + var modulus = EInteger.One; + if (past.ContainsNode(x)) + { + if (Period(past, x, (SpecialSet)MathS.Sets.Z) is not { } period || period.CompareTo(EInteger.FromInt32(LargestPeriod)) > 0) + return null; + modulus = period; + } int holds = 0, fails = 0, undecided = 0; void Tell(Entity verdict) { @@ -1109,9 +1148,13 @@ void Tell(Entity verdict) default: undecided++; break; } } - // The prime factors of M past the bound, which are not units modulo it. + // The primes below the threshold, one by one. + for (var prime = two; prime.CompareTo(threshold) < 0; prime += 1) + if (Functions.Primes.IsPrime(prime) == true) + Tell(body.Substitute(x, Integer.Create(prime)).InnerSimplified(isExact)); + // The prime factors of M from the threshold on, which are not units modulo it. var rest = modulus; - for (var factor = EInteger.FromInt32(2); rest.CompareTo(EInteger.One) > 0; factor += 1) + for (var factor = two; rest.CompareTo(EInteger.One) > 0; factor += 1) { // Past the square root of what is left, what is left is prime. if (factor.Multiply(factor).CompareTo(rest) > 0) @@ -1120,31 +1163,24 @@ void Tell(Entity verdict) continue; while (rest.Remainder(factor).IsZero) rest = rest.Divide(factor); - if (factor.CompareTo(above) > 0) - Tell(claim.Substitute(x, Integer.Create(factor)).InnerSimplified(isExact)); + if (factor.CompareTo(threshold) >= 0) + Tell(past.Substitute(x, Integer.Create(factor)).InnerSimplified(isExact)); } - // The units modulo M, each the residue of primes past any bound. + // The units modulo M, each the residue of infinitely many primes past the threshold. for (var residue = EInteger.One; residue.CompareTo(modulus) <= 0; residue += 1) if (residue.Gcd(modulus).Equals(EInteger.One)) - Tell(claim.Substitute(x, Integer.Create(residue)).InnerSimplified(isExact)); - return Tally(kind, holds, fails, undecided, distinct: true); + Tell(past.Substitute(x, Integer.Create(residue)).InnerSimplified(isExact)); + if (Tally(kind, holds, fails, undecided, distinct: true) is not { } verdict) + return null; + return (verdict, threshold); } /// - /// The whole number every satisfying is - /// past, where the condition is a lower bound on it: x > 3 is 3, - /// x >= 5 is 4. otherwise. + /// The period of the body in over the whole numbers, where it has + /// one: a divisibility by a whole number, or a congruence modulo one, of polynomials + /// with whole coefficients, joined by the connectives; the least common multiple of the + /// parts' periods. /// - private static EInteger? LowerBound(Entity condition, Variable x) - => condition switch - { - Greaterf(var name, Integer { EInteger: var past }) when name == x => past, - GreaterOrEqualf(var name, Integer { EInteger: var from }) when name == x => from.Subtract(EInteger.One), - Lessf(Integer { EInteger: var past }, var name) when name == x => past, - LessOrEqualf(Integer { EInteger: var from }, var name) when name == x => from.Subtract(EInteger.One), - _ => null, - }; - private static EInteger? Period(Entity body, Variable x, SpecialSet set) { switch (body) diff --git a/Sources/Tests/UnitTests/Core/QuantifierTest.cs b/Sources/Tests/UnitTests/Core/QuantifierTest.cs index b8e9f2100..c355c4c30 100644 --- a/Sources/Tests/UnitTests/Core/QuantifierTest.cs +++ b/Sources/Tests/UnitTests/Core/QuantifierTest.cs @@ -104,6 +104,9 @@ public void DecidedOverAFiniteSet(string statement, string expected) [InlineData("forall n in ZZ : n >= 14 implies (exists a in ZZ* : exists b in ZZ* : n = 3 a + 8 b)", "True")] [InlineData("forall n in ZZ : n >= 24 implies (exists a in ZZ* : exists b in ZZ* : n = 4 a + 9 b)", "True")] [InlineData("forall n in ZZ : n >= 23 implies (exists a in ZZ* : exists b in ZZ* : n = 4 a + 9 b)", "False")] + // A root that is a whole number is found exactly: 2/3 n - 2 is 0 at 3 and positive from 4. + [InlineData("forall n in ZZ* : 2/3 n - 2 <= 0", "False")] + [InlineData("exists n in ZZ* : 2/3 n - 2 > 0 and 2 divides n + 1", "True")] // Floors and ceilings of n over a whole number beside n itself repeat where the drifts // cancel, and the residues decide them: floor(n/2) + ceil(n/2) = n, and Hermite's // identity for thirds. @@ -138,6 +141,17 @@ public void DecidedOverAFiniteSet(string statement, string expected) [InlineData("forall p in PP : p > 2 implies 2 divides p + 1", "True")] [InlineData("exists p in PP : p > 3 and 3 divides p", "False")] [InlineData("exists p in PP : p > 10 and p = 3 (mod 4)", "True")] + // A comparison keeps one truth value past its root, so the primes below the last root are + // asked one by one and the rest by their residues: every prime is 2, or 1 or 3 modulo 4, + // and every prime but 2 and 3 is 1 or 5 modulo 6. + [InlineData("forall p in PP : p = 1 (mod 4) or p = 3 (mod 4) or p = 2", "True")] + [InlineData("forall p in PP : p = 1 (mod 4) or p = 3 (mod 4)", "False")] + [InlineData("forall p in PP : p = 2 or 2 divides p + 1", "True")] + [InlineData("forall p in PP : p = 2 or p = 3 or 6 divides p^2 - 1", "True")] + [InlineData("forall p in PP : p >= 5 implies p = 1 (mod 6) or p = 5 (mod 6)", "True")] + [InlineData("forall p in PP : p < 10 implies p = 2 or 2 divides p + 1", "True")] + [InlineData("exists p in PP : p > 2 and 2 divides p", "False")] + [InlineData("exists p in PP : p < 3 and p = 2 (mod 3)", "True")] // Sullivan and Mackey's Prob 4.11.6: for whole x and y the witnesses are x - y and y - x, // and one of them is not negative. Not so in ZZ+, where x = y leaves 0, nor for reals, // where 1/2 and 0 leave ±1/2. @@ -198,6 +212,22 @@ public void DecidedOverAnInfiniteSet(string statement, string expected) public void LeftAsWrittenWhereNothingDecidesIt(string statement) => Assert.Equal(statement.ToEntity(), statement.ToEntity().Simplify()); + /// + /// Where a comparison with rational coefficients settles is found exactly, so the verdict + /// does not depend on the decimal precision: at 16 or 30 digits 2/3 rounds up, which put + /// the root of 2/3 n - 2 below 3, and the statement was decided as though 3 were past it. + /// + [Theory] + [InlineData(16)] + [InlineData(30)] + [InlineData(100)] + public void WhereAComparisonSettlesDoesNotDependOnThePrecision(int digits) + { + using var _ = MathS.Settings.DecimalPrecisionContext.Set(new PeterO.Numbers.EContext(digits, PeterO.Numbers.ERounding.HalfUp, -digits, 1000, false)); + Assert.Equal(Entity.Boolean.False, MathS.FromString("forall n in ZZ* : 2/3 n - 2 <= 0", useCache: false).Simplify()); + Assert.Equal(Entity.Boolean.False, MathS.FromString("forall n in ZZ* : 5/9 n - 5 <= 0", useCache: false).Simplify()); + } + [Fact] public void AWholeNumberIsNotAssumedOfAReal() // binomial(p, k) of a real k is not a whole number, so the rule for divisibility has