Skip to content

Commit fc7aafe

Browse files
A power of a + i a tan beside a power of the secant is integrated as an exponential (#1747)
sqrt(a + i a tan(x))/sqrt(c sec(x)) is sqrt(a/c) e^(i x/2) wherever the cosine is positive, and was declined, with every pair of a power of a + i a tan and a power of the secant whose exponents add up to a whole number. a + i a tan(z) is a sec(z) e^(i z) on the real line, so the product is a constant on every interval where it is continuous times sec(z)^k e^(i n z), which in w = e^(i z) is a rational function of a root of w; it is integrated there, written back, and multiplied by the integrand over what that differentiates back to. Rubi's 4.3.1.2 goes from 0 to 43 of 43. 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 1cc2cda commit fc7aafe

4 files changed

Lines changed: 209 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,24 @@ that `sec(x)^2/(a + b sin(x))` is rational in them rather than declined at once
543543
| `"tan(x)^4/(a + a*cos(x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `tan(x/2)` |
544544
| `"csc(x)^2/(a + a*cos(x))".ToEntity().Integrate("x")` | `integral(...)` | `(tan(x/2)^3/12 + tan(x/2)/2 - 1/(4 tan(x/2)))/a` |
545545

546+
### A power of `a + i a tan` beside a power of the secant is integrated as an exponential
547+
548+
**Answers where there were none.** `sqrt(a + i a tan(x))/sqrt(c sec(x))` is `sqrt(a/c) e^(i x/2)`
549+
wherever the cosine is positive, and was declined, with every other pair of such powers, one of them
550+
not whole, whose exponents add up to a whole number: Rubi's 4.3.1.2 has 43 of them. `a + i a tan(z)` is
551+
`a sec(z) e^(i z)` on the real line, so the integrand is a constant on every interval where it is
552+
continuous times a power of the secant and an exponential, which in `w = e^(i z)` is rational in a root
553+
of `w`. Three of the 43, `(c sec(x))^p/(a + i a tan(x))^p` for `p` a half, three halves and five, were
554+
answered on the unreleased master through the power of a product taken apart, wrong by a constant
555+
wherever the cosine is negative; they are answered with the rest now
556+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
557+
558+
| Input | Was (2.5.0) | Now |
559+
|---|---|---|
560+
| `"sqrt(a + i*a*tan(x))/sqrt(c*sec(x))".ToEntity().Integrate("x")` | `integral(...)` | the integrand times `2/i`, through `e^(i x)` |
561+
| `"sqrt(c*sec(x))*sqrt(a + i*a*tan(x))".ToEntity().Integrate("x")` | `integral(...)` | the integrand times an antiderivative in `e^(i x/2)` over its derivative |
562+
| `"(c*sec(x))^(5/2)/(a + i*a*tan(x))^(5/2)".ToEntity().Integrate("x")` | `integral(...)` | the integrand times a power of `e^(i x)` |
563+
546564
### A rational function with complex coefficients is integrated through its real and imaginary parts
547565

548566
**Answers where there were none.** `1/((1 + i x)^2 (1 + x^2))` was declined: the rational

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

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23594,6 +23594,136 @@ static bool IsAWholeNumberOfSteps(ERational difference)
2359423594
return Integration.ComputeAsTheSameQuestion(inExponentials.InnerSimplified, x, integrateByParts);
2359523595
}
2359623596

23597+
/// <summary>
23598+
/// A power of <c>A + i A tan(z)</c> beside a power of the secant of the same argument, one
23599+
/// of them not whole and the two adding up to a whole number <c>k</c>, integrated as the
23600+
/// exponential it is: <c>A + i A tan(z)</c> is <c>A sec(z) e^(i z)</c> on the real line, so
23601+
/// <c>(A + i A tan(z))^n (c sec(z))^m</c> is a constant on every interval where it is
23602+
/// continuous times <c>sec(z)^k e^(i n z)</c>, which in <c>w = e^(i z)</c> is
23603+
/// <c>(2 w/(w^2 + 1))^k w^n</c>, and <c>dz</c> is <c>dw/(i w)</c>.
23604+
/// </summary>
23605+
/// <remarks>
23606+
/// <para>
23607+
/// <c>sqrt(a + i a tan(x))/sqrt(c sec(x))</c> is <c>sqrt(a/c) e^(i x/2)</c> wherever the
23608+
/// cosine is positive, and was declined, with every other power of the pair whose exponents
23609+
/// add up to a whole number: a root of a sum with the imaginary unit in it beside a root of the
23610+
/// secant is read by no rule, and <c>e^(i x/2)/cos(x)</c>, what the positive sums leave, is
23611+
/// declined as well. Rubi's 4.3.1.2.
23612+
/// </para>
23613+
/// <para>
23614+
/// The constant is not written: the answer is the integrand times the antiderivative in
23615+
/// <c>w</c> over what that antiderivative differentiates back to, <c>sec(z)^k (e^(i z))^n</c>
23616+
/// times <c>i</c> and the slope, a quotient whose logarithmic derivative is zero -- both
23617+
/// halves have <c>n (tan(z) + i) z'</c> -- so it is constant wherever it is continuous, and
23618+
/// the answer holds on every interval where the integrand is.
23619+
/// https://github.com/asc-community/AngouriMath/issues/718
23620+
/// </para>
23621+
/// </remarks>
23622+
internal static Entity? SolveAPowerOfAnImaginaryTangentBesideAPowerOfTheSecant(Entity expr, Entity.Variable x, bool integrateByParts)
23623+
{
23624+
if (!expr.Nodes.Any(node => node is Secantf) || !expr.Nodes.Any(node => node is Tanf))
23625+
return null;
23626+
Entity? argument = null;
23627+
var plus = false;
23628+
Number.Rational? tangentPower = null, secantPower = null;
23629+
Entity constant = Number.Integer.One;
23630+
Entity varying = Number.Integer.One;
23631+
foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr))
23632+
{
23633+
if (!factor.ContainsNode(x))
23634+
{
23635+
constant = underneath ? constant / factor : constant * factor;
23636+
continue;
23637+
}
23638+
varying = underneath ? varying / factor : varying * factor;
23639+
var (@base, power) = factor is Powf(var b, var p) && p.Evaled is Number.Rational r
23640+
? (b, r)
23641+
: (factor, (Number.Rational)Number.Integer.One);
23642+
if (underneath)
23643+
power = (Number.Rational)(-power);
23644+
if (TryReadAnImaginaryTangent(@base, x, out var tangentOf, out var isPlus))
23645+
{
23646+
if (tangentPower is not null || argument is not null && argument != tangentOf)
23647+
return null;
23648+
(tangentPower, argument, plus) = (power, tangentOf, isPlus);
23649+
continue;
23650+
}
23651+
var secant = @base switch
23652+
{
23653+
Secantf => @base,
23654+
Mulf(var left, Secantf right) when !left.ContainsNode(x) => right,
23655+
Mulf(Secantf left, var right) when !right.ContainsNode(x) => left,
23656+
_ => null
23657+
};
23658+
if (secant is not Secantf(var secantOf) || secantPower is not null || argument is not null && argument != secantOf)
23659+
return null;
23660+
(secantPower, argument) = (power, secantOf);
23661+
}
23662+
if (tangentPower is null || secantPower is null || argument is null
23663+
|| tangentPower is Number.Integer && secantPower is Number.Integer
23664+
|| (tangentPower + secantPower) is not Number.Integer { EInteger: var whole } || !whole.CanFitInInt32()
23665+
|| !TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out _) || TreeAnalyzer.IsZero(slope))
23666+
return null;
23667+
var k = whole.ToInt32Checked();
23668+
var phase = plus ? tangentPower : (Number.Rational)(-tangentPower);
23669+
// In w = e^(i z): sec(z)^k e^(i n z) dz is (2 w)^k (w^2 + 1)^(-k) w^(n - 1) dw / i.
23670+
var w = Variable.CreateUnique(expr, "w_exp");
23671+
var inW = k >= 0
23672+
? MathS.Pow(2, k) * MathS.Pow(w, (phase + k - 1).InnerSimplified) / MathS.Pow(MathS.Sqr(w) + 1, k)
23673+
: MathS.Pow(2, k) * MathS.Pow(MathS.Sqr(w) + 1, -k) * MathS.Pow(w, (phase + k - 1).InnerSimplified);
23674+
if (Integration.ComputeAsAQuestionOfItsOwn(inW.InnerSimplified, w, integrateByParts) is not { } inWAnswer
23675+
|| inWAnswer.Nodes.Any(node => node == MathS.NaN))
23676+
return null;
23677+
var exponential = MathS.Pow(MathS.e, MathS.i * argument);
23678+
var differentiatesBackTo = MathS.Pow(MathS.Sec(argument), k) * MathS.Pow(exponential, phase);
23679+
return constant * varying * inWAnswer.Substitute(w, exponential) / (MathS.i * slope * differentiatesBackTo);
23680+
}
23681+
23682+
/// <summary>
23683+
/// <c>A + i A tan(z)</c> or <c>A - i A tan(z)</c>, with a constant <c>A</c>: the argument,
23684+
/// and whether the imaginary unit comes with a plus.
23685+
/// </summary>
23686+
private static bool TryReadAnImaginaryTangent(Entity sum, Entity.Variable x, out Entity argument, out bool plus)
23687+
{
23688+
(argument, plus) = (Number.Integer.Zero, false);
23689+
if (sum is not Sumf and not Minusf || !sum.ContainsNode(x))
23690+
return false;
23691+
Entity free = Number.Integer.Zero;
23692+
Entity? coefficient = null;
23693+
Entity? tangentOf = null;
23694+
foreach (var term in Sumf.LinearChildren(sum))
23695+
{
23696+
if (!term.ContainsNode(x))
23697+
{
23698+
free += term;
23699+
continue;
23700+
}
23701+
if (coefficient is not null)
23702+
return false;
23703+
Entity factors = Number.Integer.One;
23704+
foreach (var factor in Mulf.LinearChildren(term))
23705+
if (factor is Tanf(var inner) && tangentOf is null)
23706+
tangentOf = inner;
23707+
else if (factor.ContainsNode(x))
23708+
return false;
23709+
else
23710+
factors *= factor;
23711+
if (tangentOf is null)
23712+
return false;
23713+
coefficient = factors;
23714+
}
23715+
if (coefficient is null || tangentOf is null || free == Number.Integer.Zero)
23716+
return false;
23717+
var ratio = Functions.PartialFractions.Bare((coefficient / free).InnerSimplified);
23718+
if (ratio.Evaled is not Number.Complex)
23719+
ratio = Functions.PartialFractions.Bare(ratio.Simplify());
23720+
plus = ratio.Evaled == MathS.i.Evaled;
23721+
if (!plus && ratio.Evaled != (-MathS.i).Evaled)
23722+
return false;
23723+
argument = tangentOf;
23724+
return true;
23725+
}
23726+
2359723727
/// <summary>
2359823728
/// <c>A cos(y) + i A sin(y)</c> below the bar, written as the exponential it is:
2359923729
/// <c>A e^(i y)</c>, and <c>A cos(y) - i A sin(y)</c> as <c>A e^(-i y)</c>. Beside a power

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,12 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
669669
if ((answer = IndefiniteIntegralSolver.SolveAnExponentialOfAHyperbolicFunctionBesideItsDerivative(expr, x, integrateByParts)) is { }) return answer;
670670
// `A + i A tan(z)` is `A e^(i z)/cos(z)`, which beside a polynomial is a shape the
671671
// closed rules answer, where the imaginary unit in the coefficient is read by none.
672+
// The same identity beside a power of the secant, where the powers are not whole and add
673+
// up to a whole number: an exponential over a power of the cosine, in e^(i z). Before the
674+
// rule below, which writes a power of the identity that is not whole as the power of a
675+
// product and takes it apart, and so answers (c sec)^(5/2)/(a + i a tan)^(5/2) with the
676+
// wrong constant wherever the cosine is negative.
677+
if ((answer = IndefiniteIntegralSolver.SolveAPowerOfAnImaginaryTangentBesideAPowerOfTheSecant(expr, x, integrateByParts)) is { }) return answer;
672678
if ((answer = IndefiniteIntegralSolver.SolveByWritingAnImaginaryTangentAsAnExponential(expr, x, integrateByParts)) is { }) return answer;
673679
// And `A cos(z) + i A sin(z)`, which is `A e^(i z)`, where no rotation is real.
674680
if ((answer = IndefiniteIntegralSolver.SolveByWritingAnImaginarySumOfACosineAndASineAsAnExponential(expr, x, integrateByParts)) is { }) return answer;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 <c>a + i a tan(x)</c> beside a power of the secant, one of them not whole and
16+
/// the two adding up to a whole number: <c>a + i a tan(x)</c> is <c>a sec(x) e^(i x)</c> on
17+
/// the real line, so the integrand is a constant on every interval where it is continuous times
18+
/// a power of the secant and an exponential. Rubi's 4.3.1.2. The integrands are complex along
19+
/// the real line and are compared as complex numbers, where the cosine is negative as well,
20+
/// which is where the fourth row's answer on master was off by a constant factor.
21+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
22+
/// </summary>
23+
[Trait("Area", "Calculus")]
24+
public sealed class ImaginaryTangentBesideTheSecantIntegralTest
25+
{
26+
[Theory]
27+
[InlineData("sqrt(a + i*a*tan(x))/sqrt(k*sec(x))")]
28+
[InlineData("sqrt(k*sec(x))*sqrt(a + i*a*tan(x))")]
29+
[InlineData("(a + i*a*tan(x))^(3/2)/(k*sec(x))^(7/2)")]
30+
[InlineData("(k*sec(x))^(5/2)/(a + i*a*tan(x))^(5/2)")]
31+
[InlineData("(k*sec(c + d*x))^(3/2)*(a - i*a*tan(c + d*x))^(3/2)")]
32+
public void AsTheExponentialItIs(string integrand)
33+
{
34+
var integral = integrand.ToEntity().Integrate("x");
35+
Assert.DoesNotContain("integral(", integral.Stringize());
36+
Assert.DoesNotContain("NaN", integral.Stringize());
37+
Entity Pinned(Entity e) => e.Substitute("a", 1.3).Substitute("k", 0.8).Substitute("c", 0.4).Substitute("d", 1.1);
38+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
39+
var original = Pinned(integrand.ToEntity());
40+
var compared = 0;
41+
foreach (var at in new[] { -2.6, -1.1, -0.4, 0.3, 0.8, 1.2, 2.0 })
42+
{
43+
var want = original.Substitute("x", at).EvalNumerical();
44+
if (want.IsNaN)
45+
continue;
46+
compared++;
47+
var got = derivative.Substitute("x", at).EvalNumerical();
48+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
49+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)),
50+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
51+
}
52+
Assert.True(compared >= 6, $"only {compared} points could be compared for {integrand}");
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)