diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 1d4a3c545..9613a2ae6 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -1501,6 +1501,28 @@ that came out in the expanded square before now come out in `x^2 + a/b`. Rubi's 1.2.2.2, 1.2.2.4, 1.2.2.7 and 1.2.3.2, the 574 problems that count with a perfect square written with symbols: 388 to 409, no row lost. +### A quantifier over the whole numbers reads the bounds in its hypothesis, and residues written with `mod` + +`forall k in ZZ : (0 < k and k < 7) implies 7 divides binomial(7, k)` was left as written, because no +route read a bound on the name out of an implication's hypothesis. The conjuncts of the hypothesis +that mention only the name, and that the solver reads, now cut the set down. Where the cut is +listed, or is the whole numbers from a least one, the statement is decided over it, with the other +conjuncts kept. `exists` and `exists!` read their conjunction the same way. And +`forall a in ZZ : a^7 mod 7 = a mod 7` was left as written while `a^7 = a (mod 7)` was decided. An +equation or an order comparison between residues `p mod m` of polynomials with whole coefficients +repeats with the moduli, since `mod` is the floored remainder, so the residues decide it too. +These are Sullivan and Mackey's Prob 8.9.24–26 at a given prime +([#1409](https://github.com/asc-community/AngouriMath/issues/1409)). Quantifiers were a parse error +in 2.5.0. + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `forall k in ZZ : (0 < k and k < 7) implies 7 divides binomial(7, k)` | `UnhandledParseException` | `True` | +| `exists! k in ZZ : (0 < k and k < 7) and 3 divides k and 2 divides k` | `UnhandledParseException` | `True` | +| `forall n in ZZ : n >= 5 implies 2^n > n^2` | `UnhandledParseException` | `True` | +| `forall a in ZZ : a^7 mod 7 = a mod 7` | `UnhandledParseException` | `True` | +| `forall a, b in ZZ : (a + b)^5 mod 5 = (a^5 + b^5) mod 5` | `UnhandledParseException` | `True` | + ### `binomial(n, k)` is a function **Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled diff --git a/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs b/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs index 31df4f5f3..9a8ae8dd3 100644 --- a/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs +++ b/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs @@ -123,6 +123,18 @@ private static Entity Quantified(Kind kind, Entity var, Entity over, Entity body by = ("the body does not mention the name, so it says the same of every member", "forall_const"); return Closed(kind, set, body); } + // A statement about the members that satisfy a condition on the name alone is the + // statement over the set cut down to them: forall k in ZZ : (0 < k and k < 7) implies + // 7 divides binomial(7, k) -- Sullivan and Mackey's Prob 8.9.24 at p = 7 -- is the + // statement over {1, ..., 6}. The converse of the set-builder route above, so the cut + // is taken only where it comes out listed, or as the whole numbers from a least one, + // which the routes below read -- never as a set-builder again. + // https://github.com/asc-community/AngouriMath/issues/1409 + if (set is SpecialSet whole && IsIntegerSet(whole) && CutByHypothesis(kind, x, whole, body, isExact) is var (cutSet, cutCondition, cutRest)) + { + by = ($"the members that satisfy {cutCondition} are {cutSet}", "Set.mem_setOf_eq"); + return Decide(kind, x, cutSet, cutRest, isExact); + } // The whole numbers from m are ZZ* shifted by m, so a statement over ZZ+ /\ [4; +oo) // is the statement about 4 + t over ZZ*, where every route below reads the set. if (set is Intersectionf cut && LeastMember(cut) is { } start && IsUnboundedAbove(cut)) @@ -635,11 +647,88 @@ internal static bool IsIntegerSet(SpecialSet set) if (Period(first, x, set) is not { } one || Period(second, x, set) is not { } another) return null; return one * another / one.Gcd(another); + // An equation or an order comparison between residues -- a^7 mod 7 = a mod 7, + // Fermat's little theorem as Sullivan and Mackey write it (Prob 8.9.26) -- repeats + // with the least common multiple of the moduli it mentions. + case Equalsf or Greaterf or GreaterOrEqualf or Lessf or LessOrEqualf: + if (body is not IBinaryNode { NodeFirstChild: var lhs, NodeSecondChild: var rhs }) + return null; + if (ResiduePeriod(lhs, x, set) is not { } l || ResiduePeriod(rhs, x, set) is not { } r) + return null; + return l * r / l.Gcd(r); default: return body.ContainsNode(x) ? null : EInteger.One; } } + /// + /// For a body that asks about the members satisfying conditions on the name alone -- + /// Q implies P of every member, Q and P of some -- the set cut down by the + /// conjuncts of Q the solver states as a set, where the cut comes out listed or as + /// the whole numbers from a least one; with those conjuncts, and the claim that is left: + /// (0 < k and k < 7 and 2 divides k) implies P is 2 divides k implies P over + /// {1, ..., 6}. + /// + private static (Set Cut, Entity Condition, Entity Claim)? CutByHypothesis(Kind kind, Variable x, SpecialSet set, Entity body, bool isExact) + { + var (hypotheses, conclusion) = (kind, body) switch + { + (Kind.All, Impliesf(var assumption, var then)) => (Andf.LinearChildren(assumption).ToList(), then), + (_, Andf) when kind != Kind.All => (Andf.LinearChildren(body).ToList(), (Entity?)null), + _ => (null, null), + }; + if (hypotheses is null) + return null; + var bounds = hypotheses.Where(h => OnlyAbout(h, x) && SolverReads(h, x)).ToList(); + var others = hypotheses.Where(h => !bounds.Contains(h)).ToList(); + if (bounds.Count == 0) + return null; + var condition = bounds.Aggregate(static (a, b) => a & b); + var claim = kind == Kind.All + ? others.Count == 0 ? conclusion! : others.Aggregate(static (a, b) => a & b).Implies(conclusion!) + : others.Count == 0 ? Entity.Boolean.True : others.Aggregate(static (a, b) => a & b); + try + { + return new Intersectionf(set, condition.Solve(x)).InnerSimplified(isExact) switch + { + FiniteSet listed => (listed, condition, claim), + Intersectionf fromLeast when LeastMember(fromLeast) is not null && IsUnboundedAbove(fromLeast) => (fromLeast, condition, claim), + _ => null, + }; + } + catch (AngouriBugException) { throw; } + catch (AngouriMathBaseException) { return null; } + } + + private static bool OnlyAbout(Entity condition, Variable x) + => condition.ContainsNode(x) && condition.Vars.All(v => v == x); + + /// + /// The period in of an expression made of residues p mod m + /// and numbers by sums, differences and products: 's for each + /// residue, since mod is the floored remainder and p(x) mod m depends on + /// x only through p(x) modulo m; where + /// appears outside a residue. + /// + private static EInteger? ResiduePeriod(Entity expr, Variable x, SpecialSet set) + { + if (!expr.ContainsNode(x)) + return EInteger.One; + switch (expr) + { + case Modf(var dividend, Integer modulus) when !modulus.EInteger.IsZero: + return PeriodModulo(dividend, modulus.EInteger.Abs(), x, set); + case Sumf or Minusf or Mulf: + if (expr is not IBinaryNode { NodeFirstChild: var first, NodeSecondChild: var second }) + return null; + if (ResiduePeriod(first, x, set) is not { } one || ResiduePeriod(second, x, set) is not { } another) + return null; + return one * another / one.Gcd(another); + default: + return null; + } + } + /// /// The period, in , of an expression's residue modulo /// : the modulus for a polynomial with whole coefficients, diff --git a/Sources/Tests/UnitTests/Core/QuantifierTest.cs b/Sources/Tests/UnitTests/Core/QuantifierTest.cs index d67d74afc..c33de302e 100644 --- a/Sources/Tests/UnitTests/Core/QuantifierTest.cs +++ b/Sources/Tests/UnitTests/Core/QuantifierTest.cs @@ -82,6 +82,15 @@ public void DecidedOverAFiniteSet(string statement, string expected) [InlineData("forall x in ZZ : (1 <= x and x <= 3) implies 2 divides x", "False")] [InlineData("exists x in ZZ : x^2 = 4 and 1 <= x and x <= 3", "True")] [InlineData("forall x in ZZ : x^2 = 4 implies 1 <= x and x <= 3", "False")] + // The members that satisfy the conditions on the name the solver reads, listed or from a + // least one, with the other conditions kept: Prob 8.9.24 at p = 7, where every C(7, k) is + // a multiple of 7, and at 6, where C(6, 2) = 15 is not; Ex 5.3.2 from its threshold. + [InlineData("forall k in ZZ : (0 < k and k < 7) implies 7 divides binomial(7, k)", "True")] + [InlineData("forall k in ZZ : (0 < k and k < 6) implies 6 divides binomial(6, k)", "False")] + [InlineData("exists k in ZZ : (0 < k and k < 6) and not 6 divides binomial(6, k)", "True")] + [InlineData("exists! k in ZZ : (0 < k and k < 7) and 3 divides k and 2 divides k", "True")] + [InlineData("forall k in ZZ : (0 < k and k < 7 and 2 divides k) implies 3 divides binomial(7, k) - 1", "False")] + [InlineData("forall n in ZZ : n >= 5 implies 2^n > n^2", "True")] // The set may be an interval, a set builder, or a special set with no member of its own. [InlineData("forall x in (0; 1) : x^2 < 1", "True")] [InlineData("forall x in [0; 1] : x^2 < 1", "False")] diff --git a/Sources/Tests/UnitTests/Core/ResidueClassTest.cs b/Sources/Tests/UnitTests/Core/ResidueClassTest.cs index bf85fc1eb..a7dcc5373 100644 --- a/Sources/Tests/UnitTests/Core/ResidueClassTest.cs +++ b/Sources/Tests/UnitTests/Core/ResidueClassTest.cs @@ -96,6 +96,14 @@ public void TheInversesModuloAPrimeAreAPermutation() [InlineData("forall a, b in ZZ : (a = 1 (mod 3) and b = 2 (mod 3)) implies a + b = 0 (mod 3)", "True")] [InlineData("forall n in ZZ : n^2 = n (mod 2)", "True")] [InlineData("forall x in ZZ : 4 divides x^2 implies 2 divides x", "True")] + // An equation or a comparison between residues written with mod repeats with the moduli: + // Fermat's little theorem at 7 (Prob 8.9.26), and its failure at 6, where 2^6 mod 6 is 4; + // the freshman's dream modulo 5 (Prob 8.9.25), and its failure modulo 4. + [InlineData("forall a in ZZ : a^7 mod 7 = a mod 7", "True")] + [InlineData("forall a in ZZ : a^6 mod 6 = a mod 6", "False")] + [InlineData("forall a, b in ZZ : (a + b)^5 mod 5 = (a^5 + b^5) mod 5", "True")] + [InlineData("forall a, b in ZZ : (a + b)^4 mod 4 = (a^4 + b^4) mod 4", "False")] + [InlineData("forall x in ZZ : x^2 mod 4 < 2", "True")] // Example 6.5.27: 3 x^2 - 5 y^2 = 1 has no solutions, since x^2 = 2 (mod 5) has none; // Problem 4.11.7: x^2 - y^2 = 14 has none, by parity. [InlineData("exists x, y in ZZ : 3 x^2 - 5 y^2 = 1", "False")]