From 36135de54f4fc7ec5b4a08c3fd30463bf43965f8 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 22 Sep 2026 10:12:04 +0000 Subject: [PATCH] =?UTF-8?q?A=20fractional=20power=20of=20a=20=C2=B1=20a=20?= =?UTF-8?q?cosh(y)=20is=20integrated=20by=20the=20half=20angle=20at=20whic?= =?UTF-8?q?h=20it=20is=20a=20square?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `x^2 sqrt(a + a cosh(c + d x))` was left as written: no substitution rationalises a root of a hyperbolic function beside a power of x, and beside the sine and cosine of y the root is of a quotient under every one. 1 + cosh(y) is 2 cosh(y/2)^2, so the root is sqrt(2a) cosh(y/2) with no sign to write, the hyperbolic cosine being positive; 1 - cosh(y) is -2 sinh(y/2)^2, so (a - a cosh(y))^(3/2) is (-2a)^(3/2) sgn(sinh(y/2)) sinh(y/2)^3, the sign a constant between the zeros that comes out in front -- SolveByTheHalfAngleWhereOnePlusASineIsASquare's identity for the hyperbolic cosine. cosh(y) and sinh(y) beside the power are written in the half angle and the question is asked again in x. Exact for any a, the constants 2a and -2a kept whole under the power; at the top only, as every rule that writes a sign for a function. The reading of the six hyperbolic functions from their exponential spellings is lifted out of SolveByBiochesOddHyperbolicSubstitution into ReadTheHyperbolicFunctions and shared. Family 6 of the Rubi suite: 357 -> 363 of 417, 0 wrong; family 7 215/270 and the 1774-problem suite 1707 unchanged. Suite 12616 passed; allocation 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 | 18 +++ .../Integration/IndefiniteIntegralSolver.cs | 138 ++++++++++++++---- .../Integration/Integration.Definition.cs | 2 + .../UnitTests/Calculus/HalfAngleSquareTest.cs | 41 ++++++ 4 files changed, 172 insertions(+), 27 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 118f33948..1532701e2 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -1050,6 +1050,24 @@ in the function is admitted. Rubi's 6.1.7, 6.2.7, 6.3.7, 6.5.7 and 6.6.3 | `"csch(x)^5/(a+b*cosh(x)^2)".Integrate("x")` | left unevaluated | the antiderivative | | `"sinh(x)^3/(1+cosh(x)^2)".Integrate("x")` | left unevaluated | `cosh(x) - 2 arctan(cosh(x))`, in exponentials -- where `master` between the releases gave it through `u = e^x`, in `arctan(2 e^x/sqrt(12 - 8 sqrt(2)))` and its like | +### A fractional power of `a ± a cosh(y)` is integrated by the half angle at which it is a square + +`x^2 sqrt(a + a cosh(c + d x))` was left as written: no substitution rationalises a root of a +hyperbolic function beside a power of `x`. `1 + cosh(y)` is `2 cosh(y/2)^2`, so the root is +`sqrt(2a) cosh(y/2)` with no sign to write, the hyperbolic cosine being positive; and +`1 - cosh(y)` is `-2 sinh(y/2)^2`, so `(a - a cosh(y))^(3/2)` is +`(-2a)^(3/2) sgn(sinh(y/2)) sinh(y/2)^3`, the sign a constant between the zeros that comes out +in front -- the identity `sqrt(a + a sin(y))` already had. Exact for any `a`, the constants +`2a` and `-2a` kept whole under the power. Rubi's 6.2.1 and 6.2.5 +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"x^2*sqrt(a+a*cosh(c+d*x))".Integrate("x")` | left unevaluated | `sqrt(2a) (2 x^2 sinh((c + d x)/2)/d - 8 x cosh((c + d x)/2)/d^2 + 16 sinh((c + d x)/2)/d^3)` up to the form, in exponentials | +| `"x*(a+a*cosh(x))^(3/2)".Integrate("x")` | left unevaluated | the antiderivative | +| `"cosh(x)/sqrt(a-a*cosh(x))".Integrate("x")` | left unevaluated | the antiderivative, with `sgn(sinh(x/2))` in front | +| `"(A+B*cosh(x))/(a-a*cosh(x))^(5/2)".Integrate("x")` | left unevaluated | the antiderivative, with `sgn(sinh(x/2))` in front | + ### `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 e472dfec9..2ade68b60 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -13984,36 +13984,17 @@ private static int ParityIn(Entity polynomial, Entity.Variable f) } /// - /// Bioche's first two rules for the hyperbolic functions: a rational function of - /// sinh(y) and cosh(y) that is odd in the hyperbolic sine is a rational - /// function of u = cosh(y) times sinh(y) dy = du, with sinh^2 as - /// u^2 - 1; one odd in the hyperbolic cosine is one of u = sinh(y) times - /// cosh(y) dy = du, with cosh^2 as u^2 + 1. Radicals of polynomials - /// in the two even in the function are admitted as coefficients, as in - /// . + /// with its hyperbolic functions of one linear argument read + /// back from their exponential spellings as for the hyperbolic + /// sine and for the cosine -- tanh as s/c, + /// sech as 1/c and so on -- and the argument they are of; null where + /// the exponentials are not of one linear form or none spells one of the six. + /// Anything else in x is left as it is, for the caller to decline. /// - /// - /// The hyperbolic functions are not nodes: the library writes sinh(y) as - /// (e^y - e^-y)/2 and tanh(y) as (e^(2y) - 1)/(e^(2y) + 1), so the - /// six are read back by their spellings, for the argument each exponential names, and - /// nothing else in x may remain. Rubi's sinh(x)^3/(a + b cosh(x)^2) is - /// (u^2 - 1)/(a + b u^2) here, where under u = e^x it is a rational - /// function of a symbolic palindromic quartic and under u = tanh(x/2) of one of - /// degree eight, and both were searches past the budget; cosh(x)/(a + b tanh(x)^2), - /// sech(x)/(a + b sinh(x)^2)^(3/2) and csch(x)^5/(a + b cosh(x)^2) likewise. - /// In front of the substitution search, which with a symbolic coefficient spends the - /// budget on u = e^x. Exact: cosh is positive, so u = sinh(y) is a - /// bijection of the line, and u = cosh(y) one on each side of zero. - /// https://github.com/asc-community/AngouriMath/issues/718 - /// - internal static Entity? SolveByBiochesOddHyperbolicSubstitution(Entity expr, Entity.Variable x, bool integrateByParts) + private static (Entity OverTheTwo, Entity Argument)? ReadTheHyperbolicFunctions(Entity expr, Entity.Variable x, Variable s, Variable c) { - if (!Integration.AnsweringTheQuestionAskedOrOneBelow) - return null; if (!TryGatherExponentialsOfOneLinearForm(expr, x, unitX: true, out var exponentials, out var y, out _)) return null; - var s = Variable.CreateUnique(expr, "s_hyp"); - var c = Variable.CreateUnique(expr, "c_hyp"); // The six read by their spellings, each exponential by the multiple of y it is: // sinh(y) is (e^y - e^-y)/2, cosh(y) (e^y + e^-y)/2, tanh(y) (e^(2y) - 1)/(e^(2y) + 1), // coth(y) the reciprocal, sech(y) 1/cosh(y) and csch(y) 1/sinh(y). The argument is y, @@ -14061,7 +14042,110 @@ private static int ParityIn(Entity polynomial, Entity.Variable f) } if (argument is null) return null; - if (!overTheTwo.ContainsNode(s) && !overTheTwo.ContainsNode(c)) + return argument is null ? null : (overTheTwo, argument); + } + + /// + /// Fractional powers of a ± a cosh(y) beside anything in the hyperbolic functions + /// of y, by the half angle at which they are squares: 1 + cosh(y) is + /// 2 cosh(y/2)^2 and 1 - cosh(y) is -2 sinh(y/2)^2, so + /// sqrt(a + a cosh(y)) is sqrt(2a) cosh(y/2) -- no sign, the hyperbolic + /// cosine being positive -- and (a - a cosh(y))^(3/2) is + /// (-2a)^(3/2) sgn(sinh(y/2)) sinh(y/2)^3, the sign a constant between the zeros + /// of the sine that comes out in front; cosh(y) and sinh(y) beside them are + /// 2 cosh(y/2)^2 - 1 and 2 sinh(y/2) cosh(y/2), and the question is asked + /// again in x. 's + /// identity for the hyperbolic cosine. + /// + /// + /// Rubi's x^2 sqrt(a + a cosh(c + d x)), x (a + a cosh(x))^(3/2), + /// cosh(x)/sqrt(a - a cosh(x)) and (A + B cosh(x))/(a - a cosh(x))^(5/2) + /// were declined or searches past the budget: no substitution rationalises a root of a + /// hyperbolic function of x beside a power of x, and beside the sine and + /// cosine of y the root is of a quotient under every one. Exact, for any + /// a: cosh(y/2)^2 and sinh(y/2)^2 are not negative, so + /// (a q)^p = a^p q^p for the principal powers whatever a is, with + /// 2a and -2a kept as the one constant they are; whole products 2p + /// only. At the top only, as every rule that writes a sign for a function. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + internal static Entity? SolveByTheHalfAngleWhereOnePlusAHyperbolicCosineIsASquare(Entity expr, Entity.Variable x, bool integrateByParts) + { + if (!Integration.AnsweringTheQuestionAsked) + return null; + var s = Variable.CreateUnique(expr, "s_hyp"); + var c = Variable.CreateUnique(expr, "c_hyp"); + if (ReadTheHyperbolicFunctions(expr, x, s, c) is not var (overTheTwo, argument)) + return null; + var found = false; + foreach (var node in overTheTwo.Nodes) + { + if (node is not Powf(var radicand, Number.Rational exponent) || exponent is Number.Integer || !radicand.ContainsNode(s) && !radicand.ContainsNode(c)) + continue; + if (ReadAsOnePlusMinusAFunction(radicand, s, c) is not var (_, _, isSine) || isSine + || !exponent.ERational.Denominator.Equals(EInteger.FromInt32(2))) + return null; + found = true; + } + if (!found) + return null; + var half = (argument / 2).InnerSimplified; + var sinhHalf = MathS.Hyperbolic.Sinh(half); + var coshHalf = MathS.Hyperbolic.Cosh(half); + Entity signs = Number.Integer.One; + var rewritten = overTheTwo.Replace(node => + { + if (node is not Powf(var radicand, Number.Rational exponent) || exponent is Number.Integer || !radicand.ContainsNode(s) && !radicand.ContainsNode(c)) + return node; + if (ReadAsOnePlusMinusAFunction(radicand, s, c) is not var (a, positive, _)) + return node; + var twoP = Number.Integer.Create(exponent.ERational.Numerator); + // (a (1 + cosh y))^p = (2a)^p cosh(y/2)^(2p); (a (1 - cosh y))^p = (-2a)^p |sinh(y/2)|^(2p), + // and 2p is odd. + if (positive) + return MathS.Pow((2 * a).InnerSimplified, exponent) * MathS.Pow(coshHalf, twoP); + signs = signs * MathS.Signum(sinhHalf); + return MathS.Pow((-2 * a).InnerSimplified, exponent) * MathS.Pow(sinhHalf, twoP); + }); + var inX = rewritten.Substitute(c, 2 * MathS.Sqr(coshHalf) - 1).Substitute(s, 2 * sinhHalf * coshHalf); + if (inX.ContainsNode(s) || inX.ContainsNode(c)) + return null; + if (Integration.ComputeAsTheSameQuestion(inX, x, integrateByParts) is not { } result) + return null; + var answer = signs == Number.Integer.One ? result : signs * result; + return answer.Nodes.Any(node => node == MathS.NaN) ? null : answer; + } + + /// + /// Bioche's first two rules for the hyperbolic functions: a rational function of + /// sinh(y) and cosh(y) that is odd in the hyperbolic sine is a rational + /// function of u = cosh(y) times sinh(y) dy = du, with sinh^2 as + /// u^2 - 1; one odd in the hyperbolic cosine is one of u = sinh(y) times + /// cosh(y) dy = du, with cosh^2 as u^2 + 1. Radicals of polynomials + /// in the two even in the function are admitted as coefficients, as in + /// . + /// + /// + /// The hyperbolic functions are not nodes: the library writes sinh(y) as + /// (e^y - e^-y)/2 and tanh(y) as (e^(2y) - 1)/(e^(2y) + 1), so the + /// six are read back by their spellings, for the argument each exponential names, and + /// nothing else in x may remain. Rubi's sinh(x)^3/(a + b cosh(x)^2) is + /// (u^2 - 1)/(a + b u^2) here, where under u = e^x it is a rational + /// function of a symbolic palindromic quartic and under u = tanh(x/2) of one of + /// degree eight, and both were searches past the budget; cosh(x)/(a + b tanh(x)^2), + /// sech(x)/(a + b sinh(x)^2)^(3/2) and csch(x)^5/(a + b cosh(x)^2) likewise. + /// In front of the substitution search, which with a symbolic coefficient spends the + /// budget on u = e^x. Exact: cosh is positive, so u = sinh(y) is a + /// bijection of the line, and u = cosh(y) one on each side of zero. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + internal static Entity? SolveByBiochesOddHyperbolicSubstitution(Entity expr, Entity.Variable x, bool integrateByParts) + { + if (!Integration.AnsweringTheQuestionAskedOrOneBelow) + return null; + var s = Variable.CreateUnique(expr, "s_hyp"); + var c = Variable.CreateUnique(expr, "c_hyp"); + if (ReadTheHyperbolicFunctions(expr, x, s, c) is not var (overTheTwo, argument)) return null; var radicals = new List<(Variable Symbol, Entity Base, Number.Rational Exponent)>(); overTheTwo = overTheTwo.Replace(node => diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index c6fbcb26d..2504fd4b1 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -614,6 +614,8 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => // cosine, by the half angle at which they are squares: `1 + sin(y)` is `2 sin(u)^2`. Before // the substitution search, which spent twenty seconds on the radicals of the sine. if ((answer = IndefiniteIntegralSolver.SolveByTheHalfAngleWhereOnePlusASineIsASquare(expr, x, integrateByParts)) is { }) return answer; + // And `a ± a cosh(y)` under a fractional power: `2a cosh(y/2)^2`, `-2a sinh(y/2)^2`. + if ((answer = IndefiniteIntegralSolver.SolveByTheHalfAngleWhereOnePlusAHyperbolicCosineIsASquare(expr, x, integrateByParts)) is { }) return answer; if ((answer = IndefiniteIntegralSolver.SolveBySubstitution(expr, x, integrateByParts)) is { }) return answer; // A logarithmic derivative the substitution above could not read for a symbol in // an exponent: `(x^(n-1) - 1)/(x^n - n x)`. diff --git a/Sources/Tests/UnitTests/Calculus/HalfAngleSquareTest.cs b/Sources/Tests/UnitTests/Calculus/HalfAngleSquareTest.cs index 37563aff5..debcfc4e1 100644 --- a/Sources/Tests/UnitTests/Calculus/HalfAngleSquareTest.cs +++ b/Sources/Tests/UnitTests/Calculus/HalfAngleSquareTest.cs @@ -70,5 +70,46 @@ public void TheHalfAngleMakesThePowerWhole(string integrand) DifferentiatesBack(integrand, 2); DifferentiatesBack(integrand, -3); } + + /// + /// The hyperbolic cosine's: 1 + cosh(y) is 2 cosh(y/2)^2, with no sign to + /// write, and 1 - cosh(y) is -2 sinh(y/2)^2, with the sign of + /// sinh(y/2); the constants 2a and -2a stay whole under the power. + /// Points on both sides of zero, where sinh(y/2) changes sign. Rubi's 6.2.1 and 6.2.5. + /// + [Theory] + [InlineData("x^2*sqrt(a + a*cosh(c + d*x))")] + [InlineData("x^3*sqrt(a + a*cosh(x))")] + [InlineData("x*(a + a*cosh(x))^(3/2)")] + [InlineData("cosh(x)/sqrt(a - a*cosh(x))")] + [InlineData("1/(a - a*cosh(c + d*x))^(3/2)")] + [InlineData("(A + B*cosh(x))/(a - a*cosh(x))^(5/2)")] + public void TheHalfAngleMakesTheHyperbolicPowerWhole(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("integral(", integral.Stringize()); + Assert.DoesNotContain("NaN", integral.Stringize()); + foreach (var a in new[] { 2.0, -3.0 }) + { + var pinned = new Dictionary { ["a"] = a, ["c"] = 0.4, ["d"] = 1.3, ["A"] = 1, ["B"] = 2 }; + Entity Pin(Entity e) + { + foreach (var pair in pinned) + e = e.Substitute(pair.Key, pair.Value); + return e; + } + var derivative = Pin(integral).Differentiate("x"); + var original = Pin(integrand.ToEntity()); + foreach (var at in new[] { -2.1, -0.7, 0.3, 1.1, 2.6 }) + { + var got = derivative.Substitute("x", at).EvalNumerical(); + var want = original.Substitute("x", at).EvalNumerical(); + var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart); + var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)); + Assert.True(difference / scale < 1e-7, + $"d/dx of the antiderivative of {integrand} at a = {a} is {got} at x = {at}, where the integrand is {want}"); + } + } + } } }