Skip to content

Commit a294a9c

Browse files
Rafael-SOWNetclaude
andcommitted
Integrate an arctangent over a power of d (1 +- i c x) under the tangent
Under x = tan(u)/c, 1 + i tan(u) is e^(i u)/cos(u), so x^m (a + b atan(c x))^p/(d (1 +- i c x))^k is (a + b u)^p (tan(u)/c)^m d^(-k) e^(-+i k u) cos(u)^(k - 2)/c, a polynomial in u times exponentials and trigonometric functions. Rubi's 5.3.4 (a + b atan(c x))^2/(d + i c d x)^2 and seven like it ran out of time. Part of #718 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 1e79b0e commit a294a9c

3 files changed

Lines changed: 126 additions & 0 deletions

File tree

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29364,6 +29364,70 @@ bool HoldsItsValue(Entity factor)
2936429364
return (alpha / (p0 * slope) * product).InnerSimplified;
2936529365
}
2936629366

29367+
/// <summary>
29368+
/// <c>x^m (a + b atan(c x))^p</c> over a power of <c>d (1 +- i c x)</c>, under <c>x = tan(u)/c</c>, where
29369+
/// <c>1 + i tan(u)</c> is <c>e^(i u)/cos(u)</c>: the integrand in <c>u</c> is
29370+
/// <c>(a + b u)^p (tan(u)/c)^m d^(-k) e^(-+i k u) cos(u)^k/(c cos(u)^2)</c>, a polynomial in <c>u</c> times
29371+
/// exponentials and trigonometric functions, and the answer is written back at <c>u = atan(c x)</c>.
29372+
/// </summary>
29373+
/// <remarks>
29374+
/// <c>1 + i tan(u) = (cos(u) + i sin(u))/cos(u)</c> holds for every <c>u</c> where the tangent is defined,
29375+
/// and <c>atan(c x)</c> takes each real <c>x</c> into <c>(-pi/2, pi/2)</c>, where it is. Rubi's 5.3.4
29376+
/// <c>(a + b atan(c x))^2/(d + i c d x)^2</c> and seven like it ran out of time.
29377+
/// https://github.com/asc-community/AngouriMath/issues/718
29378+
/// </remarks>
29379+
internal static Entity? SolveAnArctangentOverAPowerOfOnePlusICXUnderTheTangent(Entity expr, Entity.Variable x, bool integrateByParts)
29380+
{
29381+
if (!Integration.AnsweringTheQuestionAsked)
29382+
return null;
29383+
var (numerator, denominator) = Functions.SingleQuotient.Of(expr);
29384+
Arctanf? arctangent = null;
29385+
Entity? rate = null;
29386+
foreach (var node in numerator.Nodes)
29387+
if (node is Arctanf found && found.ContainsNode(x))
29388+
{
29389+
if (arctangent is not null && arctangent != found)
29390+
return null;
29391+
if (!TreeAnalyzer.TryGetPolyLinear(found.Argument, x, out var slope, out var offset) || !Functions.PartialFractions.IsZeroAsAValue(offset))
29392+
return null;
29393+
(arctangent, rate) = (found, slope);
29394+
}
29395+
if (arctangent is null || rate is null)
29396+
return null;
29397+
// Below the bar one power of a linear d (1 + s i c x), s = +-1, and nothing else with x.
29398+
Entity? linear = null;
29399+
var k = 0;
29400+
Entity constant = Number.Integer.One;
29401+
foreach (var factor in Mulf.LinearChildren(denominator))
29402+
{
29403+
if (!factor.ContainsNode(x)) { constant *= factor; continue; }
29404+
var (b, e) = factor is Powf(var pb, Number.Integer { EInteger: var pe }) && pe.Sign > 0 && pe.CanFitInInt32() ? (pb, pe.ToInt32Unchecked()) : (factor, 1);
29405+
if (linear is not null)
29406+
return null;
29407+
(linear, k) = (b, e);
29408+
}
29409+
if (linear is null || !TreeAnalyzer.TryGetPolyLinear(linear, x, out var lSlope, out var lOffset) || Functions.PartialFractions.IsZeroAsAValue(lOffset))
29410+
return null;
29411+
// The ratio of slope to constant is +-i c.
29412+
var ratio = (lSlope / (lOffset * MathS.i * rate)).InnerSimplified;
29413+
int sign;
29414+
if (Functions.PartialFractions.IsZeroAsAValue((ratio - 1).InnerSimplified)) sign = 1;
29415+
else if (Functions.PartialFractions.IsZeroAsAValue((ratio + 1).InnerSimplified)) sign = -1;
29416+
else return null;
29417+
var u = Variable.CreateUnique(expr, "u_atan");
29418+
// Above the bar: a power of x and the function of the arctangent.
29419+
var above = numerator.Substitute(arctangent, u);
29420+
if (above.Substitute(x, Number.Integer.Zero).ContainsNode(x))
29421+
return null;
29422+
var inU = above.Substitute(x, MathS.Tan(u) / rate)
29423+
* MathS.Pow(lOffset, Number.Integer.Create(-k)) * MathS.Pow(MathS.e, -sign * k * MathS.i * u) * (k == 2 ? Number.Integer.One : MathS.Pow(MathS.Cos(u), Number.Integer.Create(k - 2)))
29424+
/ (constant * rate);
29425+
if (Integration.ComputeAsAQuestionOfItsOwn(inU.InnerSimplified, u, integrateByParts) is not { } integrated)
29426+
return null;
29427+
var answer = integrated.Substitute(u, arctangent).InnerSimplified;
29428+
return Functions.PartialFractions.DerivativeDiffersWhereReal(answer, expr, x) ? null : answer;
29429+
}
29430+
2936729431
/// <summary>
2936829432
/// A power of a constant multiple of a radicand that a logarithm holds under a root,
2936929433
/// written over that radicand: <c>acosh(c x)</c> is <c>ln(c x + sqrt(c^2 x^2 - 1))</c>,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,9 @@ private static Entity WithItsPiecewiseSumsCombined(Entity expr)
664664
// A sum free of the variable that comes to a number multiplied out, as that number,
665665
// before anything reads a coefficient or an exponent in it: `2 + 2 a - 2 (1 + a)`.
666666
if ((answer = IndefiniteIntegralSolver.SolveByFoldingConstantSumsToTheirNumbers(expr, x, integrateByParts)) is { }) return answer;
667+
// An arctangent over a power of d (1 +- i c x), under x = tan(u)/c, where 1 + i tan(u) is
668+
// e^(i u)/cos(u): the rules below ran out of time on `(a + b atan(c x))^2/(d + i c d x)^2`.
669+
if ((answer = IndefiniteIntegralSolver.SolveAnArctangentOverAPowerOfOnePlusICXUnderTheTangent(expr, x, integrateByParts)) is { }) return answer;
667670
// A symbolic power of a sine, cosine, secant or cosecant times `alpha + beta f^2`, where it is
668671
// the derivative of `cos sin^(m + 1)`, `sin cos^(m + 1)`, `tan sec^m` or `cot csc^m`.
669672
if ((answer = IndefiniteIntegralSolver.SolveASymbolicPowerOfATrigonometricFunctionAsTheDerivativeOfAProduct(expr, x)) is { }) return answer;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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>x^m (a + b atan(c x))^p</c> over a power of <c>d (1 +- i c x)</c>, under <c>x = tan(u)/c</c>, where
16+
/// <c>1 + i tan(u)</c> is <c>e^(i u)/cos(u)</c>: Rubi's 5.3.4.
17+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
18+
/// </summary>
19+
[Trait("Area", "Calculus")]
20+
public sealed class AnArctangentOverAPowerOfOnePlusICXIntegralTest
21+
{
22+
private static void DifferentiatesBack(string integrand, params (string, double)[] pins)
23+
{
24+
var integral = integrand.ToEntity().Integrate("x");
25+
Assert.DoesNotContain("integral(", integral.Stringize());
26+
27+
var pinned = integral.Substitute("C", 0);
28+
Entity original = integrand.ToEntity();
29+
foreach (var (name, value) in pins)
30+
{
31+
pinned = pinned.Substitute(name, value);
32+
original = original.Substitute(name, value);
33+
}
34+
var derivative = pinned.Differentiate("x");
35+
foreach (var at in new[] { -2.3, -0.7, 0.4, 1.9 })
36+
{
37+
var got = derivative.Substitute("x", at).EvalNumerical();
38+
var want = original.Substitute("x", at).EvalNumerical();
39+
var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart);
40+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
41+
Assert.True(difference / scale < 1e-8,
42+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
43+
}
44+
}
45+
46+
// d with the sign that keeps the argument real at every sampled x, and |d| below 2/2.3^2
47+
// so that the inverse sine and cosine stay inside [-1, 1]; off it the exact checks hold too.
48+
[Theory]
49+
[InlineData("(a+b*atan(c*x))^2/(d+i*c*d*x)^2")]
50+
[InlineData("x*(a+b*atan(c*x))^2/(d+i*c*d*x)^3")]
51+
[InlineData("(a+b*atan(c*x))^3/(d+i*c*d*x)^4")]
52+
[InlineData("(a+b*atan(c*x))/(d-i*c*d*x)^3")]
53+
public void IsAnsweredAndDifferentiatesBack(string integrand)
54+
{
55+
DifferentiatesBack(integrand, ("a", 0.6), ("b", 1.25), ("c", 0.4), ("d", 1.5));
56+
DifferentiatesBack(integrand, ("a", -1.5), ("b", -0.25), ("c", -2), ("d", 0.4));
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)