diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index d036a8063..be47d7a74 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -276,6 +276,21 @@ square and the cube keep the Hermite reduction's answers. Rubi's 1.1.3.8 `P(x) ( | `"(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 polynomial over a power of a trinomial past the square is integrated + +`P(x)/(a + b x^n + c x^(2n))^k`, a polynomial over a power of a trinomial in `x^n` with symbols in +it, was left unevaluated past the square, which the Hermite reduction answers in one linear solve. +Now each monomial is taken down a power at a time by the trinomial's recurrence, two monomials a +power up each step, and what reaches the first power is integrated once over the trinomial. Rubi's +1.2.2.2 and 1.2.2.4 have these by the dozen +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"x^4/(a + b*x^2 + c*x^4)^3".ToEntity().Integrate("x")` | `integral(...)` | odd polynomials over `(a + b x^2 + c x^4)^2` and over it, and arctangents at its roots | +| `"1/(a + b*x^2 + c*x^4)^3".ToEntity().Integrate("x")` | `integral(...)` | the same | +| `"x/(a + b*x^3 + c*x^6)^2".ToEntity().Integrate("x")` | `integral(...)` | a polynomial over `a + b x^3 + c x^6`, and the integral of a polynomial over it | + ### 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 261c1f821..19c7d9d5c 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -760,6 +760,11 @@ is var (multiple, leftover) if (IntegrateAPolynomialOverAPowerOfABinomial(numerator, denominator, x, integrateByParts) is { } downThePowers) return downThePowers; + // And of a trinomial in x^n, `x^4/(a + b x^2 + c x^4)^3`: its recurrence, a power at a + // time, down to the trinomial itself. + if (IntegrateAPolynomialOverAPowerOfATrinomial(numerator, denominator, x, integrateByParts) is { } downTheTrinomialsPowers) + return downTheTrinomialsPowers; + // Splitting into coprime blocks comes before peeling one root off, and the order is // load-bearing rather than a preference. // @@ -1219,6 +1224,119 @@ Entity InU(Dictionary polynomial) /// private const int MaximumPowerOfABinomial = 12; + /// + /// A polynomial over a power of a trinomial in x^n, + /// P(x)/(a + b x^n + c x^(2n))^k with n >= 2 and k >= 2, a power at + /// a time down to a polynomial over the trinomial itself. + /// + /// + /// + /// For a monomial, with w = x^n, T = a + b w + c w^2 and D = 4 a c - b^2, + /// the derivative of x^(m+1) (beta0 + beta1 w) T^(p+1) is + /// x^m T^p + delta0 x^m T^(p+1) + delta1 x^(m+n) T^(p+1), where + /// + /// beta0 = (b^2 - 2 a c)/(a n (p + 1) D), beta1 = b c/(a n (p + 1) D), + /// delta0 = -((2 a c - b^2)(m + 1) + n (p + 1) D)/(a n (p + 1) D), + /// delta1 = b c (m + 2 n p + 3 n + 1)/(a n (p + 1) D), + /// + /// which is what matching the powers of w in + /// (m + 1)(beta0 + beta1 w) T + n beta1 w T + (p + 1) n w (b + 2 c w)(beta0 + beta1 w) + /// against 1 + (delta0 + delta1 w) T gives. So int x^m T^p is that algebraic + /// term, less delta0 int x^m T^(p+1) and delta1 int x^(m+n) T^(p+1): two + /// monomials a power up. What reaches the first power is gathered into one polynomial + /// over T and integrated once. + /// + /// + /// After the Hermite reduction, which answers the square with symbols in it in one linear + /// solve and keeps those answers; its system grows with the degree, and + /// x^4/(a + b x^2 + c x^4)^3 was declined. Rubi's 1.2.2.2 and 1.2.2.4 have these by the + /// dozen. a and D are divided by, which is the generic case, as everywhere in + /// the integrator: D = 0 is a square, which the rules for one take. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + /// + private static Entity? IntegrateAPolynomialOverAPowerOfATrinomial(Entity numerator, Entity denominator, Entity.Variable x, bool integrateByParts) + { + Entity constant = Number.Integer.One; + Entity? trinomial = 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 (trinomial is null && factor is Powf(var @base, Number.Integer { EInteger: var e }) && e.CompareTo(EInteger.FromInt32(2)) >= 0 && e.CanFitInInt32()) + { + trinomial = @base; + power = e.ToInt32Unchecked(); + } + else + return null; + } + if (trinomial is null || power > MaximumPowerOfABinomial + || !TreeAnalyzer.TryGetPolynomial(trinomial, x, out var below) || below.Count != 3 + || below.Keys.Any(degree => !degree.CanFitInInt32()) || below.Values.Any(coefficient => coefficient.ContainsNode(x)) + || !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 degrees = below.Keys.Select(degree => degree.ToInt32Unchecked()).OrderBy(degree => degree).ToArray(); + var n = degrees[1]; + if (degrees[0] != 0 || n < 2 || degrees[2] != 2 * n) + return null; + var (a, b, c) = (below[EInteger.Zero], below[EInteger.FromInt32(n)], below[EInteger.FromInt32(2 * n)]); + var discriminant = 4 * a * c - b * b; + if (VanishesIdentically(a) || VanishesIdentically(c) || VanishesIdentically(b) || VanishesIdentically(discriminant)) + return null; + + // Each monomial down the powers: the algebraic part over T^(j - 1) gathered by j, and + // what is left at the first power gathered by the monomial's degree. + var overPowers = new Dictionary(); + var atThisPower = new Dictionary(); + foreach (var pair in above) + atThisPower[pair.Key.ToInt32Unchecked()] = pair.Value; + for (var j = power; j >= 2; j--) + { + var p = -j; + var over = a * n * (p + 1) * discriminant; + var beta0 = Functions.PartialFractions.InLowestTermsOverTheSymbols((b * b - 2 * a * c) / over); + var beta1 = Functions.PartialFractions.InLowestTermsOverTheSymbols(b * c / over); + var atTheNext = new Dictionary(); + foreach (var pair in atThisPower) + { + var (m, coefficient) = (pair.Key, pair.Value); + if (VanishesIdentically(coefficient)) + continue; + var term = Functions.PartialFractions.InLowestTermsOverTheSymbols(coefficient * beta0) * MathS.Pow(x, m + 1) + + Functions.PartialFractions.InLowestTermsOverTheSymbols(coefficient * beta1) * MathS.Pow(x, m + 1 + n); + overPowers[j - 1] = overPowers.TryGetValue(j - 1, out var so) ? so + term : term; + var delta0 = -((2 * a * c - b * b) * (m + 1) + n * (p + 1) * discriminant) / over; + var delta1 = b * c * (m + 2 * n * p + 3 * n + 1) / over; + foreach (var (degree, times) in new[] { (m, delta0), (m + n, delta1) }) + { + var next = Functions.PartialFractions.InLowestTermsOverTheSymbols(-coefficient * times); + atTheNext[degree] = atTheNext.TryGetValue(degree, out var already) + ? Functions.PartialFractions.InLowestTermsOverTheSymbols(already + next) : next; + } + } + atThisPower = atTheNext; + } + + Entity answer = Number.Integer.Zero; + foreach (var pair in overPowers.OrderByDescending(pair => pair.Key)) + answer = answer + pair.Value / (pair.Key == 1 ? trinomial : MathS.Pow(trinomial, pair.Key)); + Entity rest = Number.Integer.Zero; + foreach (var pair in atThisPower.OrderBy(pair => pair.Key)) + if (!VanishesIdentically(pair.Value)) + rest = rest + pair.Value * (pair.Key == 0 ? Number.Integer.One : pair.Key == 1 ? x : MathS.Pow(x, pair.Key)); + if (rest != Number.Integer.Zero) + { + if (Integration.ComputeIndefiniteIntegral(rest / trinomial, x, integrateByParts) is not { } overTheTrinomial) + return null; + answer = answer + overTheTrinomial; + } + return (constant == Number.Integer.One ? answer : answer / constant).InnerSimplified; + } + /// /// 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/PowerOfATrinomialIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/PowerOfATrinomialIntegralTest.cs new file mode 100644 index 000000000..9ca038f51 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/PowerOfATrinomialIntegralTest.cs @@ -0,0 +1,67 @@ +// +// 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 trinomial a + b x^n + c x^(2n) with symbols in it, + /// taken down a power at a time by its recurrence to the trinomial itself. + /// #718 + /// + /// + /// Rubi's 1.2.2.2 x^4/(a + b x^2 + c x^4)^3 and the like: the Hermite reduction answers + /// the square, and its system grew past what it takes at the cube. Differentiated back on + /// both sides of 4 a c - b^2, since the roots the last step writes are complex for one. + /// + [Trait("Area", "Calculus")] + public sealed class PowerOfATrinomialIntegralTest + { + 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 cube of a trinomial in x^2, and the square of one in x^3. + [Theory] + [InlineData("x^4/(a + b*x^2 + c*x^4)^3", 0.7)] + [InlineData("1/(a + b*x^2 + c*x^4)^3", 0.7)] + [InlineData("(d + k*x^2)/(a + b*x^2 + c*x^4)^3", 0.7)] + [InlineData("x/(a + b*x^3 + c*x^6)^2", 0.7)] + [InlineData("x^4/(a + b*x^2 + c*x^4)^3", 3.1)] + [InlineData("(d + k*x^2)/(a + b*x^2 + c*x^4)^3", 3.1)] + public void PastTheSquare(string integrand, double b) + => DifferentiatesBack(integrand, ("a", 1.3), ("b", b), ("c", 0.9), ("d", 0.4), ("k", 1.1)); + } +}