From 01aaa1dcb1976d34d8389ee1ae6503461859bbbd Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Thu, 1 Oct 2026 11:23:21 +0000 Subject: [PATCH] What is left of a substitution in x as a power of its candidate is written in the candidate A substitution u = g(x) can leave some x in the integrand. Under u = sqrt(1 - a x)/sqrt(1 + a x), F^(k u)/(1 - a^2 x^2) divided by du/dx is F^(k u) times -sqrt(1 + a x)/(a sqrt(1 - a x)). That is -1/(a u), but it holds no u as written, so the candidate was refused. The integral is -Ei(k ln(F) u)/a, Rubi's 2.3. What is left in x is now tried as c u^k, for k from -2 to 2. Each power is first screened by the ratio at three points, with every other symbol pinned. A power that passes has its constant worked out symbolically, and a constant that keeps an x refuses the candidate. A linear candidate is skipped: what is left beside it is always a power of u, and the change of scale there would ask the same question again ahead of the rules that answer it. e^(1/x) is answered this way, as x e^(1/x) - Ei(1/x) under u = 1/x. The test that pinned it as declined predates Ei being a node, and now checks that the answer differentiates back. Measured on the Rubi corpus against master cd3b27b9, both arms side by side: family 2 544 -> 557 of 650, family 3 175 of 180 against 174, family 0 1756 -> 1758 of 1814, and family 8 411 of 420 on both. There are 0 wrong anywhere. The 16 problems whose verdict moved, re-run alone, are 16 of 16 on the branch and 0 of 16 on master. The suite passes, 14,303 tests, and so does the allocation gate. Part of #718. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- .../Integration/IndefiniteIntegralSolver.cs | 191 ++++++++++++++++++ .../Calculus/ExponentialAnsatzTest.cs | 13 +- .../ExponentialIntegralIntegrationTest.cs | 17 ++ .../Calculus/SpecialFunctionsByPartsTest.cs | 13 ++ 4 files changed, 233 insertions(+), 1 deletion(-) diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index e04cdb285..f9f7317fc 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -18623,6 +18623,189 @@ static bool Close(Number.Complex left, Number.Complex right, double tolerance) && !Close(quotientFirst, quotientSecond, 1e-6); } + /// + /// with the part of it still in written + /// in , where that part is a constant times a power of the candidate + /// , from -2 to 2; or . + /// + /// + /// What the two are made of, compared without evaluating, and then the ratio at three points, + /// every other symbol pinned, screen each power cheaply, and only one that passes is worked + /// out: over one bar, with a negative fractional power written as a quotient by the + /// positive one, which it is on every branch, and simplified. That is what puts the + /// derivative of sqrt(1 - a x)/sqrt(1 + a x), a sum of two quotients of roots, over + /// one bar, and takes what is left beside u^(-1) to -1/(2 a). A constant that + /// keeps an x is no constant, and the candidate stays refused. + /// + private static Entity? WithTheCandidatesPowerLeftInX(Entity integrandInU, Entity u, Variable uSub, Entity.Variable x) + { + // Not for a linear candidate: what is left of x beside `u = f x` is always a power of u, + // and taking the change of scale here asks the same question again in u, ahead of the + // rules that answer it. `(c + d x)^2/(a + i a tan(e + f x))^3` is answered in 33 s, + // and with the change of scale taken here, not in 70. A quadratic is another matter: + // beside `u = a + b x + c x^2`, what `F^(1/u) (b + 2 c x)/u^2` leaves is `1/u^2` + // spelled otherwise, and read as one it is `-F^(1/u)/ln(F)` at once. + if (IsWrittenAsAPolynomial(u, x) && TreeAnalyzer.TryGetPolynomial(u, x, out var asAPolynomial) + && asAPolynomial.Keys.Max()!.CompareTo(EInteger.One) <= 0) + return null; + Entity inX = Number.Integer.One; + Entity rest = Number.Integer.One; + foreach (var (factor, underneath) in FactorsOfTheIntegrand(integrandInU)) + if (factor.ContainsNode(x)) + { + if (factor.ContainsNode(uSub)) + return null; + inX = underneath ? inX / factor : inX * factor; + } + else + rest = underneath ? rest / factor : rest * factor; + // A constant times a power of the candidate is made of what the candidate is made of: + // its functions of x, its radicands, the bases of its powers with x in the exponent. + // Checked first, since it costs no evaluation: a decline spends a thousand candidates + // here, and a polynomial left beside `erfc(a + b x)` is none of them. + if (!TheBuildingBlocks(inX, x).SetEquals(TheBuildingBlocks(u, x))) + return null; + foreach (var power in ThePowersTheRatioAgreesFor(inX, u, x)) + { + var ofTheCandidate = MathS.Pow(u, power); + var overRoots = (inX / ofTheCandidate).Replace(node => + node is Powf(var radicand, Number.Rational { IsNegative: true } negative) && negative is not Number.Integer + ? Number.Integer.One / MathS.Pow(radicand, -negative) + : node); + var constant = Functions.PartialFractions.Bare(Functions.SingleQuotient.Combine(overRoots).Simplify()); + // Where the simplification leaves x, over one bar the two halves may be polynomials + // in x, one a constant multiple of the other: `f (d^2 - e^2 x^2)` over + // `2 d e f (d^2 - e^2 x^2)` is `1/(2 d e)`. + if (constant.ContainsNode(x) + && Functions.SingleQuotient.Of(Functions.SingleQuotient.Combine(constant)) is var (above, below) + && TheConstantRatioOf(above, below, x) is { } ratio) + constant = ratio; + if (constant.ContainsNode(x) || constant.Nodes.Any(node => node == MathS.NaN) + || constant.Evaled is Number.Complex { IsZero: true }) + continue; + // One quotient, which is what the rules for `F^(k u)/u` read. + return Functions.SingleQuotient.Combine(rest * constant * MathS.Pow(uSub, power)).InnerSimplified; + } + return null; + } + + /// + /// /, two polynomials in , + /// as a constant, or where one is not a constant multiple of the + /// other. Read off the coefficients, expanded so that a term such as f (d e - d e) x + /// the simplification leaves standing is not counted: with leading coefficients p and + /// q, every pair a_k, b_k has a_k q - b_k p expand to zero, which + /// asks no division by a symbol, and the ratio is p/q in lowest terms. The + /// simplifier would not cancel e^5 f/e^3 against e^2 f, for want of knowing + /// e is not zero. + /// + private static Entity? TheConstantRatioOf(Entity above, Entity below, Entity.Variable x) + { + if (!TreeAnalyzer.TryGetPolynomial(Functions.PartialFractions.Bare(above.Expand()), x, out var top) + || !TreeAnalyzer.TryGetPolynomial(Functions.PartialFractions.Bare(below.Expand()), x, out var bottom)) + return null; + static bool IsZero(Entity coefficient) + => Functions.PartialFractions.Bare(coefficient.Expand()) is var expanded + && (expanded == Number.Integer.Zero || expanded.Evaled is Number.Complex { IsZero: true }); + var topTerms = top.Where(term => !IsZero(term.Value)).ToDictionary(term => term.Key, term => term.Value); + var bottomTerms = bottom.Where(term => !IsZero(term.Value)).ToDictionary(term => term.Key, term => term.Value); + if (bottomTerms.Count == 0 || topTerms.Count != bottomTerms.Count || topTerms.Keys.Any(degree => !bottomTerms.ContainsKey(degree)) + || topTerms.Values.Concat(bottomTerms.Values).Any(coefficient => coefficient.ContainsNode(x))) + return null; + var degree = bottomTerms.Keys.Max()!; + var (p, q) = (topTerms[degree], bottomTerms[degree]); + foreach (var term in bottomTerms) + if (!IsZero(topTerms[term.Key] * q - term.Value * p)) + return null; + return Functions.PartialFractions.InLowestTermsOverTheSymbols(p / q); + } + + /// + /// The powers k, of -1, 1, -2 and 2, for which + /// /^k is one nonzero number at three + /// points, every other symbol pinned, agreeing to nine digits relative to its size. Each side + /// is evaluated once a point. A point where either cannot be evaluated admits no power: the + /// screen is all that keeps from simplifying. And + /// no ratio near zero, where two tiny values agree to any absolute tolerance: + /// x erfc(a + b x)^2 is 1e-9 at one pinned point and 1e-28 at the next. + /// + private static IEnumerable ThePowersTheRatioAgreesFor(Entity expr, Entity candidate, Entity.Variable x) + { + var pinned = new Dictionary(); + var values = new[] { 1.37, 0.61, 2.23, 1.91, 0.83, 1.13 }; + var index = 0; + foreach (var symbol in expr.Vars.Concat(candidate.Vars).Distinct()) + if (symbol != x) + pinned[symbol] = values[index++ % values.Length]; + Number.Complex? At(Entity function, double point) + { + var value = function.Substitute(x, point); + foreach (var pair in pinned) + value = value.Substitute(pair.Key, pair.Value); + return value.Evaled is Number.Complex { IsNaN: false } number && number.IsFinite && !number.IsZero ? number : null; + } + var powers = new List { -1, 1, -2, 2 }; + var first = new Dictionary(); + foreach (var point in new[] { 0.37, 1.71, -0.83 }) + { + if (At(expr, point) is not { } above || At(candidate, point) is not { } below) + return System.Array.Empty(); + foreach (var power in powers.ToList()) + { + var ofTheCandidate = power switch { -1 => 1 / below, 1 => below, -2 => 1 / (below * below), _ => below * below }; + var ratio = (Number.Complex)(above / ofTheCandidate); + var size = ((Number.Real)ratio.Abs()).EDecimal.ToDouble(); + if (!(size > 1e-12 && size < 1e12) + || first.TryGetValue(power, out var earlier) && ((Number.Real)(ratio - earlier).Abs()).EDecimal.ToDouble() > 1e-9 * size) + powers.Remove(power); + else if (!first.ContainsKey(power)) + first[power] = ratio; + } + if (powers.Count == 0) + break; + } + return powers; + } + + /// + /// Whether is written as a polynomial in : x in + /// sums, products and whole powers that are not negative only, and nowhere below a bar. + /// Read off the tree, with nothing expanded, since the substitution search asks it of every + /// candidate. + /// + private static bool IsWrittenAsAPolynomial(Entity expr, Entity.Variable x) + => expr.ContainsNode(x) && expr.Nodes.All(node => !node.ContainsNode(x) + || node is Variable or Sumf or Minusf or Mulf + || node is Powf(_, Number.Integer { EInteger.Sign: >= 0 }) + || node is Divf(_, var below) && !below.ContainsNode(x)); + + /// + /// What is made of in beyond sums, products, + /// quotients and whole powers: each function of x, each root's radicand, and the base of + /// each power with x in its exponent. + /// + private static HashSet TheBuildingBlocks(Entity expr, Entity.Variable x) + { + var blocks = new HashSet(); + foreach (var node in expr.Nodes) + switch (node) + { + case Variable or Number or Sumf or Minusf or Mulf or Divf: + break; + case Powf(_, var exponent) when !exponent.ContainsNode(x) && exponent is Number.Integer: + break; + case Powf(var @base, _): + if (node.ContainsNode(x)) + blocks.Add(@base); + break; + default: + if (node.ContainsNode(x)) + blocks.Add(node); + break; + } + return blocks; + } + /// /// Whether / takes the same value at two /// points, every other symbol pinned to a fixed value -- a necessary condition for a @@ -20032,6 +20215,14 @@ private static bool HoldsAnEvenRootBesides(Entity expr, Entity radicand, Entity. integrandInU = SimplifiedWithoutTheImaginaryUnit(inTheCandidate, expr); } } + // The candidate itself, left over in x as a power of it: under + // `u = sqrt(1 - a x)/sqrt(1 + a x)` the quotient of `F^(k u)/(1 - a^2 x^2)` by + // du/dx is `F^(k u)` times `-sqrt(1 + a x)/(a sqrt(1 - a x))`, which is + // `-1/(a u)` and holds no u as written, and the candidate was refused where the + // integral is `-Ei(k ln(F) u)/a`, Rubi's 2.3. + // https://github.com/asc-community/AngouriMath/issues/718 + if (integrandInU.ContainsNode(x) && WithTheCandidatesPowerLeftInX(integrandInU, u, uSub, x) is { } inPowersOfTheCandidate) + integrandInU = inPowersOfTheCandidate; if (u is Sinf or Cosf && integrandInU.ContainsNode(x)) firstPass[u] = integrandInU; } diff --git a/Sources/Tests/UnitTests/Calculus/ExponentialAnsatzTest.cs b/Sources/Tests/UnitTests/Calculus/ExponentialAnsatzTest.cs index 541316932..585daf23d 100644 --- a/Sources/Tests/UnitTests/Calculus/ExponentialAnsatzTest.cs +++ b/Sources/Tests/UnitTests/Calculus/ExponentialAnsatzTest.cs @@ -184,10 +184,21 @@ public void AnExponentialTimesARootOfAQuadraticIsDeclinedWhereThereIsNone() /// [Theory] [InlineData("e^(x^3)")] - [InlineData("e^(1/x)")] public void ANonElementaryOneIsDeclined(string integrand) => Assert.Contains("integral(", integrand.ToEntity().Integrate("x").Stringize()); + /// + /// e^(1/x) has no elementary antiderivative either, and it has one in Ei: + /// x e^(1/x) - Ei(1/x), found under u = 1/x, where what is left in x is + /// -1/u^2. + /// + [Fact] + public void AnExponentialOfAReciprocalIsWrittenWithEi() + { + Assert.Contains("Ei(", "e^(1/x)".ToEntity().Integrate("x").Stringize()); + DifferentiatesBack("e^(1/x)"); + } + /// /// What by parts answered before keeps its form: a polynomial times an exponential is /// still integrated by parts, since the ansatz runs after it. diff --git a/Sources/Tests/UnitTests/Calculus/ExponentialIntegralIntegrationTest.cs b/Sources/Tests/UnitTests/Calculus/ExponentialIntegralIntegrationTest.cs index 7190702a9..1ab28f9d0 100644 --- a/Sources/Tests/UnitTests/Calculus/ExponentialIntegralIntegrationTest.cs +++ b/Sources/Tests/UnitTests/Calculus/ExponentialIntegralIntegrationTest.cs @@ -104,6 +104,23 @@ public void APowerTimesAFunctionOfALogarithmOfAMonomial(string integrand) public void AnExponentialOfALinearOverSeveralLinears(string integrand) => DifferentiatesBack(integrand, AroundZero, Pins); + /// + /// Under a substitution, what is left in x is a power of the candidate: under + /// u = sqrt(1 - a x)/sqrt(1 + a x), F^(k u)/(1 - a^2 x^2) is + /// -F^(k u)/(a u), and under u = c/(a + b x), F^u is + /// -c F^u/(b u^2); and beside a quadratic u, what is left of + /// F^(1/u) u'/u^2 is 1/u^2 written otherwise. Rubi's 2.3. Every point is where + /// |a x| < 1, off a + b x = 0, and the integrand is real. + /// + [Theory] + [InlineData("F^(sqrt(1 - a*x)/sqrt(1 + a*x))/(1 - a^2*x^2)")] + [InlineData("F^(2*sqrt(1 - a*x)/sqrt(1 + a*x))/(1 - a^2*x^2)")] + [InlineData("F^(3*sqrt(1 - a*x)/sqrt(1 + a*x))/(1 - a^2*x^2)")] + [InlineData("F^(c/(a + b*x))")] + [InlineData("F^(1/(a + b*x + c*x^2))*(b + 2*c*x)/(a + b*x + c*x^2)^2")] + public void WhatIsLeftInXIsAPowerOfTheCandidate(string integrand) + => DifferentiatesBack(integrand, AroundZero, Pins); + /// /// The Gaussian's odd negative moments end at int e^(A x^2)/x = Ei(A x^2)/2, and /// x e^(-1/x^2) is one of them under u = 1/x. diff --git a/Sources/Tests/UnitTests/Calculus/SpecialFunctionsByPartsTest.cs b/Sources/Tests/UnitTests/Calculus/SpecialFunctionsByPartsTest.cs index 0ba2167aa..17288252a 100644 --- a/Sources/Tests/UnitTests/Calculus/SpecialFunctionsByPartsTest.cs +++ b/Sources/Tests/UnitTests/Calculus/SpecialFunctionsByPartsTest.cs @@ -171,5 +171,18 @@ public void ASquareIsTwoRoundsOfParts(string integrand) [InlineData("erf(a + b*x)^2")] public void ASquareOfAShiftedArgumentIsTwoRoundsOfParts(string integrand) => DifferentiatesBack(integrand, Parameters); + + /// + /// Beside a linear, the square of a complementary error function of a + b x is not + /// answered yet, and what is asserted is the value of any answer given in its place. Under + /// u = erfc(a + b x) what is left in x is a polynomial, which is no power of u, however + /// small erfc is at the points that screen it. + /// + [Fact] + public void ASquareOfAShiftedComplementaryErrorFunctionBesideALinearHasNoWrongAnswer() + { + if (!"(c + d*x)*erfc(a + b*x)^2".ToEntity().Integrate("x").Stringize().Contains("integral(")) + DifferentiatesBack("(c + d*x)*erfc(a + b*x)^2", Parameters); + } } }