Skip to content

Commit 61fb760

Browse files
Two half-odd powers of secant sums in the half angle's sine (#1828)
Part of #718. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent e1a1a3f commit 61fb760

3 files changed

Lines changed: 190 additions & 0 deletions

File tree

‎Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs‎

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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>.

‎Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
905905
// And `tan(y) tan(2y)` written `sec(2y) - 1` first, so that the rule reads one argument.
906906
if ((answer = IndefiniteIntegralSolver.SolveByWritingATangentTimesThatOfItsDoubleThroughTheSecant(expr, x, integrateByParts)) is { }) return answer;
907907
if ((answer = IndefiniteIntegralSolver.SolveByTheHalfAngleTangentBesideAHalfOddPowerOfOnePlusASecant(expr, x, integrateByParts)) is { }) return answer;
908+
// And beside a half-odd power of `c + d sec(y)`, which the half-angle tangent leaves as a
909+
// second root: in sin(y/2), where the two powers of cos(y) they bring make a whole one.
910+
if ((answer = IndefiniteIntegralSolver.SolveTwoHalfOddPowersOfSecantSumsByTheHalfAngleSine(expr, x, integrateByParts)) is { }) return answer;
908911
// And `a ± a cosh(y)` under a fractional power: `2a cosh(y/2)^2`, `-2a sinh(y/2)^2`.
909912
if ((answer = IndefiniteIntegralSolver.SolveByTheHalfAngleWhereOnePlusAHyperbolicCosineIsASquare(expr, x, integrateByParts)) is { }) return answer;
910913
// The hyperbolic sine's, off the real line: 1 + i sinh(y) is (cosh(y/2) + i sinh(y/2))^2.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Calculus
13+
{
14+
/// <summary>
15+
/// A half-odd power of <c>a ± a sec(y)</c> beside one of <c>c + d sec(y)</c>, in the half
16+
/// angle's sine, for a symbolic slope and shift too. Rubi's 4.5.2.1 and 4.5.2.3.
17+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
18+
/// </summary>
19+
[Trait("Area", "Calculus")]
20+
public sealed class TwoRootsOfSecantSumsIntegralTest
21+
{
22+
[Theory]
23+
[InlineData("sec(g + f*x)*sqrt(a + a*sec(g + f*x))/sqrt(c + d*sec(g + f*x))")]
24+
[InlineData("sqrt(c + d*sec(g + f*x))/sqrt(a + a*sec(g + f*x))")]
25+
[InlineData("sqrt(a + a*sec(g + f*x))/(c + d*sec(g + f*x))^(3/2)")]
26+
[InlineData("sqrt(a - a*sec(x))/sqrt(c + d*sec(x))")]
27+
[InlineData("csc(x)*sqrt(a + a*csc(x))/sqrt(c + d*csc(x))")]
28+
public void InTheHalfAngleSine(string integrand)
29+
{
30+
var integral = integrand.ToEntity().Integrate("x");
31+
var text = integral.Stringize();
32+
Assert.DoesNotContain("integral(", text);
33+
Assert.True(text.Length < 5000, $"{text.Length} characters of answer for {integrand}");
34+
Entity Pinned(Entity e) => e.Substitute("a", 1.3).Substitute("c", 0.9).Substitute("d", 0.4).Substitute("g", 0.2).Substitute("f", 0.7);
35+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
36+
var original = Pinned(integrand.ToEntity());
37+
var compared = 0;
38+
foreach (var at in new[] { -1.2, -0.7, 0.3, 0.8, 1.3, 2.9 })
39+
{
40+
var want = original.Substitute("x", at).EvalNumerical();
41+
var got = derivative.Substitute("x", at).EvalNumerical();
42+
if (want.IsNaN)
43+
continue;
44+
compared++;
45+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
46+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)),
47+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
48+
}
49+
Assert.True(compared >= 5, $"only {compared} points could be compared for {integrand}");
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)