diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 70dbb7b71..555e0b2a7 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -204,6 +204,8 @@ only where its factors were linear or quadratic, so `1/(x (x^3 + c))` was declin - a power of `x` beside a block `B` whose constant term is not zero is split off: the first `k` terms of the power series of `N/B` at 0 are the part over `x^k`, and the rest is a polynomial over `B` exactly; +- a power of another linear `g + h x` beside such a block is split the same way at the linear's + root, where the coefficients of `N` and `B` are read by Taylor's formula; - blocks that are each a polynomial in one power `x^n` are split in `u = x^n`, where they are linear or quadratic, for `n >= 3`, or `n = 2` with two blocks or more; one block quadratic in `u` is two at its roots, as `a + b x^2 + c x^4` already was; @@ -219,6 +221,8 @@ Rubi's `x^m (a + b x^n)^p` and `(a + b x^n)^p (c + d x^n)^q` | `"1/(x*(x^3 + c))".Integrate("x")` | left unevaluated | `ln(x)/c - ln(c x^3 + c^2)/(3 c)` | | `"1/(x^4*(a + b*x^3))".Integrate("x")` | left unevaluated | `-1/(3 a x^3) - b ln(x)/a^2 + b ln(a^2 b x^3 + a^3)/(3 a^2)` | | `"1/(x*(a + b*x^5))".Integrate("x")` | left unevaluated | `ln(x)/a - ln(a b x^5 + a^2)/(5 a)` | +| `"1/((x - c)*(a + b*x^3))".Integrate("x")` | left unevaluated | `ln(x - c)/(a + b c^3)`, and logarithms and an arctangent at the binomial's roots | +| `"1/((g + h*x)*(a + b*x^3))".Integrate("x")` | left unevaluated | `h^3 ln(g + h x)/(a h^3 - b g^3)`, and the same | | `"1/(x^2*(x^3 + c))".Integrate("x")`, and `(c + d x + e x^2)/(x^2 (a + b x^3))` | left unevaluated | a logarithm and an arctangent in `c^(1/3)`, beside `-1/(c x)` | | `"1/((a+b*x^3)*(c+d*x^3))".Integrate("x")`, and `x/((a + b x^3)(c + d x^3))` | left unevaluated | logarithms and arctangents in `(-a/b)^(1/3)` and `(-c/d)^(1/3)`, over `a d - b c` | | `"1/((a+b*x^4)*(c+d*x^4))".Integrate("x")` | left unevaluated | arctangents and logarithms of each binomial's roots, over `a d - b c` | diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 63cf404d1..544223092 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -830,6 +830,10 @@ is var (multiple, leftover) // the second degree. Split at the power of x, each part goes to a rule that reads it. if (IntegrateOverAPowerOfXBesideABlock(numerator, denominator, x, integrateByParts) is { } besideABlock) return besideABlock; + // And a power of another linear beside such a block, split at its root the same way: + // `1/((u - c)(a + b u^3))`, which writing `1/(x (a + b (c + d x)^3))` in `c + d x` makes. + if (IntegrateOverAPowerOfALinearBesideABlock(numerator, denominator, x, integrateByParts) is { } besideABlockAtARoot) + return besideABlockAtARoot; // Blocks that are each a polynomial in one power of x past the second, // `1/((a + b x^3)(c + d x^3))`: split in `u = x^n`, where they are linear or quadratic, @@ -993,6 +997,155 @@ Entity Coefficient(Dictionary polynomial, int power) : null; } + /// + /// N/(L^k B) for a linear L = g + h x other than x, beside a block B + /// with a symbol in it, past the second degree, that does not vanish at the root of L: + /// split there as splits at 0. With + /// s = x - r, r = -g/h, N/B has a power series in s, and its first + /// k terms P are the part over s^k: N/(s^k B) = P/s^k + R/B, with + /// R = (N - P B)/s^k exactly. + /// + /// + /// 1/(x (a + b (c + d x)^3)) is 1/((u - c)(a + b u^3)) in u = c + d x: a + /// linear beside a binomial, which nothing took apart, since the split over written + /// factors takes linear and quadratic blocks only. The coefficients of N and B at the + /// root are read by Taylor's formula, and the block is left as it is written, for the + /// rules that read it. Rubi's 1.3.1. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + private static Entity? IntegrateOverAPowerOfALinearBesideABlock(Entity numerator, Entity denominator, Entity.Variable x, bool integrateByParts) + { + // For the question asked, or one substitution below it: the rest over the block is + // integrated again. + if (!Integration.AnsweringTheQuestionAskedOrOneBelow) + return null; + Entity? linear = null; + var k = 0; + Entity block = Number.Integer.One; + var pastTheSecondDegree = false; + foreach (var factor in Mulf.LinearChildren(denominator)) + { + var (@base, power) = factor is Powf(var raised, Number.Integer e) && e.EInteger.Sign > 0 && e.EInteger.CanFitInInt32() + ? (raised, e.EInteger.ToInt32Unchecked()) + : (factor, 1); + if (@base != x && @base.ContainsNode(x) && TreeAnalyzer.TryGetPolyLinear(@base, x, out var slope, out var offset) + && !slope.ContainsNode(x) && !offset.ContainsNode(x)) + { + if (linear is { } && linear != @base) + return null; + linear = @base; + k += power; + } + else + { + block = block == Number.Integer.One ? factor : block * factor; + pastTheSecondDegree |= TreeAnalyzer.TryGetPolynomial(@base, x, out var terms) && terms.Keys.Any(degree => degree.CompareTo(EInteger.FromInt32(2)) > 0); + } + } + if (linear is null || k > MaximumPowerOfXBesideABlock || !pastTheSecondDegree || !block.ContainsNode(x) || !block.Vars.Any(symbol => symbol != x) + || !TreeAnalyzer.TryGetPolyLinear(linear, x, out var h, out var g) || VanishesIdentically(h) + || !TreeAnalyzer.TryGetPolynomial(block, x, out var below) || !TreeAnalyzer.TryGetPolynomial(numerator, x, out var above)) + return null; + foreach (var term in below.Concat(above)) + if (term.Key.Sign < 0 || !term.Key.CanFitInInt32() || term.Value.ContainsNode(x)) + return null; + var degreeBelow = below.Keys.Max()!.ToInt32Unchecked(); + var degreeAbove = above.Count == 0 ? 0 : above.Keys.Max()!.ToInt32Unchecked(); + if (degreeBelow > MaximumPowerOfXBesideABlock || degreeAbove > MaximumPowerOfXBesideABlock) + return null; + var root = Functions.PartialFractions.InLowestTermsOverTheSymbols(-g / h); + // N and B in s = x - r, by Taylor's formula. + Entity[] InS(Dictionary polynomial, int degree) + { + var shifted = new Entity[degree + 1]; + for (var i = 0; i <= degree; i++) + { + Entity sum = Number.Integer.Zero; + for (var m = i; m <= degree; m++) + if (polynomial.TryGetValue(EInteger.FromInt32(m), out var coefficient)) + sum += coefficient * Binomial(m, i) * (m == i ? Number.Integer.One : MathS.Pow(root, m - i)); + shifted[i] = Functions.PartialFractions.InLowestTermsOverTheSymbols(sum); + } + return shifted; + } + // Where s divides B, the block shares the root. + var b = InS(below, degreeBelow); + if (VanishesIdentically(b[0])) + return null; + // A block that is a polynomial in a power of the linear, `a + b (c + d x)^3`, makes the + // whole a function of the linear, which the substitution of it reads. Split here, the + // rest goes back over that block written in x, which the rules over x search long for: + // 20 s for `1/((c + d x)^2 (a + b (c + d x)^3)^2)`. + static int Gcd(int p, int q) => q == 0 ? p : Gcd(q, p % q); + var stride = 0; + for (var i = 1; i < b.Length; i++) + if (!VanishesIdentically(b[i])) + stride = Gcd(stride, i); + if (stride > 1) + return null; + // Where s divides N, the part over s^k is nothing and the rest is the question asked + // again. + var n = InS(above, degreeAbove); + if (VanishesIdentically(n[0])) + return null; + Entity At(Entity[] polynomial, int power) => power < polynomial.Length ? polynomial[power] : Number.Integer.Zero; + + // P: the first k terms of the power series of N/B at r. + var series = new Entity[k]; + for (var j = 0; j < k; j++) + { + var sum = At(n, j); + for (var i = 1; i <= j; i++) + sum -= At(b, i) * series[j - i]; + series[j] = Functions.PartialFractions.InLowestTermsOverTheSymbols(sum / b[0]); + } + + // R = (N - P B)/s^k in s, and then in x. + var top = System.Math.Max(degreeAbove, degreeBelow + k - 1); + var inS = new Entity[System.Math.Max(top - k + 1, 0)]; + for (var m = k; m <= top; m++) + { + var coefficient = At(n, m); + for (var j = System.Math.Max(0, m - degreeBelow); j <= System.Math.Min(k - 1, m); j++) + coefficient -= series[j] * At(b, m - j); + inS[m - k] = Functions.PartialFractions.InLowestTermsOverTheSymbols(coefficient); + } + Entity remainder = Number.Integer.Zero; + for (var j = 0; j < inS.Length; j++) + { + Entity sum = Number.Integer.Zero; + for (var m = j; m < inS.Length; m++) + if (!VanishesIdentically(inS[m])) + sum += inS[m] * Binomial(m, j) * (m == j ? Number.Integer.One : MathS.Pow(-root, m - j)); + var coefficient = Functions.PartialFractions.InLowestTermsOverTheSymbols(sum); + if (!VanishesIdentically(coefficient)) + remainder += j == 0 ? coefficient : coefficient * (j == 1 ? x : MathS.Pow(x, j)); + } + + // L^k is h^k s^k, and s is L/h: the part over L^k term by term, and the rest over the + // block as it is written. + Entity overTheLinear = Number.Integer.Zero; + for (var j = 0; j < k; j++) + if (!VanishesIdentically(series[j])) + overTheLinear += j == k - 1 + ? series[j] * IntegralPatterns.AntiderivativeLog(linear) + : series[j] * MathS.Pow(h, k - 1 - j) * MathS.Pow(linear, j - k + 1) / (j - k + 1); + var scale = k == 1 ? h : MathS.Pow(h, k); + if (remainder == Number.Integer.Zero) + return overTheLinear / scale; + return Integration.ComputeIndefiniteIntegral(remainder / block, x, integrateByParts) is { } overTheBlock + ? (overTheLinear + overTheBlock) / scale + : null; + + static Entity Binomial(int n, int k) + { + var result = EInteger.One; + for (var i = 1; i <= k; i++) + result = result.Multiply(EInteger.FromInt32(n - k + i)).Divide(EInteger.FromInt32(i)); + return Number.Integer.Create(result); + } + } + /// /// N/(B_1 ... B_m) where every block B_i is a polynomial in x^n for one /// n >= 2, linear or quadratic in it, with a symbol among them: split in diff --git a/Sources/Tests/UnitTests/Calculus/SymbolicFactorsPartialFractionsTest.cs b/Sources/Tests/UnitTests/Calculus/SymbolicFactorsPartialFractionsTest.cs index bad185d9a..1cd1e8316 100644 --- a/Sources/Tests/UnitTests/Calculus/SymbolicFactorsPartialFractionsTest.cs +++ b/Sources/Tests/UnitTests/Calculus/SymbolicFactorsPartialFractionsTest.cs @@ -135,6 +135,18 @@ public void TheNumericSpellingsStillAnswer(string integrand) public void APowerOfXBesideASymbolicBlock(string integrand) => DifferentiatesBack(integrand, "x", ("a", 1.7), ("b", 2.3), ("c", 0.4), ("d", 1.1), ("e", 0.6)); + /// + /// And a power of another linear beside such a block, split at its root: + /// 1/((x - c)(a + b x^3)) was declined, and so was the same over g + h x. + /// + [Theory] + [InlineData("1/((x - c)*(a + b*x^3))")] + [InlineData("1/((x - c)^2*(a + b*x^4))")] + [InlineData("(1 + x)/((x + 2)^2*(a + b*x^3))")] + [InlineData("1/((g + h*x)*(a + b*x^3))")] + public void APowerOfALinearBesideASymbolicBlock(string integrand) + => DifferentiatesBack(integrand, "x", ("a", 1.7), ("b", 2.3), ("c", 0.4), ("g", 1.1), ("h", 0.6)); + /// /// Blocks that are each a polynomial in one power of x past the second, split in /// u = x^n, where they are linear: 1/((a + b x^3)(c + d x^3)) was declined.