Skip to content

Commit 0aceeb5

Browse files
A difference of two intervals keeps the parts on either side (#2300)
A \ B for two intervals is now the part of A below B and the part above it, each an interval, a point or nothing, and each holding the end of B that B does not. The last three returns of SetSubtractIntervalAndInterval answered one interval from an end of A to the far end of B where B lay inside A, so [0; 10] \ (2; 3) was [0; 3]. Where B covered an end of A, they read the end of the cut from B's other end, so [0; 10] \ [-1; 3) was (3; 10]. And an end of A that B left was folded into a flag, so [0; 10] \ (0; 3) lost the point 0. Membership followed: 5 in ([0; 10] \ (2; 3)) was False. The ends are compared by their values, as the intersection compares them, and kept as written, so [0; sqrt(2)] \ [1; 2] is [0; 1) where it was left as written. Ends with no value decide only when they are the same and B holds every end A holds: (a; b) \ [a; b] is empty, where it was (a; b). [a; b] \ (a; b) is { a, b } where a < b and nothing where a > b, so it is left as written, where it was [a; b]. IntervalAndInterval checks the issue's table and a few more, and that the difference holds a point exactly where A does and B does not, over every order of four ends from 0 to 3 with each end open or closed, 4096 pairs at nine points each. Nineteen of the new tests fail on master. 2.6.0 answers as master does. The suite, the trio, F#, AOT and the eight harnesses pass. Fixes #2290. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent c633c41 commit 0aceeb5

3 files changed

Lines changed: 168 additions & 29 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,28 @@ but 0, and `log`'s own condition already excluded it. A base that is small but n
884884
| `"log(x, 2)".ToEntity().Compile("x").Call(0)` | `(-0, -0)` | `(NaN, 0)` |
885885
| `"log(x, 1)".ToEntity().Simplify()` | `0 provided not x = 1` | `0 provided not x = 0 and not x = 1` |
886886

887+
### A difference of two intervals keeps the parts on either side
888+
889+
**Silent: `A \ B` for two intervals is the part of `A` below `B` and the part above it, each holding the end `B`
890+
leaves.** It was one interval, from an end of `A` to the far end of `B`, where `B` lay inside `A`. Where `B` covered
891+
an end of `A`, the end of the cut was read from `B`'s other end. And an end of `A` that `B` left out was lost.
892+
Membership follows the set, so `5 in ([0; 10] \ (2; 3))` was `False`. Ends such as `sqrt(2)` are compared by their
893+
values, where the difference was left as written. With ends that have no value, `[a; b] \ (a; b)` is left as written,
894+
since it is `{ a, b }` where `a < b` and nothing where `a > b`
895+
([#2290](https://github.com/asc-community/AngouriMath/issues/2290)).
896+
897+
| Input | Was (2.6.0) | Now |
898+
|---|---|---|
899+
| `"[0; 10] \\ (2; 3)".ToEntity().Simplify()` | `[0; 3]` | `[0; 2] \/ [3; 10]` |
900+
| `"[0; 10] \\ [-1; 3)".ToEntity().Simplify()` | `(3; 10]` | `[3; 10]` |
901+
| `"[0; 10] \\ (0; 3)".ToEntity().Simplify()` | `[3; 10]` | `{ 0 } \/ [3; 10]` |
902+
| `"[0; 10] \\ (0; 10)".ToEntity().Simplify()` | `[0; 10]` | `{ 0, 10 }` |
903+
| `"(-oo; +oo) \\ [0; 1)".ToEntity().Simplify()` | `(-oo; 1)` | `(-oo; 0) \/ [1; +oo)` |
904+
| `"[0; sqrt(2)] \\ [1; 2]".ToEntity().Simplify()` | `[0; sqrt(2)] \ [1; 2]` | `[0; 1)` |
905+
| `"(a; b) \\ [a; b]".ToEntity().Simplify()` | `(a; b)` | `{ }` |
906+
| `"[a; b] \\ (a; b)".ToEntity().Simplify()` | `[a; b]` | `[a; b] \ (a; b)` |
907+
| `"5 in ([0; 10] \\ (2; 3))".ToEntity().Simplify()` | `False` | `True` |
908+
887909
### `re` and `im` are the real and the imaginary part
888910

889911
**Not silent: `re(…)` and `im(…)`, and `real(…)` and `imag(…)`, are now the real and the imaginary part, where they were

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

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,67 @@ internal static Set SetSubtractSetAndFiniteSet(Set set, FiniteSet finite)
2626
private static bool Implies(this bool assumption, bool conclusion)
2727
=> !assumption || conclusion;
2828

29+
/// <summary>
30+
/// What is left of <paramref name="A"/> with <paramref name="B"/> taken out: the part of
31+
/// <paramref name="A"/> below <paramref name="B"/> and the part above it, each an
32+
/// interval, a point or nothing.
33+
/// </summary>
34+
/// <remarks>
35+
/// Each part ends where <paramref name="B"/> begins, and holds that end where
36+
/// <paramref name="B"/> does not: <c>[0; 10] \ (2; 3)</c> is <c>[0; 2] \/ [3; 10]</c>,
37+
/// and <c>[0; 10] \ (0; 3)</c> is <c>{ 0 } \/ [3; 10]</c>. One interval from an end of
38+
/// <paramref name="A"/> to the far end of <paramref name="B"/> was answered for the first,
39+
/// <c>[0; 3]</c>, the end of the cut was read from the other end of
40+
/// <paramref name="B"/>, and an end of <paramref name="A"/> that <paramref name="B"/> left
41+
/// was lost. The ends are compared by their values, as the intersection compares them,
42+
/// and kept as written. Where an end has no value the difference is left as it is, unless
43+
/// the ends are the same and <paramref name="B"/> holds every end <paramref name="A"/>
44+
/// holds; then it holds all of <paramref name="A"/>. Where <paramref name="A"/> holds an
45+
/// end that <paramref name="B"/> does not, what is left depends on the order of the ends:
46+
/// <c>[a; b] \ (a; b)</c> is <c>{ a, b }</c> where <c>a &lt; b</c> and nothing where
47+
/// <c>a &gt; b</c>.
48+
/// https://github.com/asc-community/AngouriMath/issues/2290
49+
/// </remarks>
2950
internal static Set SetSubtractIntervalAndInterval(Interval A, Interval B)
3051
{
3152
if (A == B)
3253
return Empty;
33-
if (A.Left == B.Left && A.Right == B.Right)
34-
return new Interval(A.Left, A.LeftClosed && !B.LeftClosed, A.Right, A.RightClosed && !B.RightClosed);
35-
if (A.Left is not Real aLeft ||
36-
A.Right is not Real aRight ||
37-
B.Left is not Real bLeft ||
38-
B.Right is not Real bRight)
54+
if (A.Left == B.Left && A.Right == B.Right && A.LeftClosed.Implies(B.LeftClosed) && A.RightClosed.Implies(B.RightClosed))
55+
return Empty;
56+
if (A.Left.Evaled is not Real { IsNaN: false } aLeft ||
57+
A.Right.Evaled is not Real { IsNaN: false } aRight ||
58+
B.Left.Evaled is not Real { IsNaN: false } bLeft ||
59+
B.Right.Evaled is not Real { IsNaN: false } bRight)
3960
return A.SetSubtract(B);
40-
41-
if (aLeft > aRight)
61+
if (aLeft > aRight || aLeft == aRight && !(A.LeftClosed && A.RightClosed))
4262
return Empty;
43-
if (bLeft > bRight)
44-
return A;
45-
if (aLeft == bRight)
46-
return B.RightClosed ? A.New(A.Left, false, A.Right, A.RightClosed) : A;
47-
if (aRight == bLeft)
48-
return B.LeftClosed ? A.New(A.Left, A.LeftClosed, A.Right, false) : A;
49-
if (aRight < bLeft || aLeft > bRight)
63+
if (bLeft > bRight || bLeft == bRight && !(B.LeftClosed && B.RightClosed))
5064
return A;
51-
if (
52-
(bLeft < aLeft || bLeft == aLeft && A.LeftClosed.Implies(B.LeftClosed))
53-
&&
54-
(bRight > aRight || bRight == aRight && A.RightClosed.Implies(B.RightClosed))
55-
)
56-
return Empty;
57-
if (aLeft >= bLeft && aRight >= bRight)
58-
return new Interval(bRight, aLeft == bLeft ? A.LeftClosed && !B.LeftClosed : !B.LeftClosed,
59-
aRight, aRight == bRight ? A.RightClosed && !B.RightClosed : A.RightClosed);
60-
if (aLeft <= bLeft && aRight <= bRight)
61-
return new Interval(aLeft, aLeft == bLeft ? A.LeftClosed && !B.LeftClosed : A.LeftClosed,
62-
bLeft, aRight == bRight ? A.RightClosed && !B.RightClosed : !B.RightClosed);
63-
return new Interval(aLeft, aLeft == bLeft ? A.LeftClosed && !B.LeftClosed : !B.LeftClosed,
64-
bRight, aRight == bRight ? A.RightClosed && !B.RightClosed : A.RightClosed);
65+
// Below B: from A's left end to whichever comes first, A's right end or B's left end.
66+
var (belowEnd, belowEndValue, belowEndClosed) =
67+
aRight < bLeft ? (A.Right, aRight, A.RightClosed) :
68+
bLeft < aRight ? (B.Left, bLeft, !B.LeftClosed) :
69+
(A.Right, aRight, A.RightClosed && !B.LeftClosed);
70+
// Above B: from whichever comes last, A's left end or B's right end, to A's right end.
71+
var (aboveStart, aboveStartValue, aboveStartClosed) =
72+
aLeft > bRight ? (A.Left, aLeft, A.LeftClosed) :
73+
bRight > aLeft ? (B.Right, bRight, !B.RightClosed) :
74+
(A.Left, aLeft, A.LeftClosed && !B.RightClosed);
75+
var below = Part(A.Left, aLeft, A.LeftClosed, belowEnd, belowEndValue, belowEndClosed);
76+
var above = Part(aboveStart, aboveStartValue, aboveStartClosed, A.Right, aRight, A.RightClosed);
77+
return (below, above) switch
78+
{
79+
(null, null) => Empty,
80+
({ } low, null) => low,
81+
(null, { } high) => high,
82+
(FiniteSet { Count: 1 } low, FiniteSet { Count: 1 } high) => new FiniteSet(low.First(), high.First()),
83+
({ } low, { } high) => low.Unite(high),
84+
};
85+
86+
static Set? Part(Entity left, Real leftValue, bool leftClosed, Entity right, Real rightValue, bool rightClosed)
87+
=> leftValue < rightValue ? new Interval(left, leftClosed, right, rightClosed)
88+
: leftValue == rightValue && leftClosed && rightClosed ? new FiniteSet(left)
89+
: null;
6590
}
6691

6792
internal static Set SetSubtractCSetAndCSet(ConditionalSet intLeft, ConditionalSet intRight)

‎Sources/Tests/UnitTests/Core/Sets/IntervalAndInterval.cs‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77

88
using AngouriMath;
9+
using System.Collections.Generic;
10+
using System.Linq;
911
using Xunit;
1012
using static AngouriMath.Entity;
1113
using static AngouriMath.Entity.Set;
@@ -86,6 +88,96 @@ public void TwoRaysMeetWhateverTheirEnds(string intersection, string expected)
8688
[Fact] public void Subtraction5() => TestInt(A.SetSubtract(F), Interval(2, true, 3, false));
8789
[Fact] public void Subtraction6() => TestInt(F.SetSubtract(A), Interval(5, false, 6, true));
8890

91+
/// <summary>
92+
/// What is left of an interval with another taken out is the part below it and the part
93+
/// above it, and each part holds the end the second one leaves. One interval from an end
94+
/// of the first to the far end of the second used to be answered, with its ends read from
95+
/// the wrong side. https://github.com/asc-community/AngouriMath/issues/2290
96+
/// </summary>
97+
[Theory]
98+
[InlineData("[0; 10] \\ (2; 3)", "[0; 2] \\/ [3; 10]")]
99+
[InlineData("[0; 10] \\ [2; 3]", "[0; 2) \\/ (3; 10]")]
100+
[InlineData("(0; 10) \\ (2; 3)", "(0; 2] \\/ [3; 10)")]
101+
[InlineData("[0; 10] \\ [-1; 3)", "[3; 10]")]
102+
[InlineData("[0; 10] \\ (-1; 3]", "(3; 10]")]
103+
[InlineData("[0; 10] \\ (5; 12]", "[0; 5]")]
104+
[InlineData("[0; 10] \\ [5; 12)", "[0; 5)")]
105+
[InlineData("[0; 10] \\ (0; 3)", "{ 0 } \\/ [3; 10]")]
106+
[InlineData("[0; 10] \\ (7; 10)", "[0; 7] \\/ { 10 }")]
107+
[InlineData("[0; 10] \\ (0; 10)", "{ 0, 10 }")]
108+
[InlineData("[0; 10] \\ [0; 10)", "{ 10 }")]
109+
[InlineData("[0; 10] \\ [5; 5]", "[0; 10] \\ { 5 }")]
110+
[InlineData("[0; 10] \\ (5; 5)", "[0; 10]")]
111+
[InlineData("[0; sqrt(2)] \\ [1; 2]", "[0; 1)")]
112+
[InlineData("(-oo; +oo) \\ [0; 1)", "(-oo; 0) \\/ [1; +oo)")]
113+
public void ADifferenceKeepsThePartsOnEitherSide(string difference, string expected)
114+
=> Assert.Equal(MathS.FromString(expected).Simplify(), MathS.FromString(difference).Simplify());
115+
116+
/// <summary>
117+
/// The difference holds a point exactly where the first interval does and the second does
118+
/// not, over every order of the four ends, with each end open or closed, and with either
119+
/// interval reversed or at a single point. Two intervals that each have more than one point
120+
/// are subtracted to a set without a difference in it; one that has a point or none is a
121+
/// listed set by then, and the difference may stay.
122+
/// https://github.com/asc-community/AngouriMath/issues/2290
123+
/// </summary>
124+
[Fact]
125+
public void ADifferenceHoldsWhatTheFirstHoldsAndTheSecondDoesNot()
126+
{
127+
var points = Enumerable.Range(-1, 9).Select(k => (Entity)Entity.Number.Rational.Create(k, 2)).ToArray();
128+
var failures = new List<string>();
129+
foreach (var aLeft in Enumerable.Range(0, 4))
130+
foreach (var aRight in Enumerable.Range(0, 4))
131+
foreach (var bLeft in Enumerable.Range(0, 4))
132+
foreach (var bRight in Enumerable.Range(0, 4))
133+
foreach (var flags in Enumerable.Range(0, 16))
134+
{
135+
var a = Interval(aLeft, (flags & 1) != 0, aRight, (flags & 2) != 0);
136+
var b = Interval(bLeft, (flags & 4) != 0, bRight, (flags & 8) != 0);
137+
var difference = a.SetSubtract(b).InnerSimplified;
138+
if (difference is not Set left || left is SetMinusf && aLeft < aRight && bLeft < bRight)
139+
{
140+
failures.Add($"{a} \\ {b} is not decided: {difference}");
141+
continue;
142+
}
143+
foreach (var point in points)
144+
{
145+
var expected = Holds(a, point) && !Holds(b, point);
146+
if (!left.TryContains(point, out var contains) || contains != expected)
147+
failures.Add($"{point} in {a} \\ {b} = {left}: expected {expected}");
148+
}
149+
}
150+
Assert.Empty(failures);
151+
152+
static bool Holds(Interval interval, Entity point)
153+
{
154+
var value = (Entity.Number.Real)point.EvalNumerical();
155+
var (left, right) = ((Entity.Number.Real)interval.Left.EvalNumerical(), (Entity.Number.Real)interval.Right.EvalNumerical());
156+
return (left < value || interval.LeftClosed && left == value) && (value < right || interval.RightClosed && value == right);
157+
}
158+
}
159+
160+
/// <summary>
161+
/// Ends that have no value say whether anything is left only when they are the same and
162+
/// the second interval holds every end the first holds. <c>[a; b] \ (a; b)</c> is
163+
/// <c>{ a, b }</c> where <c>a &lt; b</c> and nothing where <c>a &gt; b</c>, so it stays as
164+
/// it is, and it used to be answered <c>[a; b]</c>.
165+
/// https://github.com/asc-community/AngouriMath/issues/2290
166+
/// </summary>
167+
[Theory]
168+
[InlineData("[a; b] \\ (a; b)")]
169+
[InlineData("[a; b] \\ [a; b)")]
170+
[InlineData("(a; b] \\ (a; b)")]
171+
public void ADifferenceOfSymbolicIntervalsWithAnEndLeftStays(string difference)
172+
=> Assert.IsType<SetMinusf>(MathS.FromString(difference).InnerSimplified);
173+
174+
[Theory]
175+
[InlineData("(a; b) \\ [a; b]")]
176+
[InlineData("[a; b) \\ [a; b]")]
177+
[InlineData("(a; b] \\ (a; b]")]
178+
public void ADifferenceOfSymbolicIntervalsThatCoversIsEmpty(string difference)
179+
=> Assert.Equal(Set.Empty, MathS.FromString(difference).InnerSimplified);
180+
89181
/// <summary>
90182
/// An interval whose ends are reversed holds nothing: it meets nothing, and adds nothing to
91183
/// a union. The meeting used to be the other interval.

0 commit comments

Comments
 (0)