@@ -4370,6 +4370,141 @@ Entity Integral(ERational power)
43704370 return roots < 2 ? answer : answer.Provided((secantKind ? MathS.Cos(argument) : MathS.Sin(argument)) >= Number.Integer.Zero);
43714371 }
43724372
4373+ /// <summary>
4374+ /// A half-odd power of <c>a ± a sec(y)</c> beside one of <c>c + d sec(y)</c>, integrated in
4375+ /// the half angle's sine, or its cosine for the minus: <c>a + a sec(y)</c> is
4376+ /// <c>2a cos(y/2)^2/cos(y)</c> and <c>c + d sec(y)</c> is <c>(c cos(y) + d)/cos(y)</c>, the two
4377+ /// powers of <c>cos(y)</c> make a whole one, and in <c>w = sin(y/2)</c>, where
4378+ /// <c>cos(y) = 1 - 2w^2</c> and <c>dy = 2 dw/cos(y/2)</c>, what is left is rational beside
4379+ /// the one root of <c>c + d - 2c w^2</c>. Either sum may be in the cosine instead, and the
4380+ /// cosecant's, with the sine, are the same by the complement.
4381+ /// </summary>
4382+ /// <remarks>
4383+ /// Rubi's <c>sec(e + f x) sqrt(a + a sec(e + f x))/sqrt(c + d sec(e + f x))</c> and eight more
4384+ /// of 4.5.2.1 and 4.5.2.3 were declined: the half-angle tangent reads a root of the one sum
4385+ /// and not of the other. The constants are not written: the answer is the integrand times the
4386+ /// antiderivative in <c>w</c> over what that differentiates back to, a quotient whose square
4387+ /// is one; and it is kept only where that square is one at the sampled points.
4388+ /// https://github.com/asc-community/AngouriMath/issues/718
4389+ /// </remarks>
4390+ internal static Entity? SolveTwoHalfOddPowersOfSecantSumsByTheHalfAngleSine(Entity expr, Entity.Variable x, bool integrateByParts)
4391+ {
4392+ if (!Integration.AnsweringTheQuestionAskedOrOneBelow)
4393+ return null;
4394+ Entity? argument = null;
4395+ bool? ofTheSecant = null;
4396+ foreach (var node in expr.Nodes)
4397+ {
4398+ if (TrigonometricArgument(node) is not { } thisArgument || !thisArgument.ContainsNode(x))
4399+ continue;
4400+ if (argument is null)
4401+ argument = thisArgument;
4402+ else if (argument != thisArgument)
4403+ return null;
4404+ var isSecant = node switch { Secantf or Cosf => true, Cosecantf or Sinf => false, _ => (bool?)null };
4405+ if (isSecant is not { } kind || ofTheSecant is { } seen && seen != kind)
4406+ return null;
4407+ ofTheSecant = kind;
4408+ }
4409+ if (argument is null || ofTheSecant is not { } secantKind
4410+ || !TreeAnalyzer.TryGetPolyLinear(argument, x, out var rate, out _) || rate.ContainsNode(x) || TreeAnalyzer.IsZero(rate))
4411+ return null;
4412+ if (!expr.Nodes.All(node => !node.ContainsNode(x)
4413+ || node is Variable or Sumf or Minusf or Mulf or Divf or Sinf or Cosf or Secantf or Cosecantf
4414+ || node is Powf(_, Number.Rational)))
4415+ return null;
4416+ // In y, the argument for the secant and its complement for the cosecant.
4417+ var y = Variable.CreateUnique(expr, "y_half_sine");
4418+ var secant = MathS.Sec(y);
4419+ var cosine = MathS.Cos(y);
4420+ var inY = expr.Replace(node => node switch
4421+ {
4422+ Secantf(var a) when a == argument => secant,
4423+ Cosf(var a) when a == argument => cosine,
4424+ Cosecantf(var a) when a == argument => secant,
4425+ Sinf(var a) when a == argument => cosine,
4426+ _ => node,
4427+ });
4428+ if (inY.ContainsNode(x))
4429+ return null;
4430+ (Entity Radicand, Number.Rational Exponent)? square = null, other = null;
4431+ Entity rest = Number.Integer.One;
4432+ foreach (var (factor, underneath) in FactorsOfTheIntegrand(inY))
4433+ {
4434+ if (factor is Powf(var radicand, Number.Rational exponent) && exponent is not Number.Integer && radicand.ContainsNode(y))
4435+ {
4436+ if (!exponent.ERational.Denominator.Equals(EInteger.FromInt32(2)) || !exponent.ERational.Numerator.CanFitInInt32())
4437+ return null;
4438+ var power = underneath ? Number.Rational.Create(exponent.ERational.Negate()) : exponent;
4439+ if (square is null && ReadAsOnePlusMinusAFunction(radicand, secant, cosine) is not null)
4440+ square = (radicand, power);
4441+ else if (other is null)
4442+ other = (radicand, power);
4443+ else
4444+ return null;
4445+ continue;
4446+ }
4447+ rest = underneath ? rest / factor : rest * factor;
4448+ }
4449+ if (square is not var (squareRadicand, p) || other is not var (otherRadicand, q)
4450+ || ReadAsOnePlusMinusAFunction(squareRadicand, secant, cosine) is not var (a, plus, squareInTheSecant)
4451+ || rest.Nodes.Any(node => node is Powf(var @base, Number.Rational exponent) && exponent is not Number.Integer && @base.ContainsNode(y)))
4452+ return null;
4453+ // The other as c + d f, for f the secant or the cosine.
4454+ var f = Variable.CreateUnique(expr, "f_half_sine");
4455+ (Entity C, Entity D, bool InTheSecant)? linear = null;
4456+ foreach (var (function, inTheSecant) in new[] { (secant, true), (cosine, false) })
4457+ {
4458+ var read = otherRadicand.Replace(node => node == function ? f : node);
4459+ if (!read.ContainsNode(y) && TreeAnalyzer.TryGetPolyLinear(read, f, out var d, out var c) && !d.ContainsNode(f) && !c.ContainsNode(f))
4460+ {
4461+ linear = (c, d, inTheSecant);
4462+ break;
4463+ }
4464+ }
4465+ if (linear is not var (cOther, dOther, otherInTheSecant))
4466+ return null;
4467+ // The powers of cos(y) the two sums bring, which have to make a whole one.
4468+ var cosinePower = ERational.Zero;
4469+ if (squareInTheSecant)
4470+ cosinePower = cosinePower.Subtract(p.ERational);
4471+ if (otherInTheSecant)
4472+ cosinePower = cosinePower.Subtract(q.ERational);
4473+ if (!cosinePower.IsInteger())
4474+ return null;
4475+ // Plus: a (1 + sec(y)) is 2a cos(y/2)^2/cos(y), in w = sin(y/2), cos(y) = 1 - 2w^2 and
4476+ // dy = 2 dw/cos(y/2). Minus: -2a sin(y/2)^2/cos(y), in w = cos(y/2), cos(y) = 2w^2 - 1
4477+ // and dy = -2 dw/sin(y/2). Either way the half angle's power over it is a whole power of
4478+ // 1 - w^2.
4479+ var w = Variable.CreateUnique(expr, "w_half_sine");
4480+ var cosineInW = plus ? 1 - 2 * MathS.Sqr(w) : 2 * MathS.Sqr(w) - 1;
4481+ var halfPower = p.ERational.Multiply(ERational.FromInt32(2)).Subtract(ERational.One).Divide(ERational.FromInt32(2));
4482+ if (!halfPower.IsInteger())
4483+ return null;
4484+ var restInW = rest.Replace(node => node == secant ? 1 / cosineInW : node == cosine ? cosineInW : node);
4485+ var otherInW = otherInTheSecant ? cOther * cosineInW + dOther : cOther + dOther * cosineInW;
4486+ var inW = (restInW
4487+ * MathS.Pow(1 - MathS.Sqr(w), Number.Integer.Create(halfPower.ToEInteger()))
4488+ * MathS.Pow(otherInW, q)
4489+ * MathS.Pow(cosineInW, Number.Integer.Create(cosinePower.ToEInteger()))).InnerSimplified;
4490+ // Bare: a zeroth power of 1 - w^2 comes back as one provided it is not zero.
4491+ inW = Functions.PartialFractions.Bare(inW);
4492+ if (inW.ContainsNode(y) || inW.ContainsNode(x) || inW.Nodes.Any(node => node == MathS.NaN))
4493+ return null;
4494+ var angle = secantKind ? argument : MathS.pi / 2 - argument;
4495+ var wOfX = plus ? MathS.Sin(angle / 2) : MathS.Cos(angle / 2);
4496+ var inX = inW.Substitute(w, wOfX) * wOfX.Differentiate(x);
4497+ // The integrand is that times a constant and a quotient of roots whose square is one; the
4498+ // square of the constant is 4 (±2a)^(2p) over the rate's.
4499+ var constantSquared = MathS.Pow(plus ? 2 * a : -2 * a, Number.Integer.Create(p.ERational.Numerator)) * 4 / MathS.Sqr(rate);
4500+ if (!Functions.PartialFractions.HoldsAtSampledPoints(constantSquared * MathS.Sqr(inX), MathS.Sqr(expr), x))
4501+ return null;
4502+ if (Integration.ComputeAsAQuestionOfItsOwn(inW, w, integrateByParts) is not { } inTermsOfW
4503+ || inTermsOfW.Nodes.Any(node => node == MathS.NaN))
4504+ return null;
4505+ return expr * inTermsOfW.Substitute(w, wOfX) / inX;
4506+ }
4507+
43734508 /// <summary>
43744509 /// <c>tan(y) tan(2y)</c> written <c>sec(2y) - 1</c>, and the integrand asked again in the
43754510 /// one argument <c>2y</c>.
0 commit comments