diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 2af804431..c3b7d07c0 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -930,6 +930,20 @@ piece by piece. So Sullivan and Mackey's Prob 4.11.22 is decided both ways: for | `"((0; 1) \/ (3; 5)) /\ ((-1; 2) \/ (4; 6))".ToEntity().Evaled` | left as written | `(0; 1) \/ (4; 5)` | | `forall x in RR : x > 0 implies ((x^2 - 4)/(x^2 + 1) > 1 - 1/x implies (x^2 + 4)/(x^2 + 1) < 1 + 1/x)` | `UnhandledParseException` (left as written when quantifiers arrived) | `True` | +### A union of numbers is written as its pieces + +A union of numeric intervals and listed real numbers is written as its disjoint pieces: the listed +numbers first, then the intervals in increasing order, a point at an open end closing it and an +empty interval dropped. Pieces met pairwise before, and only when they were neighbours, so the +inequality solver's answer to `x^2 >= x` kept its roots beside the open intervals they close. Two +ways of writing one set of numbers now come out the same +([#1409](https://github.com/asc-community/AngouriMath/issues/1409)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"x^2 >= x".Solve("x")` | `{ 0, 1 } \/ (-oo; 0) \/ (1; +oo)` | `(-oo; 0] \/ [1; +oo)` | +| `"(0; 2) \/ ((1; 3) \/ (5; 6))".ToEntity().Evaled` | `(0; 2) \/ (1; 3) \/ (5; 6)` | `(0; 3) \/ (5; 6)` | + ### The size of an infinite set is an aleph, and sizes compare `card` of an infinite set is an aleph, `aleph(k)`, or a power of 2 of one: `card(ZZ)`, `card(QQ)` diff --git a/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Union.cs b/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Union.cs index 268fcfb30..349c7c032 100644 --- a/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Union.cs +++ b/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Union.cs @@ -12,6 +12,72 @@ namespace AngouriMath.Core.Sets { internal static partial class SetOperators { + /// + /// A union of numeric intervals and listed real numbers written as its disjoint pieces: + /// the listed numbers first, in increasing order, then the intervals in increasing order, + /// a point at an open end closing it and an empty interval dropped. x^2 >= x is + /// solved to { 0, 1 } \/ (-oo; 0) \/ (1; +oo), which is (-oo; 0] \/ [1; +oo), + /// and two ways of writing one set of numbers come out the same. + /// where a piece is anything else, or the union is written so already. + /// https://github.com/asc-community/AngouriMath/issues/1409 + /// + internal static Set? CanonicalUnion(Set set) + { + var pieces = new List<(Entity Left, bool LeftClosed, Entity Right, bool RightClosed)>(); + bool Collect(Entity piece) + { + switch (piece) + { + case Unionf(var left, var right): + return Collect(left) && Collect(right); + case Interval(var left, var leftClosed, var right, var rightClosed) when left.Evaled is Number.Real && right.Evaled is Number.Real: + var order = At(left).CompareTo(At(right)); + if (order < 0 || order == 0 && leftClosed && rightClosed) + pieces.Add((left, leftClosed, right, rightClosed)); + return true; + case FiniteSet listed when listed.All(static member => member.Evaled is Number.Real): + foreach (var member in listed) + pieces.Add((member, true, member, true)); + return true; + default: + return false; + } + } + if (!Collect(set)) + return null; + // By the left end, a closed end before an open one at the same point. + pieces.Sort(static (a, b) => At(a.Left).CompareTo(At(b.Left)) is var byLeft and not 0 ? byLeft : b.LeftClosed.CompareTo(a.LeftClosed)); + var merged = new List<(Entity Left, bool LeftClosed, Entity Right, bool RightClosed)>(); + foreach (var piece in pieces) + { + if (merged.Count == 0) + { + merged.Add(piece); + continue; + } + var last = merged[^1]; + var gap = At(piece.Left).CompareTo(At(last.Right)); + if (gap > 0 || gap == 0 && !last.RightClosed && !piece.LeftClosed) + { + merged.Add(piece); + continue; + } + var further = At(piece.Right).CompareTo(At(last.Right)); + merged[^1] = further > 0 ? (last.Left, last.LeftClosed, piece.Right, piece.RightClosed) + : further == 0 ? (last.Left, last.LeftClosed, last.Right, last.RightClosed || piece.RightClosed) + : last; + } + var points = merged.Where(static piece => At(piece.Left).CompareTo(At(piece.Right)) == 0).Select(static piece => piece.Left).ToList(); + var intervals = merged.Where(static piece => At(piece.Left).CompareTo(At(piece.Right)) != 0) + .Select(static piece => (Set)new Interval(piece.Left, piece.LeftClosed, piece.Right, piece.RightClosed)).ToList(); + Set canonical = intervals.Count == 0 ? new FiniteSet(points) + : (points.Count == 0 ? intervals : intervals.Prepend(new FiniteSet(points))) + .Aggregate(static (left, right) => new Unionf(left, right)); + return canonical == set ? null : canonical; + } + + private static PeterO.Numbers.EDecimal At(Entity end) => ((Number.Real)end.Evaled).EDecimal; + internal static Set UniteFiniteSetAndSet(FiniteSet finite, Set set) { if (set is FiniteSet another) diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs index 975c525d9..5acba0df4 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs @@ -131,61 +131,12 @@ protected override Entity InnerSimplify(bool isExact) } /// - /// A union of intervals and listed real numbers written as its disjoint pieces in - /// increasing order, which is how it reads, with a point at an open end closing it: - /// { 1, 2 } \/ (-oo; 1) \/ (2; +oo) is (-oo; 1] \/ [2; +oo). So two set - /// builders for one set of numbers come out the same. As it was where an end is not a - /// real number. + /// A listed set or a union of numbers as writes + /// it, which is how it reads: the listing's pieces met one by one come out in the order + /// they met. /// private static Entity? Increasing(Entity? set) - { - var pieces = new System.Collections.Generic.List<(Entity Left, bool LeftClosed, Entity Right, bool RightClosed)>(); - bool Collect(Entity piece) - { - switch (piece) - { - case Unionf(var left, var right): - return Collect(left) && Collect(right); - case Interval(var left, var leftClosed, var right, var rightClosed) when left.Evaled is Number.Real && right.Evaled is Number.Real: - pieces.Add((left, leftClosed, right, rightClosed)); - return true; - case FiniteSet listed when listed.All(static member => member.Evaled is Number.Real): - foreach (var member in listed) - pieces.Add((member, true, member, true)); - return true; - default: - return false; - } - } - if (set is null || !Collect(set) || pieces.Count < 2) - return set; - static EDecimal At(Entity end) => ((Number.Real)end.Evaled).EDecimal; - // By the left end, a closed end before an open one at the same point. - pieces.Sort(static (a, b) => At(a.Left).CompareTo(At(b.Left)) is var byLeft and not 0 ? byLeft : b.LeftClosed.CompareTo(a.LeftClosed)); - var merged = new System.Collections.Generic.List<(Entity Left, bool LeftClosed, Entity Right, bool RightClosed)> { pieces[0] }; - foreach (var piece in pieces.Skip(1)) - { - var last = merged[^1]; - var gap = At(piece.Left).CompareTo(At(last.Right)); - if (gap > 0 || gap == 0 && !last.RightClosed && !piece.LeftClosed) - { - merged.Add(piece); - continue; - } - var further = At(piece.Right).CompareTo(At(last.Right)); - merged[^1] = further > 0 ? (last.Left, last.LeftClosed, piece.Right, piece.RightClosed) - : further == 0 ? (last.Left, last.LeftClosed, last.Right, last.RightClosed || piece.RightClosed) - : last; - } - // Points on their own are one listed set; each interval stands where it lies. - var points = merged.Where(static piece => At(piece.Left).CompareTo(At(piece.Right)) == 0).Select(static piece => piece.Left).ToList(); - var intervals = merged.Where(static piece => At(piece.Left).CompareTo(At(piece.Right)) != 0) - .Select(static piece => (Entity)new Interval(piece.Left, piece.LeftClosed, piece.Right, piece.RightClosed)).ToList(); - if (intervals.Count == 0) - return new FiniteSet(points); - var union = intervals.Aggregate(static (left, right) => new Unionf(left, right)); - return points.Count == 0 ? union : new Unionf(new FiniteSet(points), union); - } + => set is Set numbers && SetOperators.CanonicalUnion(numbers) is { } canonical ? canonical : set; /// The membership a set builder's predicate declares, wherever it sits among the conjuncts, and the rest. private static (Set Declared, Entity Condition)? Declared(Entity predicate, Variable x) @@ -332,6 +283,13 @@ protected override Entity InnerSimplify(bool isExact) => ExpandOnTwoArguments(Left, Right, (a, b) => (a, b) switch { + // A union of numbers is written as its disjoint pieces in increasing order, + // a point at an open end closing it: x^2 >= x is solved to + // { 0, 1 } \/ (-oo; 0) \/ (1; +oo), which is (-oo; 0] \/ [1; +oo). The pieces + // meet pairwise below, and pieces that are not neighbours only here. + // https://github.com/asc-community/AngouriMath/issues/1409 + (Set unionLeft, Set unionRight) when (unionLeft is Unionf || unionRight is Unionf) + && SetOperators.CanonicalUnion(new Unionf(unionLeft, unionRight)) is { } canonical => canonical, (FiniteSet setLeft, Set setRight) => SetOperators.UniteFiniteSetAndSet(setLeft, setRight), (Set setLeft, FiniteSet setRight) => SetOperators.UniteFiniteSetAndSet(setRight, setLeft), (Interval intLeft, Interval intRight) => SetOperators.UniteIntervalAndInterval(intLeft, intRight), diff --git a/Sources/Tests/UnitTests/Core/Sets/UnionsMeetTest.cs b/Sources/Tests/UnitTests/Core/Sets/UnionsMeetTest.cs index 0ca0e2f74..69f596757 100644 --- a/Sources/Tests/UnitTests/Core/Sets/UnionsMeetTest.cs +++ b/Sources/Tests/UnitTests/Core/Sets/UnionsMeetTest.cs @@ -26,5 +26,23 @@ public sealed class UnionsMeetTest [InlineData("((0; (5 - sqrt(21))/2) \\/ ((5 + sqrt(21))/2; +oo)) /\\ ({ (3 - sqrt(5))/2 } \\/ [(3 - sqrt(5))/2; (3 + sqrt(5))/2])", "{ }")] public void PieceByPiece(string meeting, string expected) => Assert.Equal(expected.ToEntity().InnerSimplified, meeting.ToEntity().InnerSimplified); + + /// + /// A union of numbers is written as its disjoint pieces, the listed numbers first and the + /// intervals in increasing order, a point at an open end closing it and an empty interval + /// dropped, however it was grouped: two ways of writing one set come out the same. + /// + [Theory] + [InlineData("{ 0, 1 } \\/ (-oo; 0) \\/ (1; +oo)", "(-oo; 0] \\/ [1; +oo)")] + [InlineData("(2; 3) \\/ (0; 1) \\/ { 5 }", "{ 5 } \\/ (0; 1) \\/ (2; 3)")] + [InlineData("(0; 2) \\/ ((1; 3) \\/ (5; 6))", "(0; 3) \\/ (5; 6)")] + [InlineData("(3; 3) \\/ ((0; 1) \\/ { 7 })", "{ 7 } \\/ (0; 1)")] + public void AUnionOfNumbersIsWrittenAsItsPieces(string written, string pieces) + => Assert.Equal(pieces.ToEntity().InnerSimplified, written.ToEntity().InnerSimplified); + + /// What the inequality solver answers comes out as its pieces too. + [Fact] + public void ASolvedInequalityIsItsPieces() + => Assert.Equal("(-oo; 0] \\/ [1; +oo)".ToEntity().InnerSimplified, "x^2 >= x".ToEntity().Solve("x")); } }