@@ -4505,6 +4505,90 @@ Entity Integral(ERational power)
45054505 return expr * inTermsOfW.Substitute(w, wOfX) / inX;
45064506 }
45074507
4508+ /// <summary>
4509+ /// <c>sec(y) (a ± a sec(y))^m (c ∓ c sec(y))^n</c>, with <c>n</c> half-odd and <c>m</c> anything,
4510+ /// integrated in <c>u = a ± a sec(y)</c>: the two sums multiply to <c>-a c tan(y)^2</c>, so the
4511+ /// root of the second is <c>tan(y)</c> over the root of <c>u</c> times a constant,
4512+ /// <c>sec(y) tan(y) dy</c> is <c>du</c> over one, and what is left is
4513+ /// <c>u^(m - 1/2) (2 - u/a)^(n - 1/2)</c>. The cosecant's are the same with the cotangent.
4514+ /// </summary>
4515+ /// <remarks>
4516+ /// Rubi's <c>sec(e + f x) (a + a sec(e + f x))^m sqrt(c - c sec(e + f x))</c> and two more of
4517+ /// 4.5.2.3 were declined. The constants are not written: the answer is the integrand times
4518+ /// the antiderivative in <c>u</c> over what that differentiates back to, a quotient whose
4519+ /// square is one; and it is kept only where that square is one at the sampled points.
4520+ /// https://github.com/asc-community/AngouriMath/issues/718
4521+ /// </remarks>
4522+ internal static Entity? SolveASecantBesidePowersOfItsConjugateSums(Entity expr, Entity.Variable x, bool integrateByParts)
4523+ {
4524+ if (!Integration.AnsweringTheQuestionAskedOrOneBelow)
4525+ return null;
4526+ Entity? argument = null;
4527+ foreach (var node in expr.Nodes)
4528+ {
4529+ if (TrigonometricArgument(node) is not { } thisArgument || !thisArgument.ContainsNode(x))
4530+ continue;
4531+ if (argument is null)
4532+ argument = thisArgument;
4533+ else if (argument != thisArgument)
4534+ return null;
4535+ }
4536+ if (argument is null || !TreeAnalyzer.TryGetPolyLinear(argument, x, out var rate, out _)
4537+ || rate.ContainsNode(x) || TreeAnalyzer.IsZero(rate))
4538+ return null;
4539+ var secant = MathS.Sec(argument);
4540+ var cosecant = new Cosecantf(argument);
4541+ Entity? function = null;
4542+ var sums = new List<(Entity Radicand, Entity Exponent, Entity A, bool Plus)>();
4543+ foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr))
4544+ {
4545+ if (!factor.ContainsNode(x))
4546+ continue;
4547+ if ((factor == secant || factor == cosecant) && function is null && !underneath)
4548+ {
4549+ function = factor;
4550+ continue;
4551+ }
4552+ var (radicand, exponent) = factor is Powf(var @base, var power) ? (@base, power) : (factor, (Entity)Number.Integer.One);
4553+ if (exponent.ContainsNode(x) || ReadAsOnePlusMinusAFunction(radicand, secant, cosecant) is not var (sumConstant, plus, _))
4554+ return null;
4555+ sums.Add((radicand, underneath ? (-exponent).InnerSimplified : exponent, sumConstant, plus));
4556+ }
4557+ if (function is null || sums.Count != 2 || sums[0].Plus == sums[1].Plus
4558+ || ReadAsOnePlusMinusAFunction(sums[0].Radicand, secant, cosecant) is not var (_, _, firstIsSecant)
4559+ || ReadAsOnePlusMinusAFunction(sums[1].Radicand, secant, cosecant) is not var (_, _, secondIsSecant)
4560+ || firstIsSecant != secondIsSecant || firstIsSecant != (function == secant))
4561+ return null;
4562+ // The half-odd one, which is written through the other; a power of it at least a half
4563+ // first, so that what is left of it is a whole power to expand.
4564+ static bool IsHalfOdd(Entity exponent) => exponent is Number.Rational rational and not Number.Integer
4565+ && rational.ERational.Denominator.Equals(EInteger.FromInt32(2)) && rational.ERational.Numerator.CanFitInInt32();
4566+ var order = sums[0].Exponent is Number.Rational { ERational.Sign: > 0 } && IsHalfOdd(sums[0].Exponent) ? new[] { 0, 1 } : new[] { 1, 0 };
4567+ var (half, other) = (sums[order[0]], sums[order[1]]);
4568+ if (!IsHalfOdd(half.Exponent))
4569+ return null;
4570+ var n = ((Number.Rational)half.Exponent).ERational;
4571+ // With u the other sum, the half-odd one is c (2 - u/a), whatever the signs.
4572+ var u = Variable.CreateUnique(expr, "u_conjugate");
4573+ var c = half.A;
4574+ var a = other.A;
4575+ var leftOver = n.Subtract(ERational.FromInt32(1).Divide(ERational.FromInt32(2)));
4576+ var inU = (MathS.Pow(u, other.Exponent - Number.Rational.Create(1, 2))
4577+ * MathS.Pow(2 - u / a, Number.Integer.Create(leftOver.ToEInteger()))).InnerSimplified;
4578+ inU = Functions.PartialFractions.Bare(inU);
4579+ if (inU.ContainsNode(x) || inU.Nodes.Any(node => node == MathS.NaN))
4580+ return null;
4581+ var inX = inU.Substitute(u, other.Radicand) * other.Radicand.Differentiate(x);
4582+ // The square of the constant: -c^(2n)/(a rate^2).
4583+ var constantSquared = -MathS.Pow(c, Number.Integer.Create(n.Multiply(ERational.FromInt32(2)).ToEInteger())) / (a * MathS.Sqr(rate));
4584+ if (!Functions.PartialFractions.HoldsAtSampledPoints(constantSquared * MathS.Sqr(inX), MathS.Sqr(expr), x))
4585+ return null;
4586+ if (Integration.ComputeAsAQuestionOfItsOwn(inU, u, integrateByParts) is not { } inTermsOfU
4587+ || inTermsOfU.Nodes.Any(node => node == MathS.NaN))
4588+ return null;
4589+ return expr * inTermsOfU.Substitute(u, other.Radicand) / inX;
4590+ }
4591+
45084592 /// <summary>
45094593 /// <c>tan(y) tan(2y)</c> written <c>sec(2y) - 1</c>, and the integrand asked again in the
45104594 /// one argument <c>2y</c>.
0 commit comments