Skip to content

Commit b3ac1e8

Browse files
A power of the secant beside a power of a + i a tan is integrated in the sum, whatever the powers (#1812)
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent db33ae1 commit b3ac1e8

4 files changed

Lines changed: 192 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,24 @@ quotient of two such linears the sum is `(b - d t^2)^2 + (c t^2 - a)^2`. Rubi's
365365
| `"(c + d*tan(x))^(3/2)/(a + b*tan(x))^3".ToEntity().Integrate("x")` | `integral(...)` | the same |
366366
| `"1/((a + b*tan(x))^(3/2)*(c + d*tan(x))^(3/2))".ToEntity().Integrate("x")` | `integral(...)`; past a minute on the unreleased master | in the root of the quotient of the two, in a second |
367367

368+
### A power of the secant beside a power of `a + i a tan` is integrated in the sum, whatever the powers
369+
370+
**Answers where there were none.** `sec(x)^3 sqrt(a + i a tan(x))` was declined, with the rest of
371+
Rubi's 4.3.1.2 whose powers add up to no whole number. Under `u = a + i a tan(z)`,
372+
`du = i a sec(z)^2 dz` and `sec(z)^2 = (u/a) ((2 a - u)/a)`, so a power of the secant, or of the
373+
cosine, beside a power of the sum is a power of `u` beside a power of `2 a - u`. The antiderivative in
374+
`u` is found for a real `u`, and its conditions -- a radicand at least zero -- hold nowhere on the
375+
path, where `u` is not real; they are dropped, and the answer is kept only where its derivative is the
376+
integrand at sampled points. `cos(x)^9 (a + i a tan(x))^(7/2)` was answered on the unreleased master
377+
with the condition `1 + i tan(x) >= 0`, which no real `x` but the zeros of the tangent meets; it is
378+
answered without one now ([#718](https://github.com/asc-community/AngouriMath/issues/718)).
379+
380+
| Input | Was (2.5.0) | Now |
381+
|---|---|---|
382+
| `"sec(x)^3*sqrt(a + i*a*tan(x))".ToEntity().Integrate("x")` | `integral(...)` | powers of `sqrt(a - i a tan(x))` times a factor constant wherever it is continuous, 495 characters |
383+
| `"sec(x)^5/(a + i*a*tan(x))^(3/2)".ToEntity().Integrate("x")` | `integral(...)` | the same, 399 characters |
384+
| `"cos(x)^9*(a + i*a*tan(x))^(7/2)".ToEntity().Integrate("x")` | `integral(...)`; on the unreleased master, an answer provided `1 + i tan(x) >= 0` | 1,067 characters, unconditional |
385+
368386
### A quotient in `x^2` over a power of a linear in `x^2` and a biquadratic is split in `x^2`
369387

370388
**Shorter answers, sooner.** `sqrt(c + d tan(x)) (A + B tan(x) + C tan(x)^2)/(a + b tan(x))^3` was

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

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27007,6 +27007,124 @@ static Entity SumOfThePowers(Entity first, Entity second)
2700727007
=> (first + second).InnerSimplified is var sum && (sum is Number || sum.Complexity > 40) ? sum : sum.Simplify();
2700827008
}
2700927009

27010+
/// <summary>
27011+
/// A power of the secant, or of the cosine, of <c>z</c> beside a power of <c>A + i A tan(z)</c>,
27012+
/// whatever the powers, integrated in that sum: under <c>u = A + i A tan(z)</c>,
27013+
/// <c>du = i A sec(z)^2 dz</c> and <c>sec(z)^2 = (u/A) ((2 A - u)/A)</c>, so
27014+
/// <c>sec(z)^s u^n dz</c> is <c>u^n ((u/A) ((2 A - u)/A))^r du/(i A)</c> with
27015+
/// <c>r = (s - 2)/2</c>, up to a factor constant wherever it is continuous; and
27016+
/// <c>(u/A)^r ((2 A - u)/A)^r</c> is <c>(sec(z)^2)^r</c> exactly, the two being
27017+
/// <c>1 + i tan(z)</c> and <c>1 - i tan(z)</c>, whose arguments are opposite and less than a
27018+
/// right angle.
27019+
/// </summary>
27020+
/// <remarks>
27021+
/// <para>
27022+
/// <c>sec(x)^3 sqrt(a + i a tan(x))</c> was declined, with the rest of Rubi's 4.3.1.2 whose
27023+
/// powers do not add up to a whole number, which
27024+
/// <see cref="SolveAPowerOfAnImaginaryTangentBesideAPowerOfTheSecant"/> integrates as an
27025+
/// exponential and this does not: <c>sec^5/(a + i a tan)^(3/2)</c>,
27026+
/// <c>(e cos)^(3/2) sqrt(a + i a tan)</c>. In <c>u</c> each is a power of <c>u</c> beside a
27027+
/// power of <c>2 A - u</c>.
27028+
/// </para>
27029+
/// <para>
27030+
/// The constant is not written, as there: the answer is the integrand times the
27031+
/// antiderivative in <c>u</c> over what that antiderivative differentiates back to, a
27032+
/// quotient constant wherever it is continuous.
27033+
/// https://github.com/asc-community/AngouriMath/issues/718
27034+
/// </para>
27035+
/// </remarks>
27036+
internal static Entity? SolveAPowerOfTheSecantBesideAPowerOfAnImaginaryTangentSumInTheSum(Entity expr, Entity.Variable x, bool integrateByParts)
27037+
{
27038+
if (!expr.Nodes.Any(node => node is Tanf) || !expr.Nodes.Any(node => node is Secantf or Cosf))
27039+
return null;
27040+
Entity? argument = null, sum = null;
27041+
var plus = false;
27042+
Entity? sumPower = null;
27043+
Entity secantPower = Number.Integer.Zero;
27044+
var sawSecant = false;
27045+
foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr))
27046+
{
27047+
if (!factor.ContainsNode(x))
27048+
continue;
27049+
var (@base, power) = factor is Powf(var b, var p) && !p.ContainsNode(x)
27050+
? (b, p.Evaled is Number.Rational r ? r : p)
27051+
: (factor, (Entity)Number.Integer.One);
27052+
if (underneath)
27053+
power = power is Number.Rational numeric ? -numeric : (-power).InnerSimplified;
27054+
if (TryReadAnImaginaryTangent(@base, x, out var tangentOf, out var isPlus))
27055+
{
27056+
if (sumPower is not null || argument is not null && argument != tangentOf)
27057+
return null;
27058+
(sumPower, argument, plus, sum) = (power, tangentOf, isPlus, @base);
27059+
continue;
27060+
}
27061+
var (trigonometric, sign) = @base switch
27062+
{
27063+
Secantf => (@base, 1),
27064+
Cosf => (@base, -1),
27065+
Mulf(var left, var right) when !left.ContainsNode(x) && right is Secantf => (right, 1),
27066+
Mulf(var left, var right) when !left.ContainsNode(x) && right is Cosf => (right, -1),
27067+
Mulf(var left, var right) when !right.ContainsNode(x) && left is Secantf => (left, 1),
27068+
Mulf(var left, var right) when !right.ContainsNode(x) && left is Cosf => (left, -1),
27069+
_ => ((Entity?)null, 0)
27070+
};
27071+
var of = trigonometric switch { Secantf(var inner) => inner, Cosf(var inner) => inner, _ => null };
27072+
if (of is null || argument is not null && argument != of)
27073+
return null;
27074+
argument = of;
27075+
secantPower = sign == 1 ? secantPower + power : secantPower - power;
27076+
sawSecant = true;
27077+
}
27078+
if (sumPower is null || sum is null || argument is null || !sawSecant
27079+
|| !TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out _) || TreeAnalyzer.IsZero(slope))
27080+
return null;
27081+
secantPower = secantPower is Number ? secantPower : secantPower.InnerSimplified;
27082+
// Whole powers on both are a rational function of the sine and the cosine, which the
27083+
// rules for those answer in a fraction of a second, where in u they ran past five:
27084+
// `cos(x)^5/(a + i a tan(x))^3`.
27085+
if (sumPower is Number.Integer && secantPower is Number.Integer)
27086+
return null;
27087+
Entity constantTerm = Number.Integer.Zero;
27088+
foreach (var term in Sumf.LinearChildren(sum))
27089+
if (!term.ContainsNode(x))
27090+
constantTerm += term;
27091+
var a = constantTerm.InnerSimplified;
27092+
var u = Variable.CreateUnique(expr, "u_tan");
27093+
var half = ((secantPower - 2) / 2).InnerSimplified;
27094+
Entity squared = MathS.Pow(u / a, half) * MathS.Pow((2 * a - u) / a, half);
27095+
var inU = MathS.Pow(u, sumPower) * squared;
27096+
if (Integration.ComputeAsAQuestionOfItsOwn(inU, u, integrateByParts) is not { } inUAnswer
27097+
|| inUAnswer.Nodes.Any(node => node == MathS.NaN))
27098+
return null;
27099+
// du/dx is +-i A z' sec(z)^2.
27100+
var derivativeOfU = (plus ? MathS.i : -MathS.i) * a * slope * MathS.Pow(MathS.Sec(argument), 2);
27101+
var differentiatesBackTo = inU.Substitute(u, sum) * derivativeOfU;
27102+
// The antiderivative in u was found for a real u, and its conditions say so -- a radicand
27103+
// at least zero -- where u = A + i A tan(z) is not real: kept, they hold nowhere on the
27104+
// path and the answer has no value at all. The formula is an antiderivative wherever it is
27105+
// analytic, so the conditions go, each piecewise taken arm by arm, and an answer is kept
27106+
// only where its derivative is the integrand at the sampled points.
27107+
foreach (var arm in new[] { 0, -1 })
27108+
{
27109+
var formula = WithoutConditions(inUAnswer, arm);
27110+
var answer = expr * formula.Substitute(u, sum) / differentiatesBackTo;
27111+
if (Functions.PartialFractions.DerivativeHoldsAtSampledPoints(answer, expr, x))
27112+
return answer;
27113+
if (!inUAnswer.Nodes.Any(node => node is Piecewise))
27114+
break;
27115+
}
27116+
return null;
27117+
27118+
// Every condition dropped, and of every piecewise the first arm, or with -1 the last.
27119+
static Entity WithoutConditions(Entity e, int arm)
27120+
=> e.Replace(node => node switch
27121+
{
27122+
Providedf(var inner, _) => inner,
27123+
Piecewise piecewise when piecewise.Cases.Any() => arm == 0 ? piecewise.Cases.First().Expression : piecewise.Cases.Last().Expression,
27124+
_ => node
27125+
});
27126+
}
27127+
2701027128
/// <summary>
2701127129
/// <c>A + i A tan(z)</c> or <c>A - i A tan(z)</c>, with a constant <c>A</c>: the argument,
2701227130
/// and whether the imaginary unit comes with a plus.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,8 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
683683
// product and takes it apart, and so answers (c sec)^(5/2)/(a + i a tan)^(5/2) with the
684684
// wrong constant wherever the cosine is negative.
685685
if ((answer = IndefiniteIntegralSolver.SolveAPowerOfAnImaginaryTangentBesideAPowerOfTheSecant(expr, x, integrateByParts)) is { }) return answer;
686+
// And whatever the powers, in the sum, where they do not add up to a whole number.
687+
if ((answer = IndefiniteIntegralSolver.SolveAPowerOfTheSecantBesideAPowerOfAnImaginaryTangentSumInTheSum(expr, x, integrateByParts)) is { }) return answer;
686688
// And powers of the two conjugate sums, both not whole, as the exponential they make.
687689
if ((answer = IndefiniteIntegralSolver.SolveAConjugatePairOfImaginaryTangentSums(expr, x, integrateByParts)) is { }) return answer;
688690
// A rational function of the tangent beside a power of a + i a tan(z), in that sum.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 power of the secant or the cosine beside a power of <c>a + i a tan(x)</c>, the powers adding
16+
/// up to no whole number, integrated in that sum: under <c>u = a + i a tan(x)</c>,
17+
/// <c>sec(x)^2 = (u/a) ((2 a - u)/a)</c>. Rubi's 4.3.1.2. The integrands are complex for a real
18+
/// <c>x</c> and are compared as complex numbers.
19+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
20+
/// </summary>
21+
[Trait("Area", "Calculus")]
22+
public sealed class SecantBesideAnImaginaryTangentSumIntegralTest
23+
{
24+
[Theory]
25+
[InlineData("sec(x)^3*sqrt(a + i*a*tan(x))")]
26+
[InlineData("sec(x)^5/(a + i*a*tan(x))^(3/2)")]
27+
[InlineData("(k*cos(x))^(3/2)*sqrt(a + i*a*tan(x))")]
28+
[InlineData("cos(x)^9*(a + i*a*tan(x))^(7/2)")]
29+
[InlineData("(m*sec(x))^(2/3)*(a + i*a*tan(x))^(5/3)")]
30+
public void InTheSum(string integrand)
31+
{
32+
var integral = integrand.ToEntity().Integrate("x");
33+
var text = integral.Stringize();
34+
Assert.DoesNotContain("integral(", text);
35+
Assert.True(text.Length < 5000, $"{text.Length} characters of answer for {integrand}");
36+
Entity Pinned(Entity e) => e.Substitute("a", 1.3).Substitute("k", 0.7).Substitute("m", 1.1);
37+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
38+
var original = Pinned(integrand.ToEntity());
39+
var compared = 0;
40+
foreach (var at in new[] { -1.2, -0.7, 0.3, 0.8, 1.3, 2.9 })
41+
{
42+
var want = original.Substitute("x", at).EvalNumerical();
43+
var got = derivative.Substitute("x", at).EvalNumerical();
44+
if (want.IsNaN)
45+
continue;
46+
compared++;
47+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
48+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)),
49+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
50+
}
51+
Assert.True(compared >= 5, $"only {compared} points could be compared for {integrand}");
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)