From 982d61a4c386486a3da77302d80357cc09a75d42 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 2 Oct 2026 14:21:04 +0000 Subject: [PATCH] A polynomial over a power of a binomial past the cube is taken down a power at a time P(x)/(a + b x^n)^k with symbols in the binomial, n >= 3, was answered by the Hermite reduction for the square and the cube only: from the fourth power its linear system grew past what it takes, and 1/(a + b x^4)^4 was declined. After it, each monomial of P now goes down the powers 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. Rubi's 1.1.3.8. Part of #718. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 17 +++ .../Integration/IndefiniteIntegralSolver.cs | 101 ++++++++++++++++++ .../Calculus/PowerOfABinomialIntegralTest.cs | 70 ++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 Sources/Tests/UnitTests/Calculus/PowerOfABinomialIntegralTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 8564b93e5..e1c7a9827 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -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, diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index a801299a2..1845af591 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -723,6 +723,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. // @@ -845,6 +851,101 @@ is var (multiple, leftover) return (total / b).Substitute(t, linear).InnerSimplified; } + /// + /// A polynomial over a power of a binomial, P(x)/(a + b x^n)^k with n >= 3 + /// and k >= 2, a power at a time down to P'(x)/(a + b x^n), which + /// answers at the roots of unity. + /// + /// + /// + /// For a monomial, with B = a + b x^n, + /// + /// 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) + /// + /// since the derivative of x^(m+1)/B^(j-1) is (m + 1) x^m/B^(j-1) - (j - 1) n b x^(m+n)/B^j + /// and b x^(m+n) = x^m B - a x^m. Each monomial of P goes down the powers on + /// its own, and what reaches the first power is gathered into one polynomial over + /// B, integrated once. + /// + /// + /// 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 + /// n k: 1/(a + b x^4)^3 is answered and 1/(a + b x^4)^4 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 + /// + /// + 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(); + var atTheFirst = new Dictionary(); + 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 }; + } + + /// + /// The largest power of a binomial + /// takes down, one algebraic term a monomial a power. + /// + private const int MaximumPowerOfABinomial = 12; + /// /// A polynomial over a binomial a x^n + b, n >= 3, decomposed at the /// n-th roots of -b/a and integrated term by term in closed form. diff --git a/Sources/Tests/UnitTests/Calculus/PowerOfABinomialIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/PowerOfABinomialIntegralTest.cs new file mode 100644 index 000000000..03f4abd52 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/PowerOfABinomialIntegralTest.cs @@ -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 +{ + /// + /// A polynomial over a power of a binomial a + b x^n, n >= 3, with symbols in + /// it, taken down a power at a time by the classical recurrence to the binomial itself. + /// #718 + /// + /// + /// Rubi's 1.1.3.8 (c + d x)/(a + b x^4)^4 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 a of either sign, since the roots the + /// last step writes are complex for one of them. + /// + [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}"); + } + + /// The fourth power and past it, of binomials of the third, fourth and fifth degree. + [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)); + } +}