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 @@ -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)`
Expand Down
66 changes: 66 additions & 0 deletions Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Union.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,72 @@ namespace AngouriMath.Core.Sets
{
internal static partial class SetOperators
{
/// <summary>
/// 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. <c>x^2 &gt;= x</c> is
/// solved to <c>{ 0, 1 } \/ (-oo; 0) \/ (1; +oo)</c>, which is <c>(-oo; 0] \/ [1; +oo)</c>,
/// and two ways of writing one set of numbers come out the same. <see langword="null"/>
/// where a piece is anything else, or the union is written so already.
/// https://github.com/asc-community/AngouriMath/issues/1409
/// </summary>
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,61 +131,12 @@ protected override Entity InnerSimplify(bool isExact)
}

/// <summary>
/// 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:
/// <c>{ 1, 2 } \/ (-oo; 1) \/ (2; +oo)</c> is <c>(-oo; 1] \/ [2; +oo)</c>. 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 <see cref="SetOperators.CanonicalUnion"/> writes
/// it, which is how it reads: the listing's pieces met one by one come out in the order
/// they met.
/// </summary>
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;

/// <summary>The membership a set builder's predicate declares, wherever it sits among the conjuncts, and the rest.</summary>
private static (Set Declared, Entity Condition)? Declared(Entity predicate, Variable x)
Expand Down Expand Up @@ -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),
Expand Down
18 changes: 18 additions & 0 deletions Sources/Tests/UnitTests/Core/Sets/UnionsMeetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
/// 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.
/// </summary>
[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);

/// <summary>What the inequality solver answers comes out as its pieces too.</summary>
[Fact]
public void ASolvedInequalityIsItsPieces()
=> Assert.Equal("(-oo; 0] \\/ [1; +oo)".ToEntity().InnerSimplified, "x^2 >= x".ToEntity().Solve("x"));
}
}
Loading