Skip to content

Commit 70b4be6

Browse files
Two piecewises split on one quantity combine sign by sign (#1686)
* Two piecewises split on one quantity combine sign by sign A piecewise combined with a piecewise joins every case to every other. The integrator's answers for a rational function with symbols in it are split on the sign of a quantity -- `t^k/(d - f t^2)^4` on that of `d/f` -- and a sum of them kept the pairs of opposite strict signs, `q > 0 and q < 0`, which are NaN rather than False off the real line, and the pairs where a vanishing coefficient met a case that divides by it, `f = 0 and not 2 f = 0`. Six such terms made 32,769 cases, and `x sqrt(1 + d x) sqrt(1 + f x)` ran out of time. A quantity is now read up to a constant factor, and a pair asking it to be zero and not zero, or zero and of a sign, goes: that is false for every value, complex or not. A pair asking it to be both positive and negative goes where every later pair tests it too: those are false or undecided off the real line, never true, so what the undecided pair stopped is not reached without it either. The sum stays at three cases, and the integral answers in a second. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * A breaking-changes entry for two piecewises combined sign by sign Measured on the 2.5.0 tag, master 94ba5ff and this branch. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent c3f5269 commit 70b4be6

4 files changed

Lines changed: 148 additions & 9 deletions

File tree

‎BREAKING-CHANGES.md‎

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

669+
### Two piecewises split on one quantity combine sign by sign
670+
671+
A sum or product of two piecewises still joins every case's condition to every other's, and now
672+
drops two more kinds of pair. A quantity is read up to a constant factor, so `f = 0 and not 2 f = 0`
673+
is `False`. And `q > 0 and q < 0`, which is `NaN` off the real line rather than `False`, goes where
674+
every case after it tests `q` too: off the real line each of those is false or undecided, so no case
675+
it was keeping from being reached could be. Where a later case tests something else it stays. Sums
676+
of the answers the radical substitution gives for `x sqrt(1 + d x) sqrt(1 + f x)` had reached
677+
32,769 cases ([#718](https://github.com/asc-community/AngouriMath/issues/718)).
678+
679+
| Input | Was (2.5.0) | Now |
680+
|---|---|---|
681+
| `piecewise(1 provided q = 0, 2 provided q > 0, 3 provided q < 0) + piecewise(10 provided q = 0, 20 provided q > 0, 30 provided q < 0)`, `.InnerSimplified` | nine cases | three: `11 provided q = 0`, `22 provided q > 0`, `33 provided q < 0` |
682+
| `piecewise(1 provided f = 0, 2 provided not 2 * f = 0) + piecewise(10 provided 3 * f = 0, 20 provided not f = 0)`, `.InnerSimplified` | four cases | two |
683+
| the same with a third case `provided p > 0` in each | nine cases | nine, unchanged |
684+
| `"x*sqrt(1 + d*x)*sqrt(1 + f*x)".Integrate("x")`, and with `x^2`, or `A + B x + K x^2`, for `x` | left unevaluated | the antiderivative, a piecewise of three cases |
685+
669686
### A union with an empty interval is the other set
670687

671688
**Wrong answer fixed.** Two intervals were joined wherever one ended where the other began, before

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Classes.cs‎

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,103 @@ Entity ops(Entity a, Entity b)
140140
private Entity CombinedCaseByCase(Piecewise a, Piecewise b,
141141
Func<Entity, Entity, Entity?> operation, Func<Entity, Entity, Entity, Entity> defaultCtor, bool isExact)
142142
{
143-
var cases = new List<Providedf>();
143+
// Every pair of cases, in order, and its conjunction; the expressions are combined only
144+
// for the pairs that are kept.
145+
var pairs = new List<(Providedf First, Providedf Second, Entity Predicate, Entity? Undecided)>();
144146
foreach (var (c1, c2) in (a.Cases, b.Cases).EachForEach())
145147
{
146148
var predicate = (c1.Predicate & c2.Predicate).InnerSimplified;
147149
if (predicate == Boolean.False)
148150
continue;
149-
cases.Add((ExpandOnTwoArguments(c1.Expression, c2.Expression, operation, defaultCtor, isExact), predicate).ToProvided());
151+
switch (Contradiction(predicate))
152+
{
153+
case (true, _):
154+
continue;
155+
case (false, var quantity):
156+
pairs.Add((c1, c2, predicate, quantity));
157+
break;
158+
}
150159
}
160+
// A pair that asks a quantity to be both positive and negative is false for a real
161+
// one and undecided off the real line, where it would stop the cases after it from
162+
// being reached. It goes only where every later pair tests that quantity too: those
163+
// are each false or undecided off the real line, never true, so nothing it stopped
164+
// is reached without it. A sum of piecewises split on the sign of `d/f` -- the
165+
// integrator's answer for `x sqrt(1 + d x) sqrt(1 + f x)` -- otherwise doubled its
166+
// cases with every term: 32,769 for six terms of `t^k/(d - f t^2)^4`.
167+
// https://github.com/asc-community/AngouriMath/issues/718
168+
var keep = new bool[pairs.Count];
169+
for (var i = pairs.Count - 1; i >= 0; i--)
170+
keep[i] = pairs[i].Undecided is not { } quantity
171+
|| !Enumerable.Range(i + 1, pairs.Count - i - 1).All(later => !keep[later] || TestsTheSignOf(pairs[later].Predicate, quantity));
172+
var cases = new List<Providedf>();
173+
for (var i = 0; i < pairs.Count; i++)
174+
if (keep[i])
175+
cases.Add((ExpandOnTwoArguments(pairs[i].First.Expression, pairs[i].Second.Expression, operation, defaultCtor, isExact), pairs[i].Predicate).ToProvided());
151176
return MathS.Piecewise(cases);
152177
}
153178

179+
/// <summary>
180+
/// Whether <paramref name="predicate"/>, a conjunction, asks one quantity <c>q</c> to be
181+
/// zero and not zero, or zero and of a sign -- false for every value, complex or not -- in
182+
/// <c>Always</c>; and otherwise the quantity it asks to be both positive and negative,
183+
/// which is false for a real one and undecided off the real line, or null. A quantity is
184+
/// read up to a constant factor, which only the direction of a sign depends on:
185+
/// <c>f = 0</c> and <c>not 2 f = 0</c> contradict each other.
186+
/// </summary>
187+
private static (bool Always, Entity? Undecided) Contradiction(Entity predicate)
188+
{
189+
var tests = new List<(Entity Quantity, int Relation)>();
190+
foreach (var conjunct in Conjuncts(predicate))
191+
if (SignTest(conjunct) is { } test)
192+
tests.Add(test);
193+
Entity? undecided = null;
194+
for (var i = 0; i < tests.Count; i++)
195+
for (var j = i + 1; j < tests.Count; j++)
196+
{
197+
if (tests[i].Quantity != tests[j].Quantity)
198+
continue;
199+
var (r1, r2) = (tests[i].Relation, tests[j].Relation);
200+
// 0 is "= 0", 2 is "not = 0", 1 and -1 the signs.
201+
if (r1 == 0 && r2 != 0 || r2 == 0 && r1 != 0)
202+
return (true, null);
203+
if (r1 * r2 == -1)
204+
undecided = tests[i].Quantity;
205+
}
206+
return (false, undecided);
207+
}
208+
209+
/// <summary>Whether <paramref name="predicate"/> has a conjunct testing <paramref name="quantity"/> against zero.</summary>
210+
private static bool TestsTheSignOf(Entity predicate, Entity quantity)
211+
=> Conjuncts(predicate).Any(conjunct => SignTest(conjunct) is var (q, relation) && q == quantity && relation != 2);
212+
213+
/// <summary>
214+
/// A comparison of a quantity with zero as the quantity, with any constant factor taken
215+
/// out, and the relation: 0 for <c>= 0</c>, 2 for <c>not = 0</c>, and the sign for
216+
/// <c>&gt; 0</c> and <c>&lt; 0</c>, turned by a negative factor. Null for anything else.
217+
/// </summary>
218+
private static (Entity Quantity, int Relation)? SignTest(Entity conjunct)
219+
{
220+
(Entity, int)? read = conjunct switch
221+
{
222+
Equalsf(var q, var zero) when zero == Integer.Zero => (q, 0),
223+
Equalsf(var zero, var q) when zero == Integer.Zero => (q, 0),
224+
Notf(Equalsf(var q, var zero)) when zero == Integer.Zero => (q, 2),
225+
Notf(Equalsf(var zero, var q)) when zero == Integer.Zero => (q, 2),
226+
Greaterf(var q, var zero) when zero == Integer.Zero => (q, 1),
227+
Lessf(var zero, var q) when zero == Integer.Zero => (q, 1),
228+
Lessf(var q, var zero) when zero == Integer.Zero => (q, -1),
229+
Greaterf(var zero, var q) when zero == Integer.Zero => (q, -1),
230+
_ => null,
231+
};
232+
if (read is not var (quantity, relation))
233+
return null;
234+
// The constant factor out: `4 f (-d)` is `f (-d)`, and `2 f` is `f`.
235+
if (quantity is Mulf(Real factor, var rest) && !factor.IsZero)
236+
(quantity, relation) = (rest, relation is 1 or -1 && factor.IsNegative ? -relation : relation);
237+
return (quantity, relation);
238+
}
239+
154240
private Entity ExpandOnOneArgument(Entity expr, Func<Entity, Entity?> operation, Func<Entity, Entity, Entity> defaultCtor, bool isExact,
155241
bool propagateSet = true)
156242
{

‎Sources/Tests/UnitTests/Calculus/TwoLinearRadicalsIntegralTest.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public void TwoRootsOfTheSameOrder(string integrand)
7878
[InlineData("sqrt(a + b*x)*sqrt(c + d*x)")]
7979
[InlineData("x/(sqrt(a + b*x)*sqrt(c + d*x))")]
8080
[InlineData("1/(sqrt(a + b*x)*sqrt(c + d*x))")]
81+
// A power of x beside them: each term of the rational function in t is split on the
82+
// sign of the same quantity, and their sum combines sign by sign rather than pair by pair
83+
// (https://github.com/asc-community/AngouriMath/issues/718).
84+
[InlineData("x*sqrt(a + b*x)*sqrt(c + d*x)")]
85+
[InlineData("x^2*sqrt(a + b*x)*sqrt(c + d*x)")]
8186
public void SymbolicCoefficients(string integrand)
8287
=> DifferentiatesBackPinned(integrand, new[] { 0.3, 0.9, 1.7, 2.6 }, ("a", 1.7), ("b", 2.3), ("c", 0.6), ("d", 1.1));
8388

‎Sources/Tests/UnitTests/Core/ContradictoryConjunctionTest.cs‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,57 @@ private static int CaseCount(Entity piecewise)
7474
[Fact]
7575
public void TwoPiecewisesOnTheSameSplitCombineCaseByCase()
7676
{
77-
// Three matching pairs, and the two pairs of opposite strict signs, `q > 0 and q < 0`
78-
// and `q < 0 and q > 0`, which are NaN rather than False off the real line and so stay
79-
// (five cases, not nine). Everything else pairs an equality against a sign and is gone.
77+
// The three matching pairs. A pair that pairs an equality against a sign is False and
78+
// gone. The two pairs of opposite strict signs, `q > 0 and q < 0` and `q < 0 and q > 0`,
79+
// are NaN rather than False off the real line, where they would stop the cases after
80+
// them -- but every case after them tests q too, so none of those is true there either,
81+
// and they go as well: three cases, not nine.
82+
// https://github.com/asc-community/AngouriMath/issues/718
8083
var sum = "piecewise(1 provided q = 0, 2 provided q > 0, 3 provided q < 0) + piecewise(10 provided q = 0, 20 provided q > 0, 30 provided q < 0)".ToEntity().InnerSimplified;
81-
Assert.Equal(5, CaseCount(sum));
84+
Assert.Equal(3, CaseCount(sum));
8285
Assert.Equal(Number.Integer.Create(11), sum.Substitute("q", 0).Evaled);
8386
Assert.Equal(Number.Integer.Create(22), sum.Substitute("q", 1).Evaled);
8487
Assert.Equal(Number.Integer.Create(33), sum.Substitute("q", -1).Evaled);
88+
// Off the real line it has no value, as with every pair: no case is true there.
89+
Assert.IsNotType<Number.Integer>(sum.Substitute("q", "i").Evaled);
8590

86-
// And a chain of them stays at five cases rather than growing by a factor of three each
87-
// time: the two contradictory cases absorb every further sign rather than multiplying.
91+
// And a chain of them stays at three cases rather than growing by a factor of three each
92+
// time.
8893
var chain = sum;
8994
for (var i = 0; i < 6; i++)
9095
chain = (chain + "piecewise(1 provided q = 0, 2 provided q > 0, 3 provided q < 0)".ToEntity()).InnerSimplified;
91-
Assert.Equal(5, CaseCount(chain));
96+
Assert.Equal(3, CaseCount(chain));
9297
Assert.Equal(Number.Integer.Create(22 + 12), chain.Substitute("q", 1).Evaled);
9398
Assert.Equal(Number.Integer.Create(33 + 18), chain.Substitute("q", -1).Evaled);
9499
Assert.Equal(Number.Integer.Create(11 + 6), chain.Substitute("q", 0).Evaled);
95100
}
96101

102+
/// <summary>
103+
/// A quantity is read up to a constant factor: <c>f = 0</c> and <c>not 2 f = 0</c>
104+
/// contradict each other, which is how the integrator's case for a vanishing leading
105+
/// coefficient meets the cases that divide by it.
106+
/// </summary>
107+
[Fact]
108+
public void AConstantFactorIsTheSameQuantity()
109+
{
110+
var sum = "piecewise(1 provided f = 0, 2 provided not 2 * f = 0) + piecewise(10 provided 3 * f = 0, 20 provided not f = 0)".ToEntity().InnerSimplified;
111+
Assert.Equal(2, CaseCount(sum));
112+
Assert.Equal(Number.Integer.Create(11), sum.Substitute("f", 0).Evaled);
113+
Assert.Equal(Number.Integer.Create(22), sum.Substitute("f", 5).Evaled);
114+
}
115+
116+
/// <summary>
117+
/// ...and two opposite strict signs stay where a later case does not test the quantity:
118+
/// off the real line that case can be true, and the undecided pair is what keeps it from
119+
/// being reached.
120+
/// </summary>
121+
[Fact]
122+
public void OppositeSignsStayWhereALaterCaseDoesNotTestTheQuantity()
123+
{
124+
var sum = "piecewise(1 provided q > 0, 2 provided q < 0, 3 provided p > 0) + piecewise(10 provided q < 0, 20 provided q > 0, 30 provided p > 0)".ToEntity().InnerSimplified;
125+
Assert.Contains(Assert.IsType<Piecewise>(sum).Cases, @case => @case.Predicate.ToString().Contains("q > 0") && @case.Predicate.ToString().Contains("q < 0"));
126+
}
127+
97128
[Fact]
98129
public void TwoPiecewisesOnDifferentSplitsStillCombineEveryPair()
99130
{

0 commit comments

Comments
 (0)