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
14 changes: 14 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,20 @@ Every spelling but `lcm` was a parse error in 2.5.0, the congruence node being n
| `forall n in ZZ : 6 divides n^3 + 5 n` | `UnhandledParseException` | `True` |
| `exists x, y in ZZ : 3 x^2 - 5 y^2 = 1` | `UnhandledParseException` | `False` |

### Floors and ceilings of `n` over a whole number are decided over its residues

A comparison between floors and ceilings of linear functions of `n` and a linear part in `n`
repeats with the least common multiple of their denominators where the drifts cancel: over a
period, a floor of `(p/q) n + b` rises by as much as the linear terms do. The residues then decide
it, as they decide a divisibility. `forall n in ZZ : floor(n/2) + ceil(n/2) = n` and Hermite's
`floor(n/3) + floor((n + 1)/3) + floor((n + 2)/3) = n` are `True`, where they were left as written
([#1409](https://github.com/asc-community/AngouriMath/issues/1409)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `forall n in ZZ : floor(n/2) + ceil(n/2) = n` | `UnhandledParseException` (left as written when quantifiers arrived) | `True` |
| `exists n in ZZ : floor(n/2) + ceil(n/2) = n + 1` | `UnhandledParseException` (left as written when quantifiers arrived) | `False` |

### An antiderivative built through an even root says where the root is real

The linear-radical substitution, `u = (a x + b)^(1/q)`, and the general substitution for an even
Expand Down
48 changes: 48 additions & 0 deletions Sources/AngouriMath/Functions/Boolean/Quantifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,10 @@ internal static bool IsIntegerSet(SpecialSet set)
case Equalsf or Greaterf or GreaterOrEqualf or Lessf or LessOrEqualf:
if (body is not IBinaryNode { NodeFirstChild: var lhs, NodeSecondChild: var rhs })
return null;
// Floors and ceilings of linear functions beside a linear part, where the
// drifts cancel: floor(n/2) + ceil(n/2) - n repeats with 2.
if (DriftlessPeriod(lhs - rhs, x) is { } rounding)
return rounding;
if (ResiduePeriod(lhs, x, set) is not { } l || ResiduePeriod(rhs, x, set) is not { } r)
return null;
return l * r / l.Gcd(r);
Expand Down Expand Up @@ -846,6 +850,50 @@ private static bool OnlyAbout(Entity condition, Variable x)
}
}

/// <summary>
/// The period of a sum of floors and ceilings of linear functions of <paramref name="x"/>
/// and of a linear part, where the drifts cancel, or <see langword="null"/>. A floor of
/// <c>(p/q) x + b</c> rises by <c>p</c> when <c>x</c> rises by <c>q</c>, and a linear
/// term by its slope times <c>q</c>, so where these sum to nothing the difference repeats
/// with the least common multiple of the <c>q</c>: <c>floor(n/2) + ceil(n/2) - n</c> with
/// 2, and Hermite's <c>floor(n/3) + floor((n + 1)/3) + floor((n + 2)/3) - n</c> with 3.
/// https://github.com/asc-community/AngouriMath/issues/1409
/// </summary>
private static EInteger? DriftlessPeriod(Entity difference, Variable x)
{
var period = EInteger.One;
var drift = ERational.Zero;
var rounded = false;
foreach (var term in Sumf.LinearChildren(difference))
{
if (!term.ContainsNode(x))
continue;
var (coefficient, core) = term is Mulf(Rational factor, var rest) ? (factor.ERational, rest) : (ERational.One, term);
var argument = core switch
{
Floorf floor => floor.Argument,
Ceilf ceiling => ceiling.Argument,
_ => null,
};
if (Slope(argument ?? core, x) is not { } slope)
return null;
if (argument is not null)
{
rounded = true;
var q = slope.Denominator.Abs();
period = period * q / period.Gcd(q);
}
drift = drift.Add(coefficient.Multiply(slope));
}
return rounded && drift.IsZero ? period : null;
}

/// <summary>The slope of an expression linear in <paramref name="x"/> with a rational one, or <see langword="null"/>.</summary>
private static ERational? Slope(Entity expression, Variable x)
=> TreeAnalyzer.TryGetPolyLinear(expression, x, out var a, out var b) && !b.ContainsNode(x) && a.Evaled is Rational rate
? rate.ERational
: null;

/// <summary>
/// The period, in <paramref name="x"/>, of an expression's residue modulo
/// <paramref name="modulus"/>: the modulus for a polynomial with whole coefficients,
Expand Down
8 changes: 8 additions & 0 deletions Sources/Tests/UnitTests/Core/QuantifierTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public void DecidedOverAFiniteSet(string statement, string expected)
[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")]
// 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.
[InlineData("forall n in ZZ : floor(n/2) + ceil(n/2) = n", "True")]
[InlineData("forall n in ZZ : floor(n/3) + floor((n + 1)/3) + floor((n + 2)/3) = n", "True")]
[InlineData("forall n in ZZ : floor(n/2) = n/2", "False")]
[InlineData("forall n in ZZ+ : ceil(n/2) - floor(n/2) <= 1", "True")]
[InlineData("exists n in ZZ : floor(n/2) + ceil(n/2) = n + 1", "False")]
// Prob 8.9.24 for every prime: that p is prime and 0 < k < p is established by the
// quantifiers around the claim, in either order and with the hypothesis curried, and the
// rule for p divides binomial(p, k) reads it. Without the primality, or without either
Expand Down
Loading