From 99e0ba746345c4bdcf8bd43f93dd29fc29402e45 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Wed, 21 Oct 2026 13:49:32 +0000 Subject: [PATCH] Stop a term-by-term integration at the first declined term, and simplify a power of a symbolic cubic under the exponential substitution at level -2 Two causes of the heavy rows of Rubi's 6.1.7. SolveBySplittingSum's helper Integrated was a Select(...).Aggregate(...), which integrated every term even after one had declined; IntegratedTermByTerm and the partial fractions' && chains already stop. It is now a loop that returns at the first null, summing in the same order. 6.1.7:196 5.5 -> 1.3 GB from this alone, and five rows master does not answer within two to three minutes are answered: 6.7.1:703 and 735 in 0.2 s, 6.2.7:121 and 122, and 2.3:714 (-e^Q/Q + Ei(Q)) in 18 s. Under u = e^(c + d x), csch^m (a + b sinh^3)^p becomes a quotient holding (a (2u)^3 + b (u^2 - 1)^3)^p, and SolveByExponentialSubstitution's combined.Simplify() spent 2.2-7.7 GB on the two candidates offered after the search: Expand().Simplify(-2) and RuleBasedFactorization(2).Simplify(-2). For a whole power >= 2 of a sum that is a polynomial in u of degree >= 3 with a symbolic coefficient and a nonzero constant term, it now simplifies at level -2, the same search without them; not where the monomials in u above and below the bar have equal degree (6.1.7:187, where the factored form wins), and not without the constant term (6.7.1:854, 3,485 characters, 178,682 that way). The 37 heavy rows of 6.1.7 and 6.4.2: answers identical, 81.7 -> 21.7 GB. Chain against master 08a3c491: pocket of 3,599 rows (6.1.x and 6.4.x whole, the rest of family 6 every 3rd, every 30th elsewhere), 3,555 -> 3,561 solved, allocation over the rows solved on both 567.5 -> 469.8 GB; samples unchanged; alone, 1.1.2.4:1198 answered as well; no row's allocation grew past 1.3x (Welz 201 and 1.1.1.3:2861, flagged in the samples, are identical in allocation and answer cold). Part of #233. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 15 +++ .../Integration/IndefiniteIntegralSolver.cs | 82 +++++++++++++- ...erOfABinomialInTheCubedSineIntegralTest.cs | 103 ++++++++++++++++++ 3 files changed, 194 insertions(+), 6 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/AHyperbolicCosecantBesideAPowerOfABinomialInTheCubedSineIntegralTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 883fc378b..7f6cd7544 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -1294,6 +1294,21 @@ arctangent, or an inverse hyperbolic tangent, of the tangent of the argument or | `"1/(a+b*cosh(x)^5)".ToEntity().Integrate("x")` | `integral(...)` | 3,607 characters | | `"1/(a-b*cosh(x)^8)".ToEntity().Integrate("x")` | `integral(...)` | 1,441 characters | +### A sum split into its terms is given up at the first term declined + +**Answers where there were none.** `1/(a sech(x) + b tanh(x))^2`, `1/(a coth(x) + b csch(x))^2` and +`1/(1 - cosh(x)^8)`, Rubi's 6.7.1 and 6.2.7, were declined, and before this change ran past two minutes. Integrating +a sum term by term asked every term after one had been declined, and declining those came before the rules that +answer these integrands; the split now ends at the first term declined. More slowly, in ten to twenty seconds, +`1/(1 - cosh(x)^6)` and `e^(a + b x + c x^2) (b + 2 c x)/(a + b x + c x^2)^2` are answered too +([#233](https://github.com/asc-community/AngouriMath/issues/233)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"1/(a*sech(x)+b*tanh(x))^2".ToEntity().Integrate("x")` | `integral(...)` | 1,836 characters | +| `"1/(a*coth(x)+b*csch(x))^2".ToEntity().Integrate("x")` | `integral(...)` | 1,831 characters | +| `"1/(1-cosh(x)^8)".ToEntity().Integrate("x")` | `integral(...)` | 1,232 characters | + ### A whole power of an inverse function of `c + d x^2` is integrated by its recurrence **Answers where there were none.** `(a + b acos(1 + d x^2))^4` was declined, and so were twenty-five more of Rubi's diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 6efc0fec9..39e9c7578 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -138,11 +138,21 @@ internal static class IndefiniteIntegralSolver return ((top * group.Radicals).InnerSimplified / bottom).InnerSimplified; }).ToList()); + // Term by term, and over at the first term declined: the sum is answered only if every + // term is, and the terms after one declined were each asked to no purpose. Rubi's 6.1.7 + // `csch(c + d x)^6 (a + b sinh(c + d x)^3)^3` is expanded into terms one of which is + // declined, and the terms after it cost 4.2 GB of the 5.5 the integral took. Entity? Integrated(List terms) - => terms.Select(e => Integration.ComputeAsAQuestionOfItsOwn(e, x, integrateByParts)).Aggregate((e1, e2) => (e1, e2) switch { - (null, _) or (_, null) => null, - (var int1, var int2) => int1 + int2 - }); + { + Entity? sum = null; + foreach (var term in terms) + { + if (Integration.ComputeAsAQuestionOfItsOwn(term, x, integrateByParts) is not { } integrated) + return null; + sum = sum is null ? integrated : sum + integrated; + } + return sum; + } } /// @@ -28609,11 +28619,13 @@ static bool IsAMonomialIn(Entity expr, Entity.Variable x) // `sech(x)^2/(a + b tanh(x))^2` after three seconds: simplified once more it is a // quotient, which the symbolic quadratic rule answers with the case `a + b = 0` apart, // and a piecewise antiderivative leaves parts a piecewise to integrate. With numbers - // alone, or no sum written twice, it is simplified as it was. + // alone, or no sum written twice, it is simplified as it was -- but for a power of a + // symbolic polynomial of the third degree or more, without the expanded and factored + // forms the simplifier offers after its search: see HasAPowerOfASymbolicCubicOrMore. var combined = Functions.SingleQuotient.Combine(rewritten / (Number.Rational.Create(k) * u)); var integrand = combined.Vars.Any(symbol => symbol != u) && WithTheFactorsWrittenOnBothSidesCancelled(u, combined, keepTheWrittenBases: true, out var anyCancelled) is var cancelled && anyCancelled - ? cancelled : combined.Simplify(); + ? cancelled : combined.Simplify(HasAPowerOfASymbolicCubicOrMore(combined, u) ? -2 : 2); if (integrand is Providedf(var inner, _)) integrand = inner; integrand = WithLogarithmArgumentsOverOneBar(integrand, u); @@ -28661,6 +28673,64 @@ static bool IsAMonomialIn(Entity expr, Entity.Variable x) } } + /// + /// Whether holds a whole power past the first of a sum that is a + /// polynomial in of the third degree or more with a symbol in one of + /// its coefficients: (a (2u)^3 + b (u^2 - 1)^3)^2, which is what the exponential + /// substitution makes of (a + b sinh(x)^3)^2. + /// + /// + /// + /// Such a quotient is simplified without the two forms the simplifier offers after its + /// search, the quotient multiplied out and the quotient factored, each simplified again + /// before it is ranked; the search itself runs as before. With the symbols in it both are + /// polynomials of a high degree with a page of coefficients: Rubi's 6.1.7 + /// csch(c + d x)^m (a + b sinh(c + d x)^3)^p spent 2.2 to 7.7 GB of its 5.2 to 10.5 + /// simplifying them -- the common denominator of the expanded terms and the long division + /// of the factored quotient, each coefficient simplified on its own -- to rank both behind + /// what the search had found, where the search alone takes 12 to 130 MB. + /// + /// + /// Those fourteen rows, m from 1 to 7 and p 2 or 3, are the corpus's 6.1.7 + /// quotients with such a power. On thirteen the search alone gives what the whole + /// simplification gives. On csch^7 (a + b sinh^3)^2 the factored form wins: the powers + /// of u written apart above the bar and below it are of one degree, seven, and in the + /// factored form they cancel to a number. So where they are of one degree the quotient is + /// simplified in full; and so it is where the sum has no constant term, so that u + /// divides it, as in 6.7.1's (A + B cosh(x) + C sinh(x))/(b cosh(x) + c sinh(x))^2: + /// the factored form wins there too, and from the search's form the antiderivative was + /// 178,682 characters where it is 3,485. A power of a polynomial with numbers for its + /// coefficients is simplified in full as well, as is one of the second degree: there the + /// forms after the search win often and cost little. + /// https://github.com/asc-community/AngouriMath/issues/233 + /// + /// + private static bool HasAPowerOfASymbolicCubicOrMore(Entity quotient, Entity.Variable u) + { + // With a constant term: a sum that u divides has a factor the factored form takes out. + if (!quotient.Nodes.Any(node => node is Powf(var @base, Number.Integer { EInteger: var power }) + && @base is Sumf or Minusf && power.CompareTo(EInteger.One) > 0 + && TreeAnalyzer.TryGetPolynomial(@base, u, out var terms) + && terms.Keys.All(degree => degree.Sign >= 0) && terms.Keys.Any(degree => degree.CompareTo(EInteger.FromInt32(3)) >= 0) + && terms.Values.All(coefficient => !coefficient.ContainsNode(u)) && terms.Values.Any(coefficient => coefficient.Vars.Any()) + && terms.TryGetValue(EInteger.Zero, out var constant) && !Functions.PartialFractions.IsZeroAsAValue(constant))) + return false; + // Where the powers of u written apart above the bar and below it are of one degree the + // factored form cancels them to a number and wins: kept to the whole simplification. + var (top, bottom) = Functions.SingleQuotient.Of(quotient); + return !DegreeOfTheMonomials(top).Equals(DegreeOfTheMonomials(bottom)); + + EInteger DegreeOfTheMonomials(Entity side) + { + var degree = EInteger.Zero; + foreach (var factor in Mulf.LinearChildren(side)) + if (factor.ContainsNode(u) && TreeAnalyzer.TryGetPolynomial(factor, u, out var monomial) && monomial.Count == 1 + && monomial.Keys.First() is { Sign: > 0 } power && !monomial.Values.First().ContainsNode(u)) + degree += power; + return degree; + } + } + /// /// with every logarithm whose argument is a rational function /// of with a quotient nested inside it written over one bar in diff --git a/Sources/Tests/UnitTests/Calculus/AHyperbolicCosecantBesideAPowerOfABinomialInTheCubedSineIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/AHyperbolicCosecantBesideAPowerOfABinomialInTheCubedSineIntegralTest.cs new file mode 100644 index 000000000..a6e08cf1b --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/AHyperbolicCosecantBesideAPowerOfABinomialInTheCubedSineIntegralTest.cs @@ -0,0 +1,103 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// Rubi's 6.1.7 csch(c + d x)^m (a + b sinh(c + d x)^3)^p. Under u = e^(c + d x) it is a + /// quotient with a power of a symbolic polynomial of the sixth degree in it, which is simplified + /// without the expanded and factored forms the simplifier offers after its search; and the split + /// of the expanded integrand into terms, which declines, stops at the first term declined. Each + /// row cost 5.2 to 10.5 GB, and the answers are the ones they were. + /// #233 + /// + /// + /// Each row has symbols of its own, so that no answer the thread remembers from another test + /// makes its bound hold without the work it measures. The bounds fail with either change + /// undone: with the quotient simplified in full every row, at 4.9 to 8.8 GB, and with every + /// term of the split asked the second and the fourth, at 5.2 and 2.8 GB. + /// + [Trait("Area", "Calculus")] + public sealed class AHyperbolicCosecantBesideAPowerOfABinomialInTheCubedSineIntegralTest + { + [Theory] + [InlineData("csch(v + w*x)^5*(p + q*sinh(v + w*x)^3)^2", "v,w,p,q", 2_000_000_000L)] + [InlineData("csch(r + s*x)^6*(k + m*sinh(r + s*x)^3)^3", "r,s,k,m", 3_000_000_000L)] + [InlineData("csch(g + h*x)*(n + t*sinh(g + h*x)^3)^3", "g,h,n,t", 2_000_000_000L)] + [InlineData("csch(y + z*x)^7*(f + u*sinh(y + z*x)^3)^3", "y,z,f,u", 2_000_000_000L)] + public void AnsweredWithinItsAllocation(string integrand, string symbols, long mostBytes) + { + var before = GC.GetAllocatedBytesForCurrentThread(); + var integral = integrand.ToEntity().Integrate("x"); + var allocated = GC.GetAllocatedBytesForCurrentThread() - before; + Assert.True(allocated < mostBytes, $"{integrand} allocated {allocated / 1_000_000} MB, past {mostBytes / 1_000_000} MB"); + DifferentiatesBack(integrand, integral, symbols); + } + + /// + /// Where the powers of u above the bar and below it are of one degree, seven here, the + /// factored form the simplifier offers after its search wins and is kept: from the search's + /// form alone the antiderivative was 1,938 characters where it is 571. + /// + [Fact] + public void WherePowersOfTheExponentialCancelTheFactoredFormIsKept() + { + const string integrand = "csch(c + d*x)^7*(a + b*sinh(c + d*x)^3)^2"; + var integral = integrand.ToEntity().Integrate("x"); + Assert.True(integral.ToString().Length < 1000, $"the antiderivative of {integrand} is {integral.ToString().Length} characters"); + DifferentiatesBack(integrand, integral, "c,d,a,b"); + } + + /// + /// And where the sum to a power has no constant term in u, so that u divides it: + /// under u = e^x Rubi's 6.7.1 (A + B cosh(x) + C sinh(x))/(b cosh(x) + c sinh(x))^2 + /// is a quotient with such a sum squared, the factored form wins there, and from the search's + /// form alone the antiderivative was 178,682 characters where it is 3,485. + /// + [Fact] + public void WhereTheSumHasNoConstantTermTheFactoredFormIsKept() + { + const string integrand = "(A + B*cosh(x) + C*sinh(x))/(b*cosh(x) + c*sinh(x))^2"; + var integral = integrand.ToEntity().Integrate("x"); + Assert.True(integral.ToString().Length < 10000, $"the antiderivative of {integrand} is {integral.ToString().Length} characters"); + DifferentiatesBack(integrand, integral, "A,B,C,b,c"); + } + + private static void DifferentiatesBack(string integrand, Entity integral, string symbols) + { + Assert.DoesNotContain("integral(", integral.Stringize()); + var names = symbols.Split(','); + var values = new[] { 0.3, 1.1, 1.3, 0.7, 0.4 }; + Entity Pinned(Entity e) + { + for (var k = 0; k < names.Length; k++) + e = e.Substitute(names[k], values[k]); + return e; + } + // A constant of integration differentiates away; C is a coefficient of one integrand here. + var derivative = Pinned(integral).Differentiate("x"); + var original = Pinned(integrand.ToEntity()); + var compared = 0; + foreach (var at in new[] { 0.2, 0.45, 0.7, 0.95, 1.2, 1.5 }) + { + var want = original.Substitute("x", at).EvalNumerical(); + var got = derivative.Substitute("x", at).EvalNumerical(); + if (want.IsNaN) + continue; + compared++; + Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart) + < 1e-8 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)), + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); + } + Assert.True(compared >= 5, $"only {compared} points could be compared for {integrand}"); + } + } +}