Skip to content

Commit e7ef0b7

Browse files
A secant beside powers of its conjugate sums, in the sum (#1829)
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 61fb760 commit e7ef0b7

3 files changed

Lines changed: 138 additions & 0 deletions

File tree

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

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
908908
// And beside a half-odd power of `c + d sec(y)`, which the half-angle tangent leaves as a
909909
// second root: in sin(y/2), where the two powers of cos(y) they bring make a whole one.
910910
if ((answer = IndefiniteIntegralSolver.SolveTwoHalfOddPowersOfSecantSumsByTheHalfAngleSine(expr, x, integrateByParts)) is { }) return answer;
911+
// And the secant beside powers of a + a sec(y) and c - c sec(y), whose product is a
912+
// square of the tangent: in u = a + a sec(y), a symbolic power of it too.
913+
if ((answer = IndefiniteIntegralSolver.SolveASecantBesidePowersOfItsConjugateSums(expr, x, integrateByParts)) is { }) return answer;
911914
// And `a ± a cosh(y)` under a fractional power: `2a cosh(y/2)^2`, `-2a sinh(y/2)^2`.
912915
if ((answer = IndefiniteIntegralSolver.SolveByTheHalfAngleWhereOnePlusAHyperbolicCosineIsASquare(expr, x, integrateByParts)) is { }) return answer;
913916
// The hyperbolic sine's, off the real line: 1 + i sinh(y) is (cosh(y/2) + i sinh(y/2))^2.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
/// <c>sec(y) (a ± a sec(y))^m (c ∓ c sec(y))^n</c>, with <c>n</c> half-odd and <c>m</c> a
16+
/// symbol, in <c>u = a ± a sec(y)</c>, and the cosecant's. Rubi's 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 ASecantBesideConjugateSumsIntegralTest
21+
{
22+
[Theory]
23+
[InlineData("sec(g + f*x)*(a + a*sec(g + f*x))^m*(c - c*sec(g + f*x))^(5/2)")]
24+
[InlineData("sec(g + f*x)*(a + a*sec(g + f*x))^m*sqrt(c - c*sec(g + f*x))")]
25+
[InlineData("sec(x)*(a - a*sec(x))^m*sqrt(c + c*sec(x))")]
26+
[InlineData("csc(x)*(a + a*csc(x))^m*sqrt(c - c*csc(x))")]
27+
public void InTheSum(string integrand)
28+
{
29+
var integral = integrand.ToEntity().Integrate("x");
30+
var text = integral.Stringize();
31+
Assert.DoesNotContain("integral(", text);
32+
Assert.True(text.Length < 2000, $"{text.Length} characters of answer for {integrand}");
33+
Entity Pinned(Entity e) => e.Substitute("a", 1.3).Substitute("c", 0.9).Substitute("m", 2.3).Substitute("g", 0.2).Substitute("f", 0.7);
34+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
35+
var original = Pinned(integrand.ToEntity());
36+
var compared = 0;
37+
foreach (var at in new[] { -1.2, -0.7, 0.3, 0.8, 1.3, 2.9 })
38+
{
39+
var want = original.Substitute("x", at).EvalNumerical();
40+
var got = derivative.Substitute("x", at).EvalNumerical();
41+
if (want.IsNaN)
42+
continue;
43+
compared++;
44+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
45+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)),
46+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
47+
}
48+
Assert.True(compared >= 5, $"only {compared} points could be compared for {integrand}");
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)