From a676acea06afa51c614a295afe80504ad98a387f Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sat, 3 Oct 2026 05:16:34 +0000 Subject: [PATCH] A rational function with complex coefficients is integrated through its real and imaginary parts 1/((1 + i x)^2 (1 + x^2)) was declined: the rational integrator reads coefficients that are rational numbers. Times the conjugate of its denominator over itself, N/D = N D'/(D D') with D' the polynomial whose coefficients are those of D conjugated, the denominator has rational coefficients and the numerator is P + i S with P and S rational, so the integral is that of P/(D D') and i times that of S/(D D'). A product of symbols common to a polynomial's coefficients, a + i a x, comes out with the constants. The tangent substitution makes such a function of Rubi's (a + i a tan(c + d x))^n. Part of https://github.com/asc-community/AngouriMath/issues/718 Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 21 ++++ .../Integration/IndefiniteIntegralSolver.cs | 111 ++++++++++++++++++ .../Integration/Integration.Definition.cs | 3 + .../ComplexCoefficientRationalIntegralTest.cs | 83 +++++++++++++ 4 files changed, 218 insertions(+) create mode 100644 Sources/Tests/UnitTests/Calculus/ComplexCoefficientRationalIntegralTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index d036a8063..695d11fb1 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -358,6 +358,27 @@ that `sec(x)^2/(a + b sin(x))` is rational in them rather than declined at once | `"tan(x)^4/(a + a*cos(x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `tan(x/2)` | | `"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` | +### A rational function with complex coefficients is integrated through its real and imaginary parts + +**Answers where there were none.** `1/((1 + i x)^2 (1 + x^2))` was declined: the rational +integrator reads coefficients that are rational numbers, and `i` is not one. Times the conjugate +of its denominator over itself, `N/D = N D'/(D D')` with `D'` the polynomial whose coefficients are +those of `D` conjugated, the denominator has rational coefficients and the numerator is `P + i S` +with `P` and `S` rational: the integral is that of `P/(D D')` and `i` times that of `S/(D D')`. A +product of symbols common to a polynomial's coefficients, `a + i a x`, comes out with the +constants. The tangent substitution makes such a function of Rubi's `(a + i a tan(c + d x))^n`, by +the hundred in its 4.3.2.1, 4.3.3.1 and 4.3.1.2; their answers are in `tan(c + d x)`, on each +interval between the poles of the tangent, as the substitution's are +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"1/((1 + i*x)^2*(1 + x^2))".ToEntity().Integrate("x")` | `integral(...)` | a rational function of `x` and an arctangent | +| `"1/(a + i*a*tan(x))^2".ToEntity().Integrate("x")` | `integral(...)` | the same in `tan(x)` | +| `"tan(x)^3/(a + i*a*tan(x))^4".ToEntity().Integrate("x")` | `integral(...)` | the same | +| `"(A + B*tan(x))/(a + i*a*tan(x))^2".ToEntity().Integrate("x")` | `integral(...)` | the same | +| `"1/(a + i*a*tan(c + d*x))^3".ToEntity().Integrate("x")` | `integral(...)` | the same in `tan(c + d x)` | + ### `NaN` was returned as the antiderivative of something that has one **A wrong answer, not a missing one.** `1/(a*x^2)` came back as `NaN + C`, and `NaN` is this diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 261c1f821..60f58bad7 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -1508,6 +1508,117 @@ node is Powf(var radicand, Number.Rational r) && r is not Number.Integer && radi return RothsteinTrager.Integrate(numerator, denominator, x); } + /// + /// A rational function with complex numbers among its coefficients, 1/((1 + i x)^2 (1 + x^2)), + /// which is what the tangent substitution makes of 1/(a + i a tan(x))^2. Times the + /// conjugate of its denominator over itself, N/D = N D'/(D D') with D' the + /// polynomial whose coefficients are those of D conjugated, the denominator has real + /// coefficients and the numerator is P + i S with P and S real: the + /// integral is that of P/(D D') and i times that of S/(D D'), which the + /// rules for real coefficients answer. An identity of polynomials, so exact wherever the + /// quotient is defined; the factors free of x are taken out first, and may hold + /// symbols. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + internal static Entity? SolveARationalFunctionWithComplexCoefficients(Entity expr, Entity.Variable x, bool integrateByParts) + { + if (!TryReadAsQuotient(expr, out var numerator, out var denominator)) + return null; + Entity constant = Number.Integer.One; + Number.Complex[]? Read(Entity side, bool below) + { + Entity rest = Number.Integer.One; + foreach (var factor in Mulf.LinearChildren(side)) + { + if (!factor.ContainsNode(x)) + constant = below ? constant / factor : constant * factor; + else + rest = rest == Number.Integer.One ? factor : rest * factor; + } + if (!TreeAnalyzer.TryGetPolynomial(rest, x, out var terms) || terms.Count == 0 + || terms.Keys.Any(power => power.Sign < 0 || power.CompareTo(EInteger.FromInt32(MaximumComplexRationalDegree)) > 0)) + return null; + var coefficients = new Number.Complex[terms.Keys.Max()!.ToInt32Checked() + 1]; + for (var i = 0; i < coefficients.Length; i++) + coefficients[i] = Number.Integer.Zero; + // A product of symbols common to every coefficient, `a + i a x`, goes out with the + // other constants: each coefficient is its numbers times the same rest. + Entity? scale = null; + foreach (var term in terms) + { + Number.Complex number = Number.Integer.One; + Entity symbols = Number.Integer.One; + foreach (var factor in Mulf.LinearChildren(term.Value.InnerSimplified)) + if (factor is Number.Complex value) + number *= value; + else + symbols = symbols == Number.Integer.One ? factor : symbols * factor; + if (scale is null) + scale = symbols; + else if (symbols != scale) + return null; + coefficients[term.Key.ToInt32Checked()] = number; + } + if (scale is { } common && common != Number.Integer.One) + constant = below ? constant / common : constant * common; + return coefficients; + } + if (Read(numerator, false) is not { } above || Read(denominator, true) is not { } below || below.Length < 2) + return null; + static bool OffTheRealLine(Number.Complex[] polynomial) => polynomial.Any(c => !c.ImaginaryPart.IsZero); + if (!OffTheRealLine(above) && !OffTheRealLine(below)) + return null; + static Number.Complex[] Times(Number.Complex[] left, Number.Complex[] right) + { + var product = new Number.Complex[left.Length + right.Length - 1]; + for (var i = 0; i < product.Length; i++) + product[i] = Number.Integer.Zero; + for (var i = 0; i < left.Length; i++) + for (var j = 0; j < right.Length; j++) + product[i + j] = product[i + j] + left[i] * right[j]; + return product; + } + var conjugate = below.Select(c => c.Conjugate).ToArray(); + var real = Times(below, conjugate); + var top = Times(above, conjugate); + Entity Polynomial(IEnumerable coefficients) + { + Entity sum = Number.Integer.Zero; + var power = 0; + foreach (var coefficient in coefficients) + { + if (!coefficient.IsZero) + sum += power == 0 ? coefficient : coefficient * MathS.Pow(x, power); + power++; + } + return sum; + } + if (real.Any(c => !c.ImaginaryPart.IsZero)) + return null; + var bottom = Polynomial(real.Select(c => c.RealPart)); + var realPart = Polynomial(top.Select(c => c.RealPart)); + var imaginaryPart = Polynomial(top.Select(c => c.ImaginaryPart)); + Entity answer = Number.Integer.Zero; + if (!TreeAnalyzer.IsZero(realPart)) + { + if (Integration.ComputeIndefiniteIntegral(realPart / bottom, x, integrateByParts) is not { } integral) + return null; + answer += integral; + } + if (!TreeAnalyzer.IsZero(imaginaryPart)) + { + if (Integration.ComputeIndefiniteIntegral(imaginaryPart / bottom, x, integrateByParts) is not { } integral) + return null; + answer += MathS.i * integral; + } + // In the generic case, as the rules for the two parts answer it. + return Functions.PartialFractions.Bare((constant * answer).InnerSimplified); + } + + // The degree past which a rational function with complex coefficients is left alone: its + // denominator's degree doubles under the conjugate. + private const int MaximumComplexRationalDegree = 12; + /// /// over with the greatest /// power of that divides both taken out of each, the denominator's diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index 0058ebd66..25139c066 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -901,6 +901,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => // cubic. The residues decide the field the answer needs, not the roots of the // denominator, and the Rothstein-Trager resultant finds them. if ((answer = IndefiniteIntegralSolver.SolveByRothsteinTrager(expr, x)) is { }) return answer; + // A rational function with complex coefficients, written as its real and imaginary parts + // over a real denominator, which the rules above read. + if ((answer = IndefiniteIntegralSolver.SolveARationalFunctionWithComplexCoefficients(expr, x, integrateByParts)) is { }) return answer; // The tangent substitution again, and this time for a *rational* function: a repeated // irreducible quadratic beside a negative power of the variable is `sin^p cos^q` with // both exponents whole, which the recurrences close. Below partial fractions rather diff --git a/Sources/Tests/UnitTests/Calculus/ComplexCoefficientRationalIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/ComplexCoefficientRationalIntegralTest.cs new file mode 100644 index 000000000..53e961c15 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ComplexCoefficientRationalIntegralTest.cs @@ -0,0 +1,83 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// A rational function with complex numbers among its coefficients, written as its real and + /// imaginary parts over the denominator times its conjugate, which is real: Rubi's + /// (a + i a tan)^n family under the tangent substitution. + /// #718 + /// + /// + /// Checked by differentiating back at real points, both parts compared, since the integrand is + /// complex there; the symbols are pinned after the integral is taken. + /// + [Trait("Area", "Calculus")] + public sealed class ComplexCoefficientRationalIntegralTest + { + private static readonly double[] Points = { -0.7, 0.3, 0.9, 1.3, 2.2 }; + + private static void DifferentiatesBack(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("integral(", integral.Stringize()); + var derivative = integral.Substitute("C", 0).Differentiate("x"); + Entity original = integrand.ToEntity(); + foreach (var (name, value) in new[] { ("a", 1.3), ("A", 0.7), ("B", -0.4), ("c", 0.2), ("d", 1.1) }) + { + derivative = derivative.Substitute(name, value); + original = original.Substitute(name, value); + } + var compared = 0; + foreach (var at in Points) + { + var got = derivative.Substitute("x", at).EvalNumerical(); + var want = original.Substitute("x", at).EvalNumerical(); + if (got.IsNaN || want.IsNaN) + continue; + compared++; + var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart); + var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)); + Assert.True(difference / scale < 1e-9, + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); + } + Assert.True(compared >= 4, $"only {compared} points could be compared for {integrand}"); + } + + /// Numbers off the real line among the coefficients. + [Theory] + [InlineData("1/((1 + i*x)^2*(1 + x^2))")] + [InlineData("x^3/((1 + i*x)^4*(1 + x^2))")] + [InlineData("(2 - i*x)/((x - i)^2*(x^2 + 4))")] + public void WithNumbers(string integrand) => DifferentiatesBack(integrand); + + /// + /// A symbol common to the coefficients, a + i a x, goes out with the constants. + /// + [Theory] + [InlineData("1/((a + i*a*x)*(1 + x^2))")] + [InlineData("x^3/((a + i*a*x)^4*(1 + x^2))")] + public void WithACommonSymbol(string integrand) => DifferentiatesBack(integrand); + + /// + /// Rubi's 4.3.2.1 and 4.3.3.1, which the tangent substitution makes rational functions with + /// complex coefficients. + /// + [Theory] + [InlineData("1/(a + i*a*tan(x))")] + [InlineData("1/(a + i*a*tan(x))^2")] + [InlineData("tan(x)^3/(a + i*a*tan(x))^4")] + [InlineData("(A + B*tan(x))/(a + i*a*tan(x))^2")] + [InlineData("1/(a + i*a*tan(c + d*x))^3")] + public void APowerOfAPlusIATangent(string integrand) => DifferentiatesBack(integrand); + } +}