diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index c88fb5893..ba2ef27cf 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -1166,6 +1166,24 @@ one form across the zeros. | `"sqrt(a + a*sin(x))".Integrate("x")` | unevaluated (`a^2 = a^2` is not decided by evaluation) | `-2 sqrt(2a) sgn(sin(u)) cos(u)` | | `"sqrt(1 + sin(x))".Integrate("x")` | `-2 cos(x)/sqrt(1 + sin(x))` | unchanged, the closed rule's | +### A half-odd power of the secant beside the cosine is integrated as the cosine's + +**Answers where there were none.** `sec(x)^(3/2)/sqrt(1 + cos(x))` was declined while +`1/(cos(x)^(3/2) sqrt(1 + cos(x)))`, which it is wherever the cosine is positive, was answered: the +substitutions that read a function of the cosine read the secant as `1/cos` to a whole power only. +A half-odd power of the secant beside the cosine is written as the cosine's on the other side of +the bar now, `sec(x)^p = K/cos(x)^p`, with `K = sec(x)^p cos(x)^p` in front of the answer: `K` is 1 +where the cosine is positive and -1 where it is negative, so the answer holds on both sides. The +same for the cosecant beside the sine. Rubi's 4.2.2.1, 4.2.3.1 and 4.2.4.2 +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"sec(x)^(3/2)/sqrt(1 + cos(x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `tan(x/2)`, times `K` | +| `"1/((a + a*cos(x))^(7/2)*sec(x)^(7/2))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `tan(x/2)`, over `K` | +| `"(A + c*cos(x)^2)*sec(x)^(11/2)*sqrt(a + a*cos(x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `tan(x/2)`, times `K` | +| `"sqrt(csc(x))*sqrt(a + a*sin(x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `tan(x/2)`, times `csc(x)^(1/2) sin(x)^(1/2)` | + ### Half-odd powers of `a ± a sec` and `a ± a csc` are integrated by the half-angle tangent Rubi's `(a + b sec)^m (d sec)^n` files with `a^2 = b^2` hold about a thousand problems with a diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index fa2111dda..15d52c2cb 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -23725,6 +23725,82 @@ bool IsAPolynomialInBoth(Entity polynomial) return null; } + /// + /// A half-odd power of the secant or the cosecant, in an integrand with the cosine or the + /// sine of the same argument in it, written as a power of the cosine or the sine: + /// sec(z)^p = K/cos(z)^p with K = sec(z)^p cos(z)^p, which is 1 wherever the + /// cosine is positive and -1 wherever it is negative, so it stands in front of the answer. + /// + /// + /// + /// sec(x)^(3/2)/sqrt(1 + cos(x)) was declined while + /// 1/(cos(x)^(3/2) sqrt(1 + cos(x))), which it is wherever the cosine is positive, + /// was answered: the substitutions that read a function of the cosine read the secant as + /// 1/cos to a whole power only. Only beside the cosine, so that a power of the + /// secant with no cosine keeps the rules for the secant. Rubi's 4.2.2.1, 4.2.3.1 and + /// 4.2.4.2. + /// + /// + /// Only a half-odd power of the secant itself, because that is what keeps K real: + /// the integrand is then real exactly where the rewritten one is, and an answer for the + /// rewritten one, which holds where it is real, holds where the integrand is. A constant + /// inside the power, sqrt(b sec(x)), or a quarter makes K imaginary on one + /// side, and the integrand real where the rewritten one is not: for a negative b, + /// sqrt(b sec(x)) is real where the cosine is negative and 1/sqrt(cos(x)) + /// is not, and nothing about an answer for the second says anything there. + /// + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + internal static Entity? SolveByWritingAPowerOfTheSecantInTheCosine(Entity expr, Entity.Variable x, bool integrateByParts) + { + if (!expr.Nodes.Any(node => node is Secantf or Cosecantf)) + return null; + var factors = FactorsOfTheIntegrand(expr); + var index = -1; + Entity? reciprocal = null; + Number.Rational? exponent = null; + for (var k = 0; k < factors.Count; k++) + { + if (factors[k].Factor is not Powf((Secantf or Cosecantf) and var found, var power) + || power.Evaled is not Number.Rational fraction || fraction is Number.Integer + || !fraction.ERational.Denominator.Equals(EInteger.FromInt32(2)) || !found.ContainsNode(x)) + continue; + if (index >= 0) + return null; + (index, reciprocal, exponent) = (k, found, fraction); + } + if (index < 0 || reciprocal is null || exponent is null) + return null; + var function = reciprocal is Secantf(var argument) ? MathS.Cos(argument) : MathS.Sin(((Cosecantf)reciprocal).Argument); + // The reciprocal nowhere else and the function somewhere: the power is then all of + // the secant there is, and the rest of the integrand a function of the cosine. + if (expr.Nodes.Count(node => node == reciprocal) != 1 || !expr.ContainsNode(function)) + return null; + var (written, underneath) = factors[index]; + // The power is K/cos^p above the bar and cos^p/K below it. The cosine's power goes to + // the side where it is positive, so that the rest is one quotient as the rules read it. + var negative = exponent.ERational.IsNegative; + var raised = MathS.Pow(function, negative ? -exponent : exponent); + var raisedAbove = underneath != negative; + Entity? above = null; + Entity? below = null; + for (var k = 0; k < factors.Count; k++) + { + var (factor, side) = k == index ? (raised, !raisedAbove) : factors[k]; + if (factor == Number.Integer.One) + continue; + if (side) + below = below is null ? factor : below * factor; + else + above = above is null ? factor : above * factor; + } + var numerator = above ?? Number.Integer.One; + var rewritten = below is null ? numerator : numerator / below; + var constant = written * MathS.Pow(function, exponent); + return Integration.ComputeAsTheSameQuestion(rewritten, x, integrateByParts) + ?.Pipe(answer => underneath ? answer / constant : answer * constant); + } + /// /// A rational function of sin(x) and cos(x), turned into a rational function /// of one variable by the half-angle substitution t = tan(x/2) and handed to the diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index 55108cc56..97ab8287d 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -967,6 +967,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => if (expr is Entity.Sumf or Entity.Minusf && (answer = IndefiniteIntegralSolver.SolveByGatheringACommonPolynomialFactorOfASum(expr, x, integrateByParts)) is { }) return answer; + // A power of the secant that is not whole beside the cosine, written as the cosine's + // with the constant that takes in front, which the substitutions below read. + if ((answer = IndefiniteIntegralSolver.SolveByWritingAPowerOfTheSecantInTheCosine(expr, x, integrateByParts)) is { }) return answer; // The half-angle substitution goes *after* linearity, and that is not a preference. // It fires on anything built from sines and cosines, and it answers `cos(x) + 1` with // a correct expression in tan(x/2) some forty characters long where splitting the sum diff --git a/Sources/Tests/UnitTests/Calculus/PowerOfTheSecantBesideTheCosineIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/PowerOfTheSecantBesideTheCosineIntegralTest.cs new file mode 100644 index 000000000..de42b27ca --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/PowerOfTheSecantBesideTheCosineIntegralTest.cs @@ -0,0 +1,56 @@ +// +// 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 +{ + /// + /// A half-odd power of the secant beside the cosine, or of the cosecant beside the sine, + /// which is the cosine's power on the other side of the bar times sec(x)^p cos(x)^p, + /// 1 where the cosine is positive and -1 where it is negative. Compared on both sides, where + /// the integrand is real and where it is not, which is where the constant is what keeps the + /// answer right. Rubi's 4.2.2.1, 4.2.3.1 and 4.2.4.2. + /// #718 + /// + [Trait("Area", "Calculus")] + public sealed class PowerOfTheSecantBesideTheCosineIntegralTest + { + [Theory] + [InlineData("sec(x)^(3/2)/sqrt(1 + cos(x))")] + [InlineData("sec(x)^(5/2)*(a + a*cos(x))^(3/2)")] + [InlineData("1/((a + a*cos(x))^(7/2)*sec(x)^(7/2))")] + [InlineData("(a + a*cos(x))^(3/2)*(A + B*cos(x))/sqrt(sec(x))")] + [InlineData("(A + c*cos(x)^2)*sec(x)^(11/2)*sqrt(a + a*cos(x))")] + [InlineData("sqrt(csc(x))*sqrt(a + a*sin(x))")] + [InlineData("1/((a + a*sin(x))^(3/2)*csc(x)^(3/2))")] + public void OnBothSidesOfTheCosine(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("integral(", integral.Stringize()); + Assert.DoesNotContain("NaN", integral.Stringize()); + Entity Pinned(Entity e) => e.Substitute("a", 1.3).Substitute("A", 0.9).Substitute("B", 1.4).Substitute("c", 0.6); + var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x"); + var original = Pinned(integrand.ToEntity()); + var compared = 0; + foreach (var at in new[] { -2.6, -1.1, -0.4, 0.3, 0.8, 1.2, 2.0, 2.7 }) + { + var want = original.Substitute("x", at).EvalNumerical(); + if (want.IsNaN) + continue; + compared++; + var got = derivative.Substitute("x", at).EvalNumerical(); + Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart) + < 1e-9 * 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 >= 6, $"only {compared} points could be compared for {integrand}"); + } + } +}