From 839987794b84ee31951e4559242f45f21f07b01c Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 2 Oct 2026 06:34:33 +0000 Subject: [PATCH 1/2] 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) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- .../Evaluation/Evaluation.Classes.cs | 90 ++++++++++++++++++- .../Calculus/TwoLinearRadicalsIntegralTest.cs | 5 ++ .../Core/ContradictoryConjunctionTest.cs | 45 ++++++++-- 3 files changed, 131 insertions(+), 9 deletions(-) diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Classes.cs index 2e5d52620..857fc3595 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Classes.cs @@ -140,17 +140,103 @@ Entity ops(Entity a, Entity b) private Entity CombinedCaseByCase(Piecewise a, Piecewise b, Func operation, Func defaultCtor, bool isExact) { - var cases = new List(); + // Every pair of cases, in order, and its conjunction; the expressions are combined only + // for the pairs that are kept. + var pairs = new List<(Providedf First, Providedf Second, Entity Predicate, Entity? Undecided)>(); foreach (var (c1, c2) in (a.Cases, b.Cases).EachForEach()) { var predicate = (c1.Predicate & c2.Predicate).InnerSimplified; if (predicate == Boolean.False) continue; - cases.Add((ExpandOnTwoArguments(c1.Expression, c2.Expression, operation, defaultCtor, isExact), predicate).ToProvided()); + switch (Contradiction(predicate)) + { + case (true, _): + continue; + case (false, var quantity): + pairs.Add((c1, c2, predicate, quantity)); + break; + } } + // A pair that asks a quantity to be both positive and negative is false for a real + // one and undecided off the real line, where it would stop the cases after it from + // being reached. It goes only where every later pair tests that quantity too: those + // are each false or undecided off the real line, never true, so nothing it stopped + // is reached without it. A sum of piecewises split on the sign of `d/f` -- the + // integrator's answer for `x sqrt(1 + d x) sqrt(1 + f x)` -- otherwise doubled its + // cases with every term: 32,769 for six terms of `t^k/(d - f t^2)^4`. + // https://github.com/asc-community/AngouriMath/issues/718 + var keep = new bool[pairs.Count]; + for (var i = pairs.Count - 1; i >= 0; i--) + keep[i] = pairs[i].Undecided is not { } quantity + || !Enumerable.Range(i + 1, pairs.Count - i - 1).All(later => !keep[later] || TestsTheSignOf(pairs[later].Predicate, quantity)); + var cases = new List(); + for (var i = 0; i < pairs.Count; i++) + if (keep[i]) + cases.Add((ExpandOnTwoArguments(pairs[i].First.Expression, pairs[i].Second.Expression, operation, defaultCtor, isExact), pairs[i].Predicate).ToProvided()); return MathS.Piecewise(cases); } + /// + /// Whether , a conjunction, asks one quantity q to be + /// zero and not zero, or zero and of a sign -- false for every value, complex or not -- in + /// Always; and otherwise the quantity it asks to be both positive and negative, + /// which is false for a real one and undecided off the real line, or null. A quantity is + /// read up to a constant factor, which only the direction of a sign depends on: + /// f = 0 and not 2 f = 0 contradict each other. + /// + private static (bool Always, Entity? Undecided) Contradiction(Entity predicate) + { + var tests = new List<(Entity Quantity, int Relation)>(); + foreach (var conjunct in Conjuncts(predicate)) + if (SignTest(conjunct) is { } test) + tests.Add(test); + Entity? undecided = null; + for (var i = 0; i < tests.Count; i++) + for (var j = i + 1; j < tests.Count; j++) + { + if (tests[i].Quantity != tests[j].Quantity) + continue; + var (r1, r2) = (tests[i].Relation, tests[j].Relation); + // 0 is "= 0", 2 is "not = 0", 1 and -1 the signs. + if (r1 == 0 && r2 != 0 || r2 == 0 && r1 != 0) + return (true, null); + if (r1 * r2 == -1) + undecided = tests[i].Quantity; + } + return (false, undecided); + } + + /// Whether has a conjunct testing against zero. + private static bool TestsTheSignOf(Entity predicate, Entity quantity) + => Conjuncts(predicate).Any(conjunct => SignTest(conjunct) is var (q, relation) && q == quantity && relation != 2); + + /// + /// A comparison of a quantity with zero as the quantity, with any constant factor taken + /// out, and the relation: 0 for = 0, 2 for not = 0, and the sign for + /// > 0 and < 0, turned by a negative factor. Null for anything else. + /// + private static (Entity Quantity, int Relation)? SignTest(Entity conjunct) + { + (Entity, int)? read = conjunct switch + { + Equalsf(var q, var zero) when zero == Integer.Zero => (q, 0), + Equalsf(var zero, var q) when zero == Integer.Zero => (q, 0), + Notf(Equalsf(var q, var zero)) when zero == Integer.Zero => (q, 2), + Notf(Equalsf(var zero, var q)) when zero == Integer.Zero => (q, 2), + Greaterf(var q, var zero) when zero == Integer.Zero => (q, 1), + Lessf(var zero, var q) when zero == Integer.Zero => (q, 1), + Lessf(var q, var zero) when zero == Integer.Zero => (q, -1), + Greaterf(var zero, var q) when zero == Integer.Zero => (q, -1), + _ => null, + }; + if (read is not var (quantity, relation)) + return null; + // The constant factor out: `4 f (-d)` is `f (-d)`, and `2 f` is `f`. + if (quantity is Mulf(Real factor, var rest) && !factor.IsZero) + (quantity, relation) = (rest, relation is 1 or -1 && factor.IsNegative ? -relation : relation); + return (quantity, relation); + } + private Entity ExpandOnOneArgument(Entity expr, Func operation, Func defaultCtor, bool isExact, bool propagateSet = true) { diff --git a/Sources/Tests/UnitTests/Calculus/TwoLinearRadicalsIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/TwoLinearRadicalsIntegralTest.cs index 97625e76b..acde0313e 100644 --- a/Sources/Tests/UnitTests/Calculus/TwoLinearRadicalsIntegralTest.cs +++ b/Sources/Tests/UnitTests/Calculus/TwoLinearRadicalsIntegralTest.cs @@ -78,6 +78,11 @@ public void TwoRootsOfTheSameOrder(string integrand) [InlineData("sqrt(a + b*x)*sqrt(c + d*x)")] [InlineData("x/(sqrt(a + b*x)*sqrt(c + d*x))")] [InlineData("1/(sqrt(a + b*x)*sqrt(c + d*x))")] + // A power of x beside them: each term of the rational function in t is split on the + // sign of the same quantity, and their sum combines sign by sign rather than pair by pair + // (https://github.com/asc-community/AngouriMath/issues/718). + [InlineData("x*sqrt(a + b*x)*sqrt(c + d*x)")] + [InlineData("x^2*sqrt(a + b*x)*sqrt(c + d*x)")] public void SymbolicCoefficients(string integrand) => DifferentiatesBackPinned(integrand, new[] { 0.3, 0.9, 1.7, 2.6 }, ("a", 1.7), ("b", 2.3), ("c", 0.6), ("d", 1.1)); diff --git a/Sources/Tests/UnitTests/Core/ContradictoryConjunctionTest.cs b/Sources/Tests/UnitTests/Core/ContradictoryConjunctionTest.cs index 5b062f427..9a1480b9f 100644 --- a/Sources/Tests/UnitTests/Core/ContradictoryConjunctionTest.cs +++ b/Sources/Tests/UnitTests/Core/ContradictoryConjunctionTest.cs @@ -74,26 +74,57 @@ private static int CaseCount(Entity piecewise) [Fact] public void TwoPiecewisesOnTheSameSplitCombineCaseByCase() { - // Three matching pairs, and the two pairs of opposite strict signs, `q > 0 and q < 0` - // and `q < 0 and q > 0`, which are NaN rather than False off the real line and so stay - // (five cases, not nine). Everything else pairs an equality against a sign and is gone. + // The three matching pairs. A pair that pairs an equality against a sign is False and + // gone. The two pairs of opposite strict signs, `q > 0 and q < 0` and `q < 0 and q > 0`, + // are NaN rather than False off the real line, where they would stop the cases after + // them -- but every case after them tests q too, so none of those is true there either, + // and they go as well: three cases, not nine. + // https://github.com/asc-community/AngouriMath/issues/718 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; - Assert.Equal(5, CaseCount(sum)); + Assert.Equal(3, CaseCount(sum)); Assert.Equal(Number.Integer.Create(11), sum.Substitute("q", 0).Evaled); Assert.Equal(Number.Integer.Create(22), sum.Substitute("q", 1).Evaled); Assert.Equal(Number.Integer.Create(33), sum.Substitute("q", -1).Evaled); + // Off the real line it has no value, as with every pair: no case is true there. + Assert.IsNotType(sum.Substitute("q", "i").Evaled); - // And a chain of them stays at five cases rather than growing by a factor of three each - // time: the two contradictory cases absorb every further sign rather than multiplying. + // And a chain of them stays at three cases rather than growing by a factor of three each + // time. var chain = sum; for (var i = 0; i < 6; i++) chain = (chain + "piecewise(1 provided q = 0, 2 provided q > 0, 3 provided q < 0)".ToEntity()).InnerSimplified; - Assert.Equal(5, CaseCount(chain)); + Assert.Equal(3, CaseCount(chain)); Assert.Equal(Number.Integer.Create(22 + 12), chain.Substitute("q", 1).Evaled); Assert.Equal(Number.Integer.Create(33 + 18), chain.Substitute("q", -1).Evaled); Assert.Equal(Number.Integer.Create(11 + 6), chain.Substitute("q", 0).Evaled); } + /// + /// A quantity is read up to a constant factor: f = 0 and not 2 f = 0 + /// contradict each other, which is how the integrator's case for a vanishing leading + /// coefficient meets the cases that divide by it. + /// + [Fact] + public void AConstantFactorIsTheSameQuantity() + { + 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; + Assert.Equal(2, CaseCount(sum)); + Assert.Equal(Number.Integer.Create(11), sum.Substitute("f", 0).Evaled); + Assert.Equal(Number.Integer.Create(22), sum.Substitute("f", 5).Evaled); + } + + /// + /// ...and two opposite strict signs stay where a later case does not test the quantity: + /// off the real line that case can be true, and the undecided pair is what keeps it from + /// being reached. + /// + [Fact] + public void OppositeSignsStayWhereALaterCaseDoesNotTestTheQuantity() + { + 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; + Assert.Contains(Assert.IsType(sum).Cases, @case => @case.Predicate.ToString().Contains("q > 0") && @case.Predicate.ToString().Contains("q < 0")); + } + [Fact] public void TwoPiecewisesOnDifferentSplitsStillCombineEveryPair() { From dc748778c272f4e6dcc154e912c77b5e47e6d41b Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 2 Oct 2026 07:56:05 +0000 Subject: [PATCH 2/2] A breaking-changes entry for two piecewises combined sign by sign Measured on the 2.5.0 tag, master 94ba5ff6 and this branch. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index b5268044d..ef8dc71cd 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -654,6 +654,23 @@ reached 3^12 cases and 8 GB inside integration by parts | `(x < 0 and x = 0).Simplify()` | `False provided x in RR` | `False` — the condition was over-strong, one conjunct is false wherever `x` is | | `(x > 0 and x > 0).Evaled` | `x > 0` | `x > 0` (unchanged) | +### Two piecewises split on one quantity combine sign by sign + +A sum or product of two piecewises still joins every case's condition to every other's, and now +drops two more kinds of pair. A quantity is read up to a constant factor, so `f = 0 and not 2 f = 0` +is `False`. And `q > 0 and q < 0`, which is `NaN` off the real line rather than `False`, goes where +every case after it tests `q` too: off the real line each of those is false or undecided, so no case +it was keeping from being reached could be. Where a later case tests something else it stays. Sums +of the answers the radical substitution gives for `x sqrt(1 + d x) sqrt(1 + f x)` had reached +32,769 cases ([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `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` | +| `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 | +| the same with a third case `provided p > 0` in each | nine cases | nine, unchanged | +| `"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 | + ### A union with an empty interval is the other set **Wrong answer fixed.** Two intervals were joined wherever one ended where the other began, before