Skip to content

Commit e8b2039

Browse files
A union with an empty interval is the other set (#1636)
Two intervals were joined wherever one ended where the other began, before either was asked whether it was empty: [0; 1] \/ [1; 0] became { 1 }, [3; 1] \/ [1; 2] the empty [3; 2], and [a; b] \/ [b; a] became { b }. An empty interval now adds nothing, and two intervals with ends that are not numbers are joined only where neither can be empty -- an end at an infinity, or a single point closed at both ends -- so [a; b] \/ [b; c] is left as written. A closed point's union was computed and discarded; it is returned now. Closes #1634. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent fac6ce1 commit e8b2039

3 files changed

Lines changed: 114 additions & 9 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,25 @@ reached 3^12 cases and 8 GB inside integration by parts
654654
| `(x < 0 and x = 0).Simplify()` | `False provided x in RR` | `False` — the condition was over-strong, one conjunct is false wherever `x` is |
655655
| `(x > 0 and x > 0).Evaled` | `x > 0` | `x > 0` (unchanged) |
656656

657+
### A union with an empty interval is the other set
658+
659+
**Wrong answer fixed.** Two intervals were joined wherever one ended where the other began, before
660+
either was asked whether it was empty. `[1; 0]` is empty, and it ends at 0 where `[0; 1]` begins,
661+
so `[0; 1] \/ [1; 0]` was joined into `{ 1 }`, and `[3; 1] \/ [1; 2]` into `[3; 2]`, which is
662+
empty. An empty interval now adds nothing. With ends that are not numbers either interval may be
663+
empty -- `[a; b]` is, when `a` is above `b` -- so two such intervals are no longer joined unless an
664+
end is infinite, and `[a; b] \/ [b; c]`, which was `[a; c]`, is left as written.
665+
[#1634](https://github.com/asc-community/AngouriMath/issues/1634). Both columns measured on a
666+
build, `v2.5.0` against this change.
667+
668+
| `"….".ToEntity().Simplify()` of | Was (2.5.0) | Is |
669+
|---|---|---|
670+
| `[0; 1] \/ [1; 0]` | `{ 1 }` — wrong | `[0; 1]` |
671+
| `[3; 1] \/ [1; 2]` | `[3; 2]`, which is empty — wrong | `[1; 2]` |
672+
| `[a; b] \/ [b; a]` | `{ b }` — wrong | `[a; b] \/ [b; a]` |
673+
| `[a; b] \/ [b; c]` | `[a; c]` — wrong where `a > b` or `b > c` | `[a; b] \/ [b; c]` |
674+
| `(-oo; x] \/ [x; +oo)` | `RR` | `RR` (unchanged: neither can be empty) |
675+
657676
### `ZZ*` and `ZZ+` are the non-negative and the positive integers
658677

659678
`ZZ*` = `{0, 1, 2, ...}` and `ZZ+` = `{1, 2, 3, ...}` are special sets, spelled as MathWorld spells

‎Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Union.cs‎

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,28 @@ internal static Set UniteFiniteSetAndSet(FiniteSet finite, Set set)
108108
return sb.IsEmpty ? set : sb.ToFiniteSet().Unite(set);
109109
}
110110

111-
// TODO: it requires cleaning
111+
/// <summary>
112+
/// The union of two intervals: one interval where they overlap or touch, and as written
113+
/// where they do not, or where that cannot be told.
114+
/// </summary>
115+
/// <remarks>
116+
/// An empty interval adds nothing -- one whose left end is above its right, or at it with
117+
/// an end open -- so <c>[0; 1] \/ [1; 0]</c> is <c>[0; 1]</c>. Two intervals that touch are
118+
/// one only when neither is empty. Touching was tested first, so <c>[1; 0]</c>, touching
119+
/// <c>[0; 1]</c> at 0, was joined to it into <c>{ 1 }</c>, and <c>[a; b] \/ [b; a]</c> into
120+
/// <c>{ b }</c>. Ends that are not numbers do not say whether an interval is empty, unless
121+
/// the ends are one expression closed at both or one end is infinite, as in
122+
/// <c>(-oo; x] \/ [x; +oo)</c>; otherwise the union is left as written.
123+
/// https://github.com/asc-community/AngouriMath/issues/1634
124+
/// </remarks>
112125
internal static Set UniteIntervalAndInterval(Interval A, Interval B)
113126
{
127+
if (IsEmpty(A))
128+
return B;
129+
if (IsEmpty(B))
130+
return A;
131+
if (!IsNotEmpty(A) || !IsNotEmpty(B))
132+
return A.Unite(B);
114133
if (A.Left == B.Right && (A.LeftClosed || B.RightClosed))
115134
return new Interval(B.Left, B.LeftClosed, A.Right, A.RightClosed);
116135
if (A.Right == B.Left && (A.RightClosed || B.LeftClosed))
@@ -120,14 +139,11 @@ A.Right is not Real aRight ||
120139
B.Left is not Real bLeft ||
121140
B.Right is not Real bRight)
122141
return A.Unite(B);
123-
if (aLeft == aRight && A.LeftClosed && A.RightClosed)
124-
UniteFiniteSetAndSet(new FiniteSet(aLeft), B);
125-
if (bLeft == bRight && B.LeftClosed && B.RightClosed)
126-
UniteFiniteSetAndSet(new FiniteSet(bLeft), A);
127-
if (aLeft >= aRight)
128-
return B;
129-
if (bLeft >= bRight)
130-
return A;
142+
// Neither is empty, so ends that are equal are a point closed at both.
143+
if (aLeft == aRight)
144+
return UniteFiniteSetAndSet(new FiniteSet(aLeft), B);
145+
if (bLeft == bRight)
146+
return UniteFiniteSetAndSet(new FiniteSet(bLeft), A);
131147
if (aLeft == bRight && !A.LeftClosed && !B.RightClosed)
132148
return A.Unite(B);
133149
if (bLeft == aRight && !B.LeftClosed && !A.RightClosed)
@@ -151,6 +167,22 @@ B.Left is not Real bLeft ||
151167
return new Interval(left, leftClosed, right, rightClosed);
152168
}
153169

170+
/// <summary>Known to be empty: numeric ends out of order, or at one point with an end open.</summary>
171+
private static bool IsEmpty(Interval interval)
172+
=> interval.Left is Real left && interval.Right is Real right
173+
&& (left > right || left == right && !(interval.LeftClosed && interval.RightClosed));
174+
175+
/// <summary>
176+
/// Known not to be empty: numeric ends in order, the same end at both sides and closed, or
177+
/// an end at an infinity, beyond any end the other side can have.
178+
/// </summary>
179+
private static bool IsNotEmpty(Interval interval)
180+
=> interval.Left is Real left && interval.Right is Real right
181+
? left < right || left == right && interval.LeftClosed && interval.RightClosed
182+
: interval.Left == interval.Right
183+
? interval.LeftClosed && interval.RightClosed
184+
: interval.Left == Real.NegativeInfinity || interval.Right == Real.PositiveInfinity;
185+
154186
internal static Set UniteCSetAndCSet(ConditionalSet intLeft, ConditionalSet intRight)
155187
{
156188
(intLeft, intRight) = MergeToOneVariable(intLeft, intRight);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using AngouriMath.Extensions;
9+
using Xunit;
10+
using static AngouriMath.Entity;
11+
12+
namespace AngouriMath.Tests.Core.Sets
13+
{
14+
/// <summary>
15+
/// An interval whose left end is above its right is empty, and a union with it is the other
16+
/// set. Two intervals were joined wherever they touched, before either was asked whether it was
17+
/// empty: <c>[0; 1] \/ [1; 0]</c> became <c>{ 1 }</c> and <c>[a; b] \/ [b; a]</c> became <c>{ b }</c>.
18+
/// https://github.com/asc-community/AngouriMath/issues/1634
19+
/// </summary>
20+
[Trait("Area", "Core")]
21+
public sealed class AnEmptyIntervalAddsNothingTest
22+
{
23+
private static Entity IsIn(string value, string set) => $"{value} in ({set})".ToEntity().Simplify();
24+
25+
/// <summary>The union is decided at points on both sides of every end, not read off its printed shape.</summary>
26+
[Theory]
27+
[InlineData("[0; 1] \\/ [1; 0]", "0, 1/2, 1", "-1, 3/2")]
28+
[InlineData("[1; 0] \\/ [0; 1]", "0, 1/2, 1", "-1, 3/2")]
29+
[InlineData("[3; 1] \\/ [1; 2]", "1, 3/2, 2", "0, 5/2, 3")]
30+
[InlineData("[0; 1] \\/ [1; 1)", "0, 1/2, 1", "-1, 3/2")]
31+
[InlineData("[2; 2] \\/ [0; 1]", "0, 1, 2", "-1, 3/2, 3")]
32+
[InlineData("[0; 1] \\/ [1; 2]", "0, 1, 2", "-1, 3")]
33+
public void TheUnionHasTheMembersOfItsNonEmptyParts(string union, string members, string others)
34+
{
35+
var simplified = union.ToEntity().Simplify().Stringize();
36+
foreach (var member in members.Split(", "))
37+
Assert.Equal(Boolean.True, IsIn(member, simplified));
38+
foreach (var other in others.Split(", "))
39+
Assert.Equal(Boolean.False, IsIn(other, simplified));
40+
}
41+
42+
/// <summary>
43+
/// With ends that are not numbers, either interval may be empty, so touching is not joining:
44+
/// the union is left as written. An end at an infinity leaves nothing to doubt.
45+
/// </summary>
46+
[Fact]
47+
public void EndsThatAreNotNumbersAreNotJoinedWhereEitherMayBeEmpty()
48+
{
49+
Assert.IsType<Set.Unionf>("[a; b] \\/ [b; a]".ToEntity().Simplify());
50+
Assert.IsType<Set.Unionf>("[a; b] \\/ [b; c]".ToEntity().Simplify());
51+
Assert.Equal("RR", "(-oo; x] \\/ [x; +oo)".ToEntity().Simplify().Stringize());
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)