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
33 changes: 33 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,39 @@ onto `B`, Sullivan and Mackey's Def 7.4.1, [#1409](https://github.com/asc-commun
| `(-oo)^2` | `NaN` — wrong | `+oo` |
| `forall b in RR : exists a in RR : a^3 = b` | `UnhandledParseException` (quantifiers are new since; left as written when they arrived) | `True` |
| `forall b in RR : exists a in RR : e^a = b` | `UnhandledParseException` | `False` |

### One-to-one is decided for a quotient of polynomials on an interval

`forall a in S : forall b in S : f(a) = f(b) implies a = b` — `f` one-to-one on `S`, Sullivan and
Mackey's Def 7.4.1 — is decided for a quotient of polynomials on an interval by the sign of its
derivative between the zeros inside: strictly monotone is one-to-one, and a turn is not. The cube
is one-to-one on the reals although its derivative is zero at 0; the square is on `[0; +oo)`. On the
whole numbers only a yes carries over from the interval around them
([#1409](https://github.com/asc-community/AngouriMath/issues/1409)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `forall a in RR : forall b in RR : a^3 = b^3 implies a = b` | `UnhandledParseException` (left as written when quantifiers arrived) | `True` |
| `forall a in [0; +oo) : forall b in [0; +oo) : a^2 = b^2 implies a = b` | `UnhandledParseException` (left as written when quantifiers arrived) | `True` |
| `forall a in RR : forall b in RR : a^3 - 3 a = b^3 - 3 b implies a = b` | `UnhandledParseException` | `False` |

### A family of intervals with monotone ends is the interval between their extremes

`union(A_n, n in I)` and `intersection(A_n, n in I)` of intervals whose ends are quotients of
polynomials in the index, monotone over its range, are the interval between the extremes of the
ends: an intersection always, a union where the family is a chain or the index runs over an
interval. An end is closed where the family's ends are and the extreme is reached, or, in an
intersection, where it is only approached. Sullivan and Mackey's §3.9.5 Try 8–10: the intersection
of `(-1/n; 1/n)` over `ZZ+` is `{ 0 }`, the union of `(x; x + 1)` over `(0; 1)` is `(0; 2)`, and
the union of `[0; (n - 1)/n)` is `[0; 1)`, which is the intersection of `(-1/n; 1)`
([#1409](https://github.com/asc-community/AngouriMath/issues/1409)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `intersection((-1/n; 1/n), n in ZZ+)` | `UnhandledParseException` (left as written when families arrived) | `{ 0 }` |
| `union((x; x + 1), x in (0; 1))` | `UnhandledParseException` (left as written when families arrived) | `(0; 2)` |
| `union([0; (n - 1)/n), n in ZZ+) = intersection((-1/n; 1), n in ZZ+)` | `UnhandledParseException` | `True` |

### The binomial coefficient's identities, and its sums in closed form

Pascal's rule, the chairperson identity and the symmetry are rewrite rules, each in the direction
Expand Down
27 changes: 27 additions & 0 deletions Sources/AngouriMath/Functions/Boolean/Quantifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,23 @@ private static Entity Quantified(Kind kind, Entity var, Entity over, Entity body
return covered;
}
}
// forall a in S : forall b in S : f(a) = f(b) implies a = b is the statement that f is
// one-to-one on S (Sullivan and Mackey's Def 7.4.1), which a quotient of polynomials is
// on an interval exactly where it is strictly monotone there: x^3 on RR is, x^2 on RR is
// not. On the whole numbers only a yes carries over from the interval they lie in,
// since x^2 is one-to-one on ZZ+ and not monotone on RR.
// https://github.com/asc-community/AngouriMath/issues/1409
if (kind == Kind.All && body is Forallf(Variable twin, Set twinSet, Impliesf(Equalsf(var atThis, var atTwin), Equalsf(var sideA, var sideB)))
&& twinSet == set && twin != x && (sideA == x && sideB == twin || sideA == twin && sideB == x)
&& (atTwin == atThis.Substitute(x, twin) ? atThis : atThis == atTwin.Substitute(x, twin) ? atTwin : null) is { } mapping
&& (set is SpecialSet wholeSet && IsIntegerSet(wholeSet) ? IntervalAround(wholeSet) : set) is { } around
&& Functions.ImageByCalculus.OneToOne(mapping, x, around) is { } oneToOne && (oneToOne || around == set))
{
by = oneToOne
? ($"{mapping} is strictly monotone on {around}, so it is one-to-one", "StrictMono.injective")
: ($"{mapping} turns inside {set}, so two members share a value", "intermediate_value_Icc");
return oneToOne ? Entity.Boolean.True : Entity.Boolean.False;
}
// An equation whose two sides differ by a polynomial that expands to nothing holds
// at every member, whatever the set: (x + 1)^2 = x^2 + 2 x + 1 is True of each.
if (body is Equalsf(var left, var right))
Expand Down Expand Up @@ -1507,6 +1524,16 @@ private static IEnumerable<Entity> Candidates(Set set)
0, 1, -1, 2, -2, 10, -10, Rational.Create(1, 2), Rational.Create(-1, 2),
};

/// <summary>The interval a set of whole numbers lies in, or <see langword="null"/>.</summary>
private static Set? IntervalAround(SpecialSet whole)
=> whole switch
{
SpecialSet.Integers => new Interval(Real.NegativeInfinity, false, Real.PositiveInfinity, false),
SpecialSet.NonNegativeIntegers => new Interval(Integer.Zero, true, Real.PositiveInfinity, false),
SpecialSet.PositiveIntegers or SpecialSet.Primes => new Interval(Integer.One, true, Real.PositiveInfinity, false),
_ => null,
};

private static Entity? BySolving(Kind kind, Variable x, Set set, Entity body)
{
// The inequality solver answers over the reals, so an order comparison is asked
Expand Down
171 changes: 171 additions & 0 deletions Sources/AngouriMath/Functions/Continuous/ImageByCalculus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,177 @@ internal static class ImageByCalculus
_ => null,
};

/// <summary>
/// Whether a quotient of polynomials is one-to-one on an interval, or <see langword="null"/>
/// where that is not settled here. A continuous function on an interval is one-to-one
/// exactly where it is strictly monotone: <c>x^3</c> on <c>RR</c> is, and <c>x^2</c> on
/// <c>RR</c> is not. Sullivan and Mackey's Def 7.4.1.
/// </summary>
internal static bool? OneToOne(Entity f, Variable x, Entity over)
=> Monotone(f, x, over) switch
{
null => null,
0 => false,
_ => true,
};

/// <summary>
/// The direction a quotient of polynomials runs in on an interval: <c>1</c> where it is
/// strictly increasing, <c>-1</c> where strictly decreasing, <c>0</c> where it is neither,
/// and <see langword="null"/> where that is not settled here. A differentiable function is
/// strictly monotone exactly where its derivative keeps one sign away from the points
/// where it is zero: the derivative of <c>x^3</c> is zero at 0 and positive on either
/// side, and that of <c>x^2</c> changes sign at 0. A pole inside the interval leaves it
/// unsettled.
/// </summary>
internal static int? Monotone(Entity f, Variable x, Entity over)
{
if (!f.ContainsNode(x) || !IsRational(f, x) || Pieces(over) is not { Count: 1 } pieces)
return null;
var piece = pieces[0];
var (numerator, denominator) = SingleQuotient.Of(f);
var slope = numerator.Differentiate(x) * denominator - numerator * denominator.Differentiate(x);
if (Polynomial(denominator, x) is not { } bottom || Polynomial(slope, x) is not { } top
|| RealRoots(bottom, x) is not { } poles || RealRoots(top, x) is not { } critical
|| AsReal(piece.Left) is not { } left || AsReal(piece.Right) is not { } right || left.CompareTo(right) >= 0
|| poles.Any(pole => pole.Value.CompareTo(left) > 0 && pole.Value.CompareTo(right) < 0))
return null;
// The derivative's sign between each two consecutive of the ends and the zeros
// inside, which is its numerator's, the denominator being a square.
var ends = new List<Real> { left };
ends.AddRange(critical.Select(point => point.Value).Where(at => at.CompareTo(left) > 0 && at.CompareTo(right) < 0).OrderBy(at => at));
ends.Add(right);
int? sign = null;
for (var i = 0; i + 1 < ends.Count; i++)
{
if (AsReal(top.Substitute(x, Between(ends[i], ends[i + 1]))) is not { IsFinite: true } value)
return null;
var here = value.EDecimal.Sign;
// Zero on a whole stretch is a constant there, and a change of sign a turn.
if (here == 0 || sign is { } before && before != here)
return 0;
sign = here;
}
return sign;
}

/// <summary>
/// A union or an intersection of a family of intervals whose ends are quotients of
/// polynomials in the index, monotone over its range, as the interval between the extremes
/// of the ends; <see langword="null"/> where that is not settled here. Sullivan and
/// Mackey's §3.9.5: the intersection of <c>(-1/n, 1/n)</c> over the positive whole numbers
/// is <c>{0}</c>, the union of <c>(x, x + 1)</c> over <c>(0, 1)</c> is <c>(0, 2)</c>, and
/// the union of <c>[0, (n - 1)/n)</c> is <c>[0, 1)</c>, the intersection of <c>(-1/n, 1)</c>.
/// </summary>
/// <remarks>
/// <para>
/// An intersection of intervals is an interval, from the supremum of the left ends to the
/// infimum of the right ones. An end of it is closed where the family's ends are, and where
/// the extreme is only approached: every <c>(-1/n, 1/n)</c> holds 0, where both ends tend.
/// </para>
/// <para>
/// A union is an interval from the infimum of the left ends to the supremum of the right
/// ones where the family is a chain, each member inside the next -- ends moving apart, or
/// one still -- and where the index runs over an interval and every member is not empty,
/// so that the members overlap as it moves. Over the whole numbers a sliding family,
/// <c>(k, k + 1)</c>, leaves gaps, and is not read. An end of the union is closed where the
/// family's ends are and the extreme is reached.
/// </para>
/// <para>
/// A monotone end has its extremes at the ends of the index's range: at a least or a
/// greatest member, where it is reached, and in the limit, where it is not.
/// </para>
/// </remarks>
internal static Set? Family(bool union, Interval member, Variable k, Entity over)
{
if (Range(over) is not var (range, whole)
|| Extremes(member.Left, k, range) is not var (leftLow, leftHigh, leftDirection)
|| Extremes(member.Right, k, range) is not var (rightLow, rightHigh, rightDirection))
return null;
(Entity Value, bool Closed) lower, upper;
if (union)
{
var chain = leftDirection <= 0 && rightDirection >= 0 || leftDirection >= 0 && rightDirection <= 0;
if (!chain && (whole || !NeverEmpty(member, k, range)))
return null;
lower = (leftLow.Value, member.LeftClosed && leftLow.Reached);
upper = (rightHigh.Value, member.RightClosed && rightHigh.Reached);
}
else
{
lower = (leftHigh.Value, member.LeftClosed || !leftHigh.Reached);
upper = (rightLow.Value, member.RightClosed || !rightLow.Reached);
}
if (AsReal(lower.Value) is not { IsNaN: false } from || AsReal(upper.Value) is not { IsNaN: false } to)
return null;
// Simplified, so that [0; 0] is the point it is and (0; 0) nothing.
return new Interval(lower.Value, lower.Closed && from.IsFinite, upper.Value, upper.Closed && to.IsFinite).InnerSimplified as Set;
}

/// <summary>The index's range as an interval, and whether the index is a whole number.</summary>
private static (Interval Range, bool Whole)? Range(Entity over)
=> over switch
{
SpecialSet.PositiveIntegers => (new Interval(Integer.One, true, Real.PositiveInfinity, false), true),
SpecialSet.NonNegativeIntegers => (new Interval(Integer.Zero, true, Real.PositiveInfinity, false), true),
SpecialSet.Integers => (new Interval(Real.NegativeInfinity, false, Real.PositiveInfinity, false), true),
SpecialSet.Reals => (new Interval(Real.NegativeInfinity, false, Real.PositiveInfinity, false), false),
Interval interval when interval.IsNumeric => (interval, false),
_ => null,
};

/// <summary>
/// The least and the greatest of an end over the range, each with whether it is reached,
/// and the end's direction, <c>0</c> for one that does not move; <see langword="null"/>
/// where the end is not monotone there.
/// </summary>
private static ((Entity Value, bool Reached) Low, (Entity Value, bool Reached) High, int Direction)? Extremes(Entity end, Variable k, Interval range)
{
if (!end.ContainsNode(k))
return ((end, true), (end, true), 0);
if (Monotone(end, k, range) is not { } direction || direction == 0)
return null;
if (At(end, k, range.Left, range.LeftClosed, Core.ApproachFrom.Right) is not { } atStart
|| At(end, k, range.Right, range.RightClosed, Core.ApproachFrom.Left) is not { } atEnd)
return null;
return direction > 0 ? (atStart, atEnd, 1) : (atEnd, atStart, -1);
}

/// <summary>The end's value at an end of the range, reached, or its limit there, not reached.</summary>
private static (Entity Value, bool Reached)? At(Entity end, Variable k, Entity at, bool included, Core.ApproachFrom side)
{
if (AsReal(at) is not { } point)
return null;
if (included && point.IsFinite)
return (end.Substitute(k, at).Simplify(), true);
var limit = point.IsFinite ? end.Limit(k, at, side) : end.Limit(k, at);
return AsReal(limit) is { IsNaN: false } ? (limit, false) : null;
}

/// <summary>Whether every member of a family is not empty: its right end above its left one over the whole range.</summary>
private static bool NeverEmpty(Interval member, Variable k, Interval range)
{
var width = (member.Right - member.Left).Simplify();
if (!width.ContainsNode(k))
return AsReal(width) is { } constant && constant.EDecimal.Sign > 0;
return Of(width, k, range) switch
{
Interval { Left: var least, LeftClosed: var reached } => AsReal(least) is { } bound && (bound.EDecimal.Sign > 0 || bound.EDecimal.Sign == 0 && !reached),
FiniteSet { Count: 1 } single => AsReal(single.First()) is { } only && only.EDecimal.Sign > 0,
_ => false,
};
}

/// <summary>A point strictly between two ends, either of which may be infinite.</summary>
private static Entity Between(Real low, Real high)
=> (low.IsFinite, high.IsFinite) switch
{
(true, true) => ((Entity)low + high) / 2,
(true, false) => (Entity)low + 1,
(false, true) => (Entity)high - 1,
_ => Integer.Zero,
};

/// <summary>The image of one interval with no pole inside.</summary>
private static Set? Of(Entity f, Variable x, Interval piece, List<(Entity Root, Real Value)> critical)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ protected override Entity InnerSimplify(bool isExact)
if (this is IndexedUnionf && Var is Variable name && body is FiniteSet { Count: 1 } single
&& Functions.ImageByCalculus.Of(single.First(), name, over) is { } byCalculus)
return byCalculus;
// A family of intervals whose ends are monotone in the index, as the interval
// between the extremes of its ends: the intersection of (-1/n; 1/n) over ZZ+ is
// { 0 }. https://github.com/asc-community/AngouriMath/issues/1409
if (Var is Variable familyIndex && body is Interval familyMember
&& Functions.ImageByCalculus.Family(this is IndexedUnionf, familyMember, familyIndex, over) is { } family)
return family;
if (over is FiniteSet indices)
{
if (indices.Count == 0)
Expand Down
25 changes: 25 additions & 0 deletions Sources/Tests/UnitTests/Core/Sets/IndexedSetOperationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ public void AFamilyOverAListedIndexSetIsFolded(string expression, string expecte
public void MembershipOfAnInfiniteFamilyIsDecidedByTheQuantifiers(string expression, string expected)
=> Assert.Equal(expected.ToEntity(), Evaluated(expression));

/// <summary>
/// A family of intervals whose ends are monotone in the index is the interval between the
/// extremes of its ends, closed where they are reached, or approached by the ends of an
/// intersection: Sullivan and Mackey's §3.9.5 Try 8–10, and growing and shrinking families.
/// A sliding family over the whole numbers leaves gaps and stays a family.
/// </summary>
[Theory]
[InlineData("intersection((-1/n; 1/n), n in ZZ+)", "{0}")]
[InlineData("union((x; x + 1), x in (0; 1))", "(0; 2)")]
[InlineData("union([0; (n - 1)/n), n in ZZ+)", "[0; 1)")]
[InlineData("intersection((-1/n; 1), n in ZZ+)", "[0; 1)")]
[InlineData("intersection([0; 1/n), n in ZZ+)", "{0}")]
[InlineData("union([1/n; 1], n in ZZ+)", "(0; 1]")]
[InlineData("union([1; (k + 1)/k], k in ZZ+)", "[1; 2]")]
public void AFamilyWithMonotoneEndsIsAnInterval(string expression, string expected)
=> Assert.Equal(expected.ToEntity().Evaled, Evaluated(expression));

[Fact]
public void TwoFamiliesAreEqualWhereTheirIntervalsAre()
{
Assert.Equal(Entity.Boolean.True, Evaluated("union([0; (n - 1)/n), n in ZZ+) = intersection((-1/n; 1), n in ZZ+)"));
var sliding = "union((n; n + 1), n in ZZ+)".ToEntity();
Assert.Equal(sliding, sliding.Evaled);
}

/// <summary>Ex 3.5.11: the complement of a set in a universe is the universe less the set.</summary>
[Theory]
[InlineData("complement({1, 2}, ZZ+ /\\ [1; 5])", "{3, 4, 5}")]
Expand Down
Loading
Loading