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
28 changes: 28 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,34 @@ written
| `"forall n in ZZ+ : n! >= 2^(n - 1)".ToEntity().Evaled` | `UnhandledParseException` | `True` |
| `"forall n in ZZ+ /\\ [4; +oo) : n^2 - 2 n - 1 > 0".ToEntity().Evaled` | `UnhandledParseException` | `True` — decided about `4 + t` over `ZZ*` |

### `n in ZZ and 2^n > n^2` is solved to a set, and was a refusal

An inequality with an exponential or a factorial over the whole numbers is solved to the members
of a window from the least member and the rays the quantifier proves by induction, upwards and,
over `ZZ`, downwards; a tail it does not prove leaves the refusal in place, since a window is
evidence about the window ([#1409](https://github.com/asc-community/AngouriMath/issues/1409),
Sullivan and Mackey's Ex 5.3.2 and Prob 5.7.2, 5.7.8–9).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"n in ZZ and 2^n > n^2".ToEntity().Solve("n")` | `NotSufficientlySupportedException` — "Only polynomial inequalities are supported" | `{ 0, 1 } \/ ZZ /\ [5; +oo)` |
| `"n in ZZ+ and n! > 3^n".ToEntity().Solve("n")` | `NotSufficientlySupportedException` | `ZZ+ /\ [7; +oo)` |
| `"n in ZZ and n^3 < 3^(n - 1)".ToEntity().Solve("n")` | `NotSufficientlySupportedException` | `ZZ /\ (-oo; 0] \/ ZZ /\ [6; +oo)` |
| `"n in ZZ and 2^n >= n^2 - 3 n + 3".ToEntity().Solve("n")` | `NotSufficientlySupportedException` | the same — the tail from `1` is not proved, and the window is not guessed from |

And a divisibility or a congruence with a power of a whole base in the quantified name is decided
by its residues too, over the non-negative whole numbers: `a^n` modulo `m` repeats with the order
of `a` where `a` is prime to `m`, so `forall n in ZZ+ : 3 divides 7^n - 4^n` is `True` by one
residue and `forall n in ZZ* : not (7 divides 2^n + 1)` by three (Sullivan and Mackey's §5.2.4 Try 3
and Ex 5.3.7). A base sharing a factor with the modulus, or a negative exponent over `ZZ`, is left
as written.

| Input | Was (2.5.0) | Now |
|---|---|---|
| `forall n in ZZ+ : 3 divides 7^n - 4^n` | `UnhandledParseException` | `True` |
| `forall n in ZZ* : not (7 divides 2^n + 1)` | `UnhandledParseException` | `True` |
| `forall n in ZZ+ : 5 divides 7^n - 1` | `UnhandledParseException` | `False` |

### `binomial(n, k)` is a function

**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled
Expand Down
6 changes: 6 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
[AngouriMath/Functions/NumberTheory/ResidueClasses.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

[AngouriMath/Functions/Continuous/Solvers/InequalitySolver/ThresholdSearch.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/Algebra/ThresholdSetTest.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/Core/InductionTest.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

Expand Down
14 changes: 12 additions & 2 deletions Sources/AngouriMath/Docs/Usage/Syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ congruence is solved to a residue class — `"3 x = 11 (mod 7)".ToEntity().Solve
Chinese remainder theorem, `x = 3 (mod 4) and x = 5 (mod 6)` giving `{ x in ZZ : x = 11 (mod 12) }`
and `x = 3 (mod 4) and x = 2 (mod 6)` nothing, and with a bounded interval the members are listed:
`x = 2 (mod 7) and 0 <= x and x < 30` is `{ 2, 9, 16, 23 }`. `MathS.NumberTheory.ModularInverse(3, 10)`
is `7`, the one representative in `[1, n - 1]`, and `null` where `a` and `n` share a factor.
is `7`, the one representative in `[1, n - 1]`, and `null` where `a` and `n` share a factor. An
inequality with an exponential or a factorial over the whole numbers, `n in ZZ and 2^n > n^2`, is
solved to the members of a window from the least member and the tails the quantifier proves by
induction — `{ 0, 1 } \/ ZZ /\ [5; +oo)`, `n in ZZ+ and n! > 3^n` is `ZZ+ /\ [7; +oo)` — and where
a tail is not proved the window is not an answer, and the inequality solver refuses as before.
| 9 | `unite` `\/`, `setsubtract` `\` | one level, so `A \/ B \ C` is `(A \/ B) \ C` |
| 10 | `intersect` `/\` | |
| 11 | `+` `-` | |
Expand Down Expand Up @@ -317,7 +321,13 @@ factorial, a polynomial in `n` decided over the set, sums and products of these
ZZ+ /\ [5; +oo) : 2^n > n^2` is `True` (`2^(n + 1) - 2 · 2^n` leaves `n^2 - 2 n - 1`, positive
from `5`), `forall n in ZZ+ /\ [4; +oo) : 2^n > n^2` is `False` at `4`, and `forall n in ZZ+ :
n! >= 2^(n - 1)` is `True`. The whole numbers from `m` are `ZZ*` shifted by `m`, so a statement
over `ZZ+ /\ [4; +oo)` is decided as one about `4 + t` over `ZZ*`, whichever route reads it. `not`
over `ZZ+ /\ [4; +oo)` is decided as one about `4 + t` over `ZZ*`, whichever route reads it. A
divisibility or a congruence in a polynomial of `n` with whole coefficients repeats with the
modulus, so its residues decide it (`forall n in ZZ : 6 divides n^3 + 5 n` is `True` by six cases),
and with a power `a^n` of a whole base prime to the modulus among the terms it repeats with the
order of `a` as well: `forall n in ZZ+ : 3 divides 7^n - 4^n` is `True` by one residue, `forall n
in ZZ* : not (7 divides 2^n + 1)` by three — over the non-negative whole numbers, where the powers
are whole; a base sharing a factor with the modulus repeats only eventually and is left as written. `not`
passes through a quantifier by flipping it: `not forall x in S : P` is `exists x in S : not P`. In LaTeX, `\forall x \in S : P`. `forall`, `exists` and
`exists!` are keywords, so they are not names.

Expand Down
61 changes: 52 additions & 9 deletions Sources/AngouriMath/Functions/Boolean/Quantifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ internal enum Kind { All, Some, Unique }
// whole coefficients repeats with the modulus, so the residues decide it:
// forall n in ZZ : 6 divides n^3 + 5 n is six cases. Sullivan and Mackey's "case
// analysis over the classes" (Ex 6.5.14).
if (Period(body, x) is { } period && period.CompareTo(EInteger.FromInt32(LargestPeriod)) <= 0)
if (Period(body, x, integers) is { } period && period.CompareTo(EInteger.FromInt32(LargestPeriod)) <= 0)
return OverResidues(kind, x, integers, body, period, isExact);
// A polynomial equation with no solution modulo some m has none in the whole
// numbers: 3 x^2 - 5 y^2 = 1 is impossible modulo 5 (Ex 6.5.27). Only a refusal
Expand Down Expand Up @@ -156,7 +156,7 @@ internal enum Kind { All, Some, Unique }
/// The least member of a set of whole numbers bounded below -- <c>ZZ+</c>, <c>ZZ*</c>,
/// either cut by an interval -- or <see langword="null"/> where the set is something else.
/// </summary>
private static Integer? LeastMember(Set set)
internal static Integer? LeastMember(Set set)
{
switch (set)
{
Expand Down Expand Up @@ -282,6 +282,9 @@ private static bool IsUnboundedAbove(Intersectionf cut)
return Entity.Boolean.False;
if (atLeast != Entity.Boolean.True)
return null;
// Read off the shape first: 3^(-t - 1) + t^3 is positive on ZZ* term by term.
if (Sign(positive, x, set, isExact) is { } direct && (!strict || direct == Signum.Positive))
return Entity.Boolean.True;
var next = positive.Substitute(x, x + Integer.One);
foreach (var c in Multipliers(positive, x))
{
Expand Down Expand Up @@ -448,7 +451,7 @@ private enum Signum { Positive, NonNegative }
_ => null,
};

private static bool IsIntegerSet(SpecialSet set)
internal static bool IsIntegerSet(SpecialSet set)
=> set.ToDomain() is Domain.Integer or Domain.NonNegativeInteger or Domain.PositiveInteger;

/// <summary>How many residues a periodic statement is checked over before it is left as written.</summary>
Expand All @@ -460,34 +463,74 @@ private static bool IsIntegerSet(SpecialSet set)
/// with whole coefficients, joined by the connectives; the least common multiple of the
/// parts' periods.
/// </summary>
private static EInteger? Period(Entity body, Variable x)
private static EInteger? Period(Entity body, Variable x, SpecialSet set)
{
switch (body)
{
case Entity.Boolean:
return EInteger.One;
case Dividesf(Integer divisor, var dividend):
return divisor.EInteger.IsZero || !IsWholePolynomial(dividend) ? null : divisor.EInteger.Abs();
return divisor.EInteger.IsZero ? null : PeriodModulo(dividend, divisor.EInteger.Abs(), x, set);
case Congruentf(var left, var right, Integer modulus):
return modulus.EInteger.IsZero || !IsWholePolynomial(left) || !IsWholePolynomial(right) ? null : modulus.EInteger.Abs();
return modulus.EInteger.IsZero ? null : PeriodModulo(left - right, modulus.EInteger.Abs(), x, set);
case Notf(var operand):
return Period(operand, x);
return Period(operand, x, set);
// A quantifier over whole numbers inside: its body is periodic in x with the
// same period whatever the inner name takes, so the residues of x decide the
// outer statement and the inner one is decided at each of them.
case Quantifier { Over: SpecialSet inner } quantifier when IsIntegerSet(inner):
return Period(quantifier.Body, x);
return Period(quantifier.Body, x, set);
case Andf or Orf or Impliesf or Xorf:
if (body is not IBinaryNode { NodeFirstChild: var first, NodeSecondChild: var second })
return null;
if (Period(first, x) is not { } one || Period(second, x) is not { } another)
if (Period(first, x, set) is not { } one || Period(second, x, set) is not { } another)
return null;
return one * another / one.Gcd(another);
default:
return body.ContainsNode(x) ? null : EInteger.One;
}
}

/// <summary>
/// The period, in <paramref name="x"/>, of an expression's residue modulo
/// <paramref name="modulus"/>: the modulus for a polynomial with whole coefficients,
/// and with a power <c>a^e</c> of a whole base coprime to the modulus among the terms --
/// <c>7^n - 4^n</c> modulo <c>3</c>, <c>2^n + 1</c> modulo <c>7</c> -- the least common
/// multiple with the order of each base, since <c>a^(e + k) = a^e</c> modulo <c>m</c>
/// where <c>a^k = 1</c>. Over the non-negative whole numbers only, where the powers are
/// whole; <see langword="null"/> for a base sharing a factor with the modulus, whose
/// powers repeat only eventually. Sullivan and Mackey's Ex 5.3.7 and §5.2.4 Try 3.
/// </summary>
private static EInteger? PeriodModulo(Entity expr, EInteger modulus, Variable x, SpecialSet set)
{
if (IsWholePolynomial(expr))
return modulus;
if (set.ToDomain() is not (Domain.NonNegativeInteger or Domain.PositiveInteger))
return null;
var period = modulus;
var replaced = expr;
var count = 0;
foreach (var power in expr.Nodes.OfType<Powf>().Where(p => p.Exponent.ContainsNode(x)).Distinct())
{
if (power.Base.Evaled is not Integer { EInteger: var @base } || !power.Exponent.Vars.All(v => v == x) || !IsWholePolynomial(power.Exponent))
return null;
if (!@base.Gcd(modulus).Equals(EInteger.One))
return null;
var order = EInteger.One;
var value = @base.Mod(modulus);
while (!value.Equals(EInteger.One) && order.CompareTo(modulus) < 0)
{
value = value * @base % modulus;
order += 1;
}
if (!value.Equals(EInteger.One))
return null;
period = period * order / period.Gcd(order);
replaced = replaced.Substitute(power, Variable.CreateUnique(expr, "power_" + count++));
}
return IsWholePolynomial(replaced) ? period : null;
}

/// <summary>
/// A polynomial with whole coefficients in whatever names it mentions -- which is
/// periodic in each of them modulo anything, as long as the others stand for whole
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ internal static Set Solve(Entity expr, Variable x)

Equalsf => Empty,

// x in ZZ and an inequality with an exponential or a factorial in x: a window
// searched and its tails proved by induction, so that { n in ZZ : 2^n > n^2 }
// is {0, 1} \/ ZZ /\ [5; +oo) rather than a refusal from the inequality solver.
// https://github.com/asc-community/AngouriMath/issues/1409
Andf(Inf(var name, Set integers), ComparisonSign inequality) when name == x && ThresholdSearch.Solve(x, integers, inequality) is { } thresholds => thresholds,
Andf(ComparisonSign inequality, Inf(var name, Set integers)) when name == x && ThresholdSearch.Solve(x, integers, inequality) is { } thresholds => thresholds,
Andf(var left, var right) =>
Conjunction(Solve(left, x), Solve(right, x), expr, x),
Orf(var left, var right) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
// Website: https://am.angouri.org.
//

using System.Collections.Generic;
using System.Linq;
using AngouriMath.Functions.Boolean;
using static AngouriMath.Entity;
using static AngouriMath.Entity.Number;
using static AngouriMath.Entity.Set;

namespace AngouriMath.Functions.Algebra.AnalyticalSolving
{
/// <summary>
/// <c>{ n in ZZ : 2^n > n^2 }</c>, an inequality with an exponential or a factorial over
/// the whole numbers, is searched over a window from the least member and its tails are
/// proved: the members are the points of the window where it holds, and the whole numbers
/// from the start of the final run where the quantifier decides it holds there -- by
/// induction, which is how Sullivan and Mackey do it (Ex 5.3.2: <c>{0, 1} \/ ZZ /\ [5; +oo)</c>).
/// Over <c>ZZ</c> the other tail is proved the same way, downwards. A tail the quantifier
/// leaves undecided leaves the set as written: a window is evidence about the window.
/// https://github.com/asc-community/AngouriMath/issues/1409
/// </summary>
internal static class ThresholdSearch
{
/// <summary>How far from the least member, or from zero, the window reaches.</summary>
private const int Window = 64;

internal static Set? Solve(Variable x, Set integers, ComparisonSign inequality)
{
if (inequality is Equalsf || inequality.Vars.Any(v => v != x))
return null;
if (!inequality.Nodes.Any(node => node is Powf(_, var e) && e.ContainsNode(x) || node is Factorialf f && f.ContainsNode(x)))
return null;
if (integers is not SpecialSet special || !Quantifiers.IsIntegerSet(special))
return null;
var least = Quantifiers.LeastMember(special);
var from = least is null ? -Window : least.EInteger.ToInt32Checked();
var truth = new List<bool>();
for (var n = from; n <= Window; n++)
{
if (inequality.Substitute(x, Integer.Create(n)).Evaled is not Entity.Boolean(var holds))
return null;
truth.Add(holds);
}
// The final run, and the first one over ZZ, are what the quantifier is asked about.
var last = truth.Count - 1;
var tailStart = last;
while (tailStart > 0 && truth[tailStart - 1] == truth[last])
tailStart--;
var upwards = truth[last] ? inequality : Negated(inequality);
if (!Holds(upwards, x, from + tailStart, downwards: false))
return null;
var headEnd = 0;
if (least is null)
{
while (headEnd < tailStart - 1 && truth[headEnd + 1] == truth[0])
headEnd++;
if (!Holds(truth[0] ? inequality : Negated(inequality), x, from + headEnd, downwards: true))
return null;
}
// One run over the whole window, proved on every side, is the whole set.
if (tailStart == 0 && truth[last])
return special;
if (tailStart == 0)
return Empty;
var listed = new List<Entity>();
for (var i = least is null ? headEnd + 1 : 0; i < tailStart; i++)
if (truth[i])
listed.Add(Integer.Create(from + i));
Set result = new FiniteSet(listed);
if (least is null && truth[0])
result = result.Unite(special.Intersect(new Interval(Real.NegativeInfinity, false, Integer.Create(from + headEnd), true)));
if (truth[last])
result = result.Unite(special.Intersect(new Interval(Integer.Create(from + tailStart), true, Real.PositiveInfinity, false)));
return result;
}

/// <summary>Whether the statement holds at every whole number from <paramref name="start"/> on, or down from it.</summary>
private static bool Holds(Entity statement, Variable x, int start, bool downwards)
{
var t = Variable.CreateUnique(statement, "t");
Entity shifted = downwards ? Integer.Create(start) - t : Integer.Create(start) + t;
var body = statement.Substitute(x, shifted).InnerSimplified;
return Quantifiers.Decide(Quantifiers.Kind.All, t, MathS.Sets.NonNegativeIntegers, body, true) is Entity.Boolean(true);
}

private static ComparisonSign Negated(ComparisonSign inequality)
=> inequality switch
{
Greaterf(var a, var b) => new LessOrEqualf(a, b),
GreaterOrEqualf(var a, var b) => new Lessf(a, b),
Lessf(var a, var b) => new GreaterOrEqualf(a, b),
LessOrEqualf(var a, var b) => new Greaterf(a, b),
_ => throw new Core.Exceptions.AngouriBugException("An equation is refused above"),
};
}
}
Loading
Loading