From 824a4dc5e78856418cf18ab5ae9ed8b8f4972a9c Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 22 Sep 2026 03:02:34 +0000 Subject: [PATCH] Powers of asech(a x) and acsch(a x) beside a power of x are integrated, by rounds of parts x^3 asech(a x)^2 was left as written. One step of parts leaves x^2 asech(a x)/sqrt(1/(a x)^2 - 1), and five things stood between that and the next step, each traced and fixed in its place: - The regrouping of parts around the differentiated factor is offered up to two levels below the question asked where everything beside the factor is algebraic with a root in it: the other factor integrates to an algebraic expression or declines at once, and the remainder is algebraic. With a root only, since beside a rational function the rule for a rational function times a logarithm answers, and parts there sent (a + b atanh(c x^2))^2/x^5 into a search that did not return. - SolveByWritingAPowerOfAQuotientApart runs at any depth where every root stands over a monomial with a polynomial above it -- sqrt(1/(a x)^2 - 1) as the derivative of asech writes it -- and answers with sgn(x)^m in front rather than by parity, where the answer in |x| was a remainder no rule read when handed on. The sign goes in front only where every power of |x| taken came from a root that is a factor of the integrand. - SolveByTakingASignOut: the sign of a real-valued factor is constant between its zeros and goes in front of the antiderivative of the rest; after every rule that reads the sign where it stands, so cos(x) sgn(sin(x)) is still |sin(x)|. - A radicand written in two orders is one spelling where it stands in two places that differ in the sign of the power or the side of the bar. - A whole power of a fractional power is normalised to the power it is: ((c^2 x^2 + 1)^(-1/2))^2 handed to Euler's substitution as a radical beside (c^2 x^2 + 1)^(-1) answered the remainder of acsch(c x)^2/x^4 wrongly, and the corpus caught it as its one wrong answer. Rubi's family 7: 210 to 215 of 270, 0 wrong, 0 error, timeouts 26 to 30 (the same rows, over the budget under the load of the new answers); the 1774-problem suite unchanged at 1707, 0 wrong, 0 timeout, wall 85 s. Suite 12552 passed; the kernel gate passed on all 19 gated benchmarks. Part of #718. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 19 +++ .../Integration/IndefiniteIntegralSolver.cs | 134 ++++++++++++++++-- .../Integration/Integration.Definition.cs | 25 +++- .../InverseHyperbolicSecantIntegralTest.cs | 8 ++ 4 files changed, 176 insertions(+), 10 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index c6f34b89f..6a8a13237 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -957,6 +957,25 @@ every rule ([#718](https://github.com/asc-community/AngouriMath/issues/718)). | `"e^(2*asech(a*x))/x^2".Integrate("x")` | left unevaluated | the antiderivative, in `sgn(x)` | | `"x*(a+b*asech(c*x))/(d+pe*x^2)^2".Integrate("x")` | left unevaluated | the antiderivative | +### Powers of `asech(a x)` and `acsch(a x)` beside a power of `x` are integrated, by rounds of parts + +`x^3 asech(a x)^2` was left as written. One step of parts leaves `x^2 asech(a x)/sqrt(1/(a x)^2 - 1)`, +and the step on that -- asked two levels below the question -- is now offered where everything +beside the differentiated factor is algebraic with a root in it; the root over a monomial is +written apart at any depth; a sign of a real-valued factor is constant between its zeros and +goes in front of the antiderivative of the rest; and a root written apart with `|x|` answers +with `sgn(x)` in front rather than in `|x|`, which a further step could not read. A square of +a root is normalised to the power it is, which is what stopped Euler's substitution answering +the remainder of `acsch(c x)^2/x^4` wrongly along the way +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"x^3*asech(a*x)^2".Integrate("x")` | left unevaluated | the antiderivative, `provided a^2 > 0` | +| `"asech(a*x)^3/x^2".Integrate("x")` | left unevaluated | the antiderivative | +| `"(a+b*acsch(c*x))^2/x^4".Integrate("x")` | left unevaluated | the antiderivative | +| `"x^2/sqrt(1/(a^2*x^2)-1)".Integrate("x")` | left unevaluated | `sgn(x) |a| (2 (1 - a^2 x^2)^(3/2)/3 - 2 sqrt(1 - a^2 x^2))/(2 a^4)` up to the form -- with `sgn(x)` in front, where an answer written in `|x|` used to be given for such shapes | + ### `binomial(n, k)` is a function **Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 5661751f5..904c4e61d 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -408,17 +408,22 @@ private static (Entity Numerator, Entity Denominator) CancelledWithFunctionsAsIn // Timofeev's 314 down a path whose answer could not be checked, where the two // roots apart are split over `(1 - x)(1 + x)` and answered. string Spelling(Entity radicand) => string.Join(" + ", Sumf.LinearChildren(radicand).Select(term => term.ToString()).OrderBy(term => term, System.StringComparer.Ordinal)); - var signsOfSpelling = new Dictionary(); - foreach (var node in numerator.Nodes.Concat(denominator.Nodes)) + // Where the radicand stands in two places that differ in the sign of the power as + // written or in the side of the bar -- `q^(-1/2)` above beside `sqrt(q)` below, + // which the joining below reads as one atom, or `sqrt(A)` above beside `sqrt(A)` + // below, which cancel -- and only there. + var placesOfSpelling = new Dictionary Places, Entity First)>(); + foreach (var (node, underneath) in numerator.Nodes.Select(n => (n, false)).Concat(denominator.Nodes.Select(n => (n, true)))) if (node is Powf(var radicand, Number.Rational r) && r is not Number.Integer && radicand.ContainsNode(x) && radicand is Sumf or Minusf) { var key = Spelling(radicand); - var (negative, positive, first) = signsOfSpelling.TryGetValue(key, out var so) ? so : (false, false, radicand); - signsOfSpelling[key] = (negative || r.ERational.IsNegative, positive || !r.ERational.IsNegative, first); + if (!placesOfSpelling.TryGetValue(key, out var seen)) + placesOfSpelling[key] = seen = (new HashSet<(bool, bool)>(), radicand); + seen.Places.Add((r.ERational.IsNegative, underneath)); } Entity OneSpelling(Entity side) => side.Replace(node => node is Powf(var radicand, Number.Rational r) && r is not Number.Integer && radicand.ContainsNode(x) && radicand is Sumf or Minusf - && signsOfSpelling.TryGetValue(Spelling(radicand), out var seen) && seen.Negative && seen.Positive && seen.First != radicand + && placesOfSpelling.TryGetValue(Spelling(radicand), out var seen) && seen.Places.Count > 1 && seen.First != radicand ? MathS.Pow(seen.First, r) : node); numerator = OneSpelling(numerator); @@ -1864,7 +1869,16 @@ bool IsAnExponential(Entity factor) // although the rule never fires on it at the top at all. // // The second is in the regrouping: exactly one factor to differentiate, never two. - if (Integration.AnsweringTheQuestionAsked + // ...and up to two below the question asked where everything beside the + // differentiated factor is algebraic in x with a root in it: the second step of + // parts on `x^3 asech(a x)^2` is `x^2 asech(a x)/sqrt(1/(a x)^2 - 1)`, whose other + // factor integrates to an algebraic expression or declines at once, and whose + // remainder is algebraic -- none of the logarithms by the dozen the scope above + // is there to stop. With a root only: beside a rational function the rule for a + // rational function times a logarithm is the one that answers, and offering + // parts there sent `(a + b atanh(c x^2))^2/x^5` into a search that did not return. + if ((Integration.AnsweringTheQuestionAsked + || Integration.AnsweringTheQuestionAskedOrTwoBelow && TryRegroupAroundTheDifferentiatedFactor(expr) is var (_, algebraicOthers) && algebraicOthers is not null && IsAlgebraicIn(algebraicOthers, x) && HasARadicalOf(algebraicOthers, x)) && TryRegroupAroundTheDifferentiatedFactor(expr) is var (differentiated, others) && differentiated is not null && others is not null && TrySplit(differentiated, others) is { } byLiate) @@ -9435,6 +9449,33 @@ node is Powf(var @base, Number.Rational half) && @base == radicand return null; } + /// + /// Whether every fractional power of a quotient in in + /// has a monomial in below its bar, and + /// there is at least one: the shape sqrt(1/(c x)^2 - 1) has, and + /// sqrt((1 + x)/(1 - x)) has not. + /// + private static bool EveryRootStandsOverAMonomial(Entity expr, Entity.Variable x) + { + var any = false; + foreach (var node in expr.Nodes) + { + if (node is not Powf(var @base, Number.Rational exponent) || exponent is Number.Integer || !@base.ContainsNode(x)) + continue; + var (above, below) = Functions.SingleQuotient.Of(AsOneQuotient(@base)); + if (below == Number.Integer.One || !below.ContainsNode(x)) + continue; + // ...and a polynomial above it, so that what comes out is the root of a + // polynomial: `(1 + ln(u^2)/u^2)^(-3/2)` has a monomial below and is not this, + // and written apart at every depth it fed Bronstein's `sqrt(x + ln(x))` search. + if (!TreeAnalyzer.TryGetPolynomial(below, x, out var monomials) || monomials.Count != 1 + || !TreeAnalyzer.TryGetPolynomial(above, x, out _)) + return false; + any = true; + } + return any; + } + /// /// , an antiderivative of for /// positive, extended to the other side by parity, which is exact: @@ -9950,6 +9991,56 @@ node is Powf(Powf(var @base, var inner), var outer) return answer?.Substitute(constant, linear - slope * x); } + /// + /// The sign of a real-valued function of among the factors of the + /// integrand is constant on every interval between its zeros, and goes in front of the + /// antiderivative of the rest: sgn(x) f(x) is sgn(x) F(x) on each half-line, + /// which is the generic case every rule answers in. The extension by parity writes the + /// antiderivative of x^2/sqrt(1/(a x)^2 - 1) with a sgn(x) in front, and a + /// step of parts against it -- x^2 asech(a x)/sqrt(1/(a x)^2 - 1), the second step + /// on x^3 asech(a x)^2 -- leaves that sign beside the remainder, where no rule + /// read past it. Only a sign whose argument is shown real: for a complex g, + /// sgn(g) is g/|g| and is not constant anywhere. Asked as the same question. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + internal static Entity? SolveByTakingASignOut(Entity expr, Entity.Variable x, bool integrateByParts) + { + if (!TryReadAsQuotient(expr, out var numerator, out var denominator) && expr is Mulf) + (numerator, denominator) = (expr, Number.Integer.One); + else if (numerator is null) + return null; + var signs = new Dictionary(); + Entity Without(Entity side) + { + Entity? rest = null; + foreach (var factor in Mulf.LinearChildren(side)) + { + var (@base, times) = factor is Powf(var inner, Number.Integer whole) ? (inner, whole.EInteger) : (factor, EInteger.One); + if (@base is Signumf(var signed) && signed.ContainsNode(x) && TreeAnalyzer.IsRealValued(signed, x) && times.CanFitInInt32()) + { + signs[signed] = (signs.TryGetValue(signed, out var seen) ? seen : 0) + times.ToInt32Unchecked(); + continue; + } + rest = rest is null ? factor : rest * factor; + } + return rest ?? Number.Integer.One; + } + var above = Without(numerator); + var below = Without(denominator); + if (signs.Count == 0) + return null; + var rest = below == Number.Integer.One ? above : above / below; + if (!rest.ContainsNode(x)) + return null; + var answer = Integration.ComputeAsTheSameQuestion(rest, x, integrateByParts); + if (answer is null) + return null; + foreach (var pair in signs) + if (pair.Value % 2 != 0) + answer = MathS.Signum(pair.Key) * answer; + return answer; + } + /// /// A whole power of a product in which the variable stands beside a constant, written /// as the product of the powers: (c x)^2 is c^2 x^2, exact for a whole @@ -13073,7 +13164,12 @@ private static bool OnEveryRealInterval(List bases, Entity.Variable x, S // and no deeper: unscoped, it fed the search on `arcsin(sqrt(1 + x) - sqrt(x))`, // thirty levels of the same root written apart and combined again, five seconds // where declining takes one. - if (!Integration.AnsweringTheQuestionAskedOrOneBelow) + // ...except where every root written apart stands over a monomial in x, which is + // the derivative of asech(c x) and acsch(c x) as they are written -- `1/(c x)^2 - 1` + // over `c^2 x^2` -- and is a rewriting that lands on the root of a quadratic beside + // `|c| |x|` and nothing else: the second step of parts on `x^3 asech(a x)^2` asks + // for `x^2/sqrt(1/(a x)^2 - 1)` three levels down, and declined it there. + if (!Integration.AnsweringTheQuestionAskedOrOneBelow && !EveryRootStandsOverAMonomial(expr, x)) return null; var tookAPowerOfX = false; // Whether every power of x taken is a whole one, and whether their sum is odd: @@ -13083,6 +13179,10 @@ private static bool OnEveryRealInterval(List bases, Entity.Variable x, S // and was declined for that after its antiderivative had been found. var wholePowersOfX = true; var oddPowersOfX = false; + // ...and only where every such power came from a root that is a factor of the + // integrand: a sign can go in front of the answer for `|x|` that multiplied the + // integrand, and not for one inside a sum or a logarithm's argument. + var everyPowerOfXFromAFactor = true; // A parameter's even power taken for positive -- `c^2 x^2` below the bar, which is // how `acsch(c x)` and `asech(c x)` arrive, is positive for a real `c` -- and the // answer says so: `provided c^2 > 0`, which holds exactly for a real non-zero `c`. @@ -13095,7 +13195,7 @@ private static bool OnEveryRealInterval(List bases, Entity.Variable x, S // front of the answer only from a power that is one of them, and not from one // inside a logarithm or a sum, where it is not a factor of anything. var factors = new HashSet( - expr is Divf(var dividend, var divisor) ? Mulf.LinearChildren(dividend).Concat(Mulf.LinearChildren(divisor)) + TryReadAsQuotient(expr, out var dividend, out var divisor) ? Mulf.LinearChildren(dividend).Concat(Mulf.LinearChildren(divisor)) : expr is Mulf ? Mulf.LinearChildren(expr) : new[] { expr }); // A root inside the argument of a logarithm is left as it is: the logarithm is an @@ -13112,7 +13212,11 @@ expr is Divf(var dividend, var divisor) ? Mulf.LinearChildren(dividend).Concat(M var (above, below) = Functions.SingleQuotient.Of(AsOneQuotient(@base)); if (below != Number.Integer.One && below.ContainsNode(x) && OfModestDegree(above) && (IsPositiveForReal(below, x) || IsPositiveForARealParameter(below, x, out assumed, assumed))) + { + if (!factors.Contains(node)) + everyPowerOfXFromAFactor = false; return MathS.Pow(above, exponent) * PowerOfAPositive(below, -exponent); + } // An even power of a polynomial below the bar is positive wherever the // polynomial is real and not zero, which the answer states: `(A/P^2)^(1/2)` is // `sqrt(A) |P|^(-1)`, and `|P|` is `sgn(P) P`, the sign taken out in front. @@ -13196,6 +13300,8 @@ expr is Divf(var dividend, var divisor) ? Mulf.LinearChildren(dividend).Concat(M && (g == x || power is Number.Integer { EInteger.IsEven: true } || IsPositiveForReal(g, x))) { var evenPower = MathS.Pow(g, Number.Integer.Create(even)); + if (g == x && !factors.Contains(node)) + everyPowerOfXFromAFactor = false; taken = taken * (g == x ? PowerOfAPositive(evenPower, underneath ? -exponent : exponent) : power is Number.Integer ? MathS.Pow(g, power) : MathS.Pow(evenPower, underneath ? -exponent : exponent)); if (!n.EInteger.IsEven) @@ -13225,6 +13331,8 @@ expr is Divf(var dividend, var divisor) ? Mulf.LinearChildren(dividend).Concat(M Entity term = degree == 0 ? pair.Value : degree == 1 ? pair.Value * x : pair.Value * MathS.Pow(x, degree); rest = rest == Number.Integer.Zero ? term : rest + term; } + if (!factors.Contains(node)) + everyPowerOfXFromAFactor = false; return PowerOfAPositive(MathS.Pow(x, 2 * k), exponent) * MathS.Pow(rest.InnerSimplified, exponent); } return node; @@ -13239,8 +13347,16 @@ expr is Divf(var dividend, var divisor) ? Mulf.LinearChildren(dividend).Concat(M var forPositive = Integration.ComputeIndefiniteIntegral(Functions.PartialFractions.Bare(written), x, integrateByParts: true); if (forPositive is null || forPositive.Nodes.Any(node => node == MathS.NaN)) return null; + // With the sign in front where every power of |x| taken is whole -- |x|^m is + // sgn(x)^m x^m -- and by parity otherwise. The extension by parity writes the + // answer in |x|, and an answer in |x| handed on by a step of parts -- the second + // step on `asech(a x)^2/x^2` -- is a remainder in |x| and `(1 - (|x| a)^2)` that no + // rule reads, where the same answer with `sgn(x)` in front is the answer in x + // beside a factor the sign-out route takes. var extended = tookAPowerOfX - ? ExtendedByParity(expr, forPositive, x) ?? (wholePowersOfX ? (oddPowersOfX ? MathS.Signum(x) * Functions.PartialFractions.Bare(forPositive) : Functions.PartialFractions.Bare(forPositive)) : null) + ? (wholePowersOfX && everyPowerOfXFromAFactor + ? (oddPowersOfX ? MathS.Signum(x) * Functions.PartialFractions.Bare(forPositive) : Functions.PartialFractions.Bare(forPositive)) + : ExtendedByParity(expr, forPositive, x)) : forPositive; foreach (var pair in signs) if (pair.Value % 2 == 1 && extended is { }) diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index 0d5fa47ce..3a72288b6 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -117,7 +117,19 @@ internal static partial class Integration /// /// private static Entity Normalized(Entity expr, Entity.Variable x) => - expr.Replace(Patterns.GatherPowersOfOneBase).Replace(node => IndefiniteIntegralSolver.GatherSignsAndModuli(node, x)); + expr.Replace(Patterns.GatherPowersOfOneBase).Replace(node => IndefiniteIntegralSolver.GatherSignsAndModuli(node, x)) + // A whole power of a fractional power is the power of the product of the + // exponents, exact for every base: `((c^2 x^2 + 1)^(-1/2))^2` is + // `(c^2 x^2 + 1)^(-1)`, and left as a square of a root it was handed to Euler's + // substitution as a radical beside `(c^2 x^2 + 1)^(-1)`, which answered it + // wrongly -- the second step of parts on `acsch(c x)^2/x^4` was that. The + // gathering above joins two powers of one base beside each other and not one + // inside the other. A fractional inner power only: `((1 + x^2)^(-1))^4` is + // spelled so by the rules that read it, and flattened it sent Timofeev's + // `x^3 arctan(x)^2/(1 + x^2)^3` from 0.1 s to 8. + .Replace(node => node is Entity.Powf(Entity.Powf(var @base, var inner), Entity.Number.Integer whole) && inner is Entity.Number.Rational && inner is not Entity.Number.Integer && @base.ContainsNode(x) + ? MathS.Pow(@base, (inner * whole).InnerSimplified) + : node); /// /// The integrals already answered under the settings in force, so that the same question @@ -256,6 +268,12 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => /// internal static bool AnsweringTheQuestionAskedOrOneBelow => descentDepth <= 2; + /// + /// The question asked or one of the two steps below it: the remainder of a step of + /// parts on a power of an inverse function, and the remainder of the step on that. + /// + internal static bool AnsweringTheQuestionAskedOrTwoBelow => descentDepth <= 3; + /// /// integrated as a question in its own right rather than as a /// step in the search for the current one: the term of a sum asked at the top is asked at @@ -801,6 +819,11 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => // throws away whatever structure it had: (1 + x^2)^2 is answered as a power and only // wants writing out if that fails. if ((answer = IndefiniteIntegralSolver.SolveByExpandingAPower(expr, x, integrateByParts)) is { }) return answer; + // The sign of a real-valued factor is constant between its zeros, and goes in + // front of the antiderivative of the rest. After every rule that reads the sign + // where it stands: `cos(x) sgn(sin(x))` is `|sin(x)|` by the substitution, and + // `sgn(sin(x)) sin(x)` with the sign taken out first. + if ((answer = IndefiniteIntegralSolver.SolveByTakingASignOut(expr, x, integrateByParts)) is { }) return answer; if (integrateByParts && (answer = IndefiniteIntegralSolver.SolveIntegratingByParts(expr, x)) is { }) return answer; return null; } diff --git a/Sources/Tests/UnitTests/Calculus/InverseHyperbolicSecantIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/InverseHyperbolicSecantIntegralTest.cs index 7e3df4d0c..43e57af9c 100644 --- a/Sources/Tests/UnitTests/Calculus/InverseHyperbolicSecantIntegralTest.cs +++ b/Sources/Tests/UnitTests/Calculus/InverseHyperbolicSecantIntegralTest.cs @@ -73,6 +73,14 @@ private static void DifferentiatesBack(string integrand) [InlineData("(a+b*asech(c*x))/x^7")] [InlineData("x*(a+b*asech(c*x))/(d+pe*x^2)^2")] [InlineData("e^(2*asech(a*x))/x^2")] + // Powers of the secant, by two and three rounds of parts: the second round asks for + // `x^2/sqrt(1/(a x)^2 - 1)` two levels down, and its answer with `sgn(x)` in front, + // where the extension by parity wrote it in |x| and the remainder was a search in |x|. + [InlineData("x^3*asech(a*x)^2")] + [InlineData("asech(a*x)^2/x^2")] + [InlineData("asech(a*x)^3/x^2")] + [InlineData("(a+b*asech(c*x))^2/x^4")] + [InlineData("x^3*(a+b*acsch(c*x))^2")] public void ARationalFunctionWithSymbolicCoefficientsBesideTheSecant(string integrand) { var integral = integrand.ToEntity().Integrate("x");