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
17 changes: 17 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,23 @@ too. A correct antiderivative in an unhelpful form, where there was none at all.
`e^(x^2)` is still declined, and correctly: its exponent is not linear and it has no elementary
antiderivative.

### A polynomial over a power of a binomial past the cube is integrated

**Answers where there were none.** `P(x)/(a + b x^n)^k` with symbols in the binomial, `n >= 3`, was
answered by the Hermite reduction for `k = 2` and `k = 3` only: from the fourth power on, its
linear system grew past what it takes. Each monomial of `P` is now taken down a power at a time by
the classical recurrence, `int x^m/B^j = x^(m+1)/(a n (j - 1) B^(j-1)) - (m + 1 - n (j - 1))/(a n (j - 1)) int x^m/B^(j-1)`,
and what reaches the first power is integrated once over the binomial, at its roots of unity. The
square and the cube keep the Hermite reduction's answers. Rubi's 1.1.3.8 `P(x) (c x)^m (a + b x^n)^p`
([#718](https://github.com/asc-community/AngouriMath/issues/718)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"1/(a + b*x^4)^4".ToEntity().Integrate("x")` | `integral(...)` | three algebraic terms over powers of the binomial, and logarithms and arctangents at its roots |
| `"(c + d*x)/(a + b*x^4)^4".ToEntity().Integrate("x")` | `integral(...)` | the same |
| `"(c + d*x)/(a + b*x^3)^4".ToEntity().Integrate("x")` | `integral(...)` | the same at the roots of `a + b x^3` |
| `"x^2/(a + b*x^5)^5".ToEntity().Integrate("x")` | `integral(...)` | four algebraic terms, and the integral of `x^2/(a + b x^5)` |

### A symbolic parameter no longer stops a rational integrand being integrated

`1/(8 + x^3)` and `1/(16 - x^4)` are answered at once. `1/(a^3 + x^3)` and `1/(a^4 - x^4)` were not,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,12 @@ is var (multiple, leftover)
&& IntegrateByAnsatz(null, numerator / denominator, x) is { } byHermite)
return byHermite;

// A power of a binomial past what the Hermite reduction's system takes with symbols in
// it, `(c + d x)/(a + b x^4)^4`: the classical recurrence, a power at a time, down to
// the binomial itself.
if (IntegrateAPolynomialOverAPowerOfABinomial(numerator, denominator, x, integrateByParts) is { } downThePowers)
return downThePowers;

// Splitting into coprime blocks comes before peeling one root off, and the order is
// load-bearing rather than a preference.
//
Expand Down Expand Up @@ -858,6 +864,101 @@ is var (multiple, leftover)
return (total / b).Substitute(t, linear).InnerSimplified;
}

/// <summary>
/// A polynomial over a power of a binomial, <c>P(x)/(a + b x^n)^k</c> with <c>n &gt;= 3</c>
/// and <c>k &gt;= 2</c>, a power at a time down to <c>P'(x)/(a + b x^n)</c>, which
/// <see cref="IntegrateAPolynomialOverABinomial"/> answers at the roots of unity.
/// </summary>
/// <remarks>
/// <para>
/// For a monomial, with <c>B = a + b x^n</c>,
/// <code>
/// int x^m/B^j = x^(m+1)/(a n (j - 1) B^(j-1)) - (m + 1 - n (j - 1))/(a n (j - 1)) int x^m/B^(j-1)
/// </code>
/// since the derivative of <c>x^(m+1)/B^(j-1)</c> is <c>(m + 1) x^m/B^(j-1) - (j - 1) n b x^(m+n)/B^j</c>
/// and <c>b x^(m+n) = x^m B - a x^m</c>. Each monomial of <c>P</c> goes down the powers on
/// its own, and what reaches the first power is gathered into one polynomial over
/// <c>B</c>, integrated once.
/// </para>
/// <para>
/// After the Hermite reduction, which answers the square and the cube of a binomial with
/// symbols in it in one linear solve and keeps those answers. Its system grows with
/// <c>n k</c>: <c>1/(a + b x^4)^3</c> is answered and <c>1/(a + b x^4)^4</c> was declined.
/// The constant term is divided by, which is the generic case, as everywhere in the
/// integrator.
/// https://github.com/asc-community/AngouriMath/issues/718
/// </para>
/// </remarks>
private static Entity? IntegrateAPolynomialOverAPowerOfABinomial(Entity numerator, Entity denominator, Entity.Variable x, bool integrateByParts)
{
Entity constant = Number.Integer.One;
Entity? binomial = null;
var power = 0;
foreach (var factor in Mulf.LinearChildren(denominator))
{
if (!factor.ContainsNode(x))
constant = constant == Number.Integer.One ? factor : constant * factor;
else if (binomial is null && factor is Powf(var @base, Number.Integer { EInteger: var e }) && e.CompareTo(EInteger.FromInt32(2)) >= 0 && e.CanFitInInt32())
{
binomial = @base;
power = e.ToInt32Unchecked();
}
else
return null;
}
if (binomial is null || power > MaximumPowerOfABinomial
|| !TreeAnalyzer.TryGetPolynomial(binomial, x, out var below) || below.Count != 2
|| !below.TryGetValue(EInteger.Zero, out var a) || IsZeroAsAValue(a)
|| below.Keys.Max() is not { } top || top.CompareTo(EInteger.FromInt32(3)) < 0 || !top.CanFitInInt32()
|| IsZeroAsAValue(below[top])
|| !TreeAnalyzer.TryGetPolynomial(numerator, x, out var above)
|| above.Keys.Any(degree => degree.Sign < 0 || !degree.CanFitInInt32())
|| above.Values.Any(coefficient => coefficient.ContainsNode(x)))
return null;
var n = top.ToInt32Unchecked();

// Each monomial down the powers: the algebraic part over B^(j - 1) gathered by j, and
// what is left at the first power gathered by the monomial's degree.
var overPowers = new Dictionary<int, Entity>();
var atTheFirst = new Dictionary<int, Entity>();
foreach (var pair in above)
{
var m = pair.Key.ToInt32Unchecked();
var coefficient = pair.Value;
for (var j = power; j >= 2 && coefficient != Number.Integer.Zero; j--)
{
var over = a * n * (j - 1);
var term = Functions.PartialFractions.InLowestTermsOverTheSymbols(coefficient / over) * MathS.Pow(x, m + 1);
overPowers[j - 1] = overPowers.TryGetValue(j - 1, out var so) ? so + term : term;
coefficient = Functions.PartialFractions.InLowestTermsOverTheSymbols(-coefficient * (m + 1 - n * (j - 1)) / over);
}
if (coefficient != Number.Integer.Zero)
atTheFirst[m] = atTheFirst.TryGetValue(m, out var so) ? so + coefficient : coefficient;
}

Entity answer = Number.Integer.Zero;
foreach (var pair in overPowers.OrderByDescending(pair => pair.Key))
answer = answer + pair.Value / (pair.Key == 1 ? binomial : MathS.Pow(binomial, pair.Key));
if (atTheFirst.Count > 0)
{
Entity rest = Number.Integer.Zero;
foreach (var pair in atTheFirst.OrderBy(pair => pair.Key))
rest = rest + pair.Value * (pair.Key == 0 ? Number.Integer.One : pair.Key == 1 ? x : MathS.Pow(x, pair.Key));
if (Integration.ComputeIndefiniteIntegral(rest / binomial, x, integrateByParts) is not { } overTheBinomial)
return null;
answer = answer + overTheBinomial;
}
return (constant == Number.Integer.One ? answer : answer / constant).InnerSimplified;

static bool IsZeroAsAValue(Entity e) => e.Evaled is Number.Complex { IsZero: true };
}

/// <summary>
/// The largest power of a binomial <see cref="IntegrateAPolynomialOverAPowerOfABinomial"/>
/// takes down, one algebraic term a monomial a power.
/// </summary>
private const int MaximumPowerOfABinomial = 12;

/// <summary>
/// A polynomial over a binomial <c>a x^n + b</c>, <c>n >= 3</c>, decomposed at the
/// <c>n</c>-th roots of <c>-b/a</c> and integrated term by term in closed form.
Expand Down
70 changes: 70 additions & 0 deletions Sources/Tests/UnitTests/Calculus/PowerOfABinomialIntegralTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// 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 polynomial over a power of a binomial <c>a + b x^n</c>, <c>n &gt;= 3</c>, with symbols in
/// it, taken down a power at a time by the classical recurrence to the binomial itself.
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
/// </summary>
/// <remarks>
/// Rubi's 1.1.3.8 <c>(c + d x)/(a + b x^4)^4</c> and the like: the Hermite reduction answers
/// the square and the cube of the binomial, and its system grew past what it takes from the
/// fourth power on. Differentiated back with <c>a</c> of either sign, since the roots the
/// last step writes are complex for one of them.
/// </remarks>
[Trait("Area", "Calculus")]
public sealed class PowerOfABinomialIntegralTest
{
private static readonly double[] Points = { 0.3, 0.9, 1.7, 2.6 };

private static void DifferentiatesBack(string integrand, params (string, double)[] pins)
{
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 pins)
{
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 >= 3, $"only {compared} points could be compared for {integrand}");
}

/// <summary>The fourth power and past it, of binomials of the third, fourth and fifth degree.</summary>
[Theory]
[InlineData("1/(a + b*x^4)^4", 1.3)]
[InlineData("(c + d*x)/(a + b*x^4)^4", 1.3)]
[InlineData("(c + d*x)/(a + b*x^3)^4", 1.3)]
[InlineData("(c + d*x + e*x^2 + f*x^3 + g*x^4 + h*x^5 + k*x^6)/(a + b*x^4)^4", 1.3)]
[InlineData("x^2/(a + b*x^5)^5", 1.3)]
[InlineData("(c + d*x)/(a + b*x^4)^4", -1.3)]
[InlineData("(c + d*x)/(a + b*x^3)^4", -1.3)]
public void PastTheCube(string integrand, double a)
=> DifferentiatesBack(integrand, ("a", a), ("b", 0.7), ("c", 0.4), ("d", 1.1), ("e", 0.6),
("f", 1.7), ("g", 0.9), ("h", 2.1), ("k", 0.3));
}
}
Loading