Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,117 @@ node is Powf(var radicand, Number.Rational r) && r is not Number.Integer && radi
return RothsteinTrager.Integrate(numerator, denominator, x);
}

/// <summary>
/// A rational function with complex numbers among its coefficients, <c>1/((1 + i x)^2 (1 + x^2))</c>,
/// which is what the tangent substitution makes of <c>1/(a + i a tan(x))^2</c>. Times the
/// conjugate of its denominator over itself, <c>N/D = N D'/(D D')</c> with <c>D'</c> the
/// polynomial whose coefficients are those of <c>D</c> conjugated, the denominator has real
/// coefficients and the numerator is <c>P + i S</c> with <c>P</c> and <c>S</c> real: the
/// integral is that of <c>P/(D D')</c> and <c>i</c> times that of <c>S/(D D')</c>, which the
/// rules for real coefficients answer. An identity of polynomials, so exact wherever the
/// quotient is defined; the factors free of <c>x</c> are taken out first, and may hold
/// symbols.
/// https://github.com/asc-community/AngouriMath/issues/718
/// </summary>
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<Number.Real> 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;

/// <summary>
/// <paramref name="numerator"/> over <paramref name="denominator"/> with the greatest
/// power of <paramref name="x"/> that divides both taken out of each, the denominator's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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
/// <c>(a + i a tan)^n</c> family under the tangent substitution.
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
[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}");
}

/// <summary>Numbers off the real line among the coefficients.</summary>
[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);

/// <summary>
/// A symbol common to the coefficients, <c>a + i a x</c>, goes out with the constants.
/// </summary>
[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);

/// <summary>
/// Rubi's 4.3.2.1 and 4.3.3.1, which the tangent substitution makes rational functions with
/// complex coefficients.
/// </summary>
[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);
}
}
Loading