diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 5456a356d..cb834cb78 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -146,6 +146,23 @@ the division's; with it on, the rounding is the setting's own and stays | `"(-60.5)!"` at 30 digits, downcasting off | `5.86118478907722232671451280188E-81` | `2.93059239453861116335725639905E-81` | | `"abs(75 + 316.22776601683796i)"`, downcasting off | `325` | `325.000000000000026076735749…` | +### A rational function with symbols in it beside a root of a linear is split into partial fractions first + +**Answers where there were none.** `1/(x (1 + x^2) sqrt(a + b x))` was declined, while +`1/(x sqrt(a + b x))` and `x/((1 + x^2) sqrt(a + b x))`, its two partial fractions beside the root, +were each answered: under `v = sqrt(a + b x)` it is a rational function over a sextic with symbols in +it, which nothing factors. A rational function with symbols in it and two or more factors below the bar, +beside a power that is not whole of a linear, is now split over its factors as written -- the whole part +of the power among them, so that each term stands over a root -- and integrated term by term. It is what +the tangent substitution makes of Rubi's 4.3.2.1, `cot(x)/sqrt(a + b tan(x))` +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"1/(x*(1 + x^2)*sqrt(a + b*x))".ToEntity().Integrate("x")` | `integral(...)` | a piecewise of arctangents and a logarithm in `sqrt(a + b x)` | +| `"cot(x)/sqrt(a + b*tan(x))".ToEntity().Integrate("x")` | `integral(...)` | the same in `tan(x)` | +| `"cot(x)^2/(a + b*tan(x))^(3/2)".ToEntity().Integrate("x")` | `integral(...)` | the same, beside a power of `a + b tan(x)` | + ### A quotient of polynomials is split into coprime blocks before a root is peeled off `SolveByPartialFractions` tried `TrySplitOffRationalRoot` before `TrySplitIntoCoprimeParts`. Both diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index a56faed4a..1ecefe349 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -19281,6 +19281,98 @@ bool IsAConstantTimesAPowerOfOneMinusUSquared(Entity polynomial, out Entity cons return Integration.ComputeIndefiniteIntegral(written, x, integrateByParts); } + /// + /// A rational function with symbols in it beside a power that is not whole of a linear, + /// R(x) (a + b x)^q, with two or more factors below the bar: split into partial + /// fractions over the factors as written, each term beside the power is the question + /// the rules for one factor answer. 1/(x (1 + x^2) sqrt(a + b x)) is + /// 1/(x sqrt(a + b x)) - x/((1 + x^2) sqrt(a + b x)), and was declined: under + /// v = sqrt(a + b x) it is a rational function over a sextic with symbols in it, + /// which nothing factors, where each term above is answered at once. It is what the + /// tangent substitution makes of Rubi's 4.3.2.1, cot(x)/sqrt(a + b tan(x)). + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + /// + /// Late, so that whatever answers the integrand whole answers first, and only with a symbol + /// in it: with numbers the substitution's rational function is split over the rationals. + /// + internal static Entity? SolveARationalFunctionBesideARootOfALinearSplitFirst(Entity expr, Entity.Variable x, bool integrateByParts) + { + if (!expr.Vars.Any(symbol => symbol != x)) + return null; + Entity? radicand = null; + var exponent = ERational.Zero; + Entity constant = Number.Integer.One; + Entity above = Number.Integer.One; + Entity below = Number.Integer.One; + var belowFactors = 0; + foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr)) + { + if (!factor.ContainsNode(x)) + { + constant = underneath ? constant / factor : constant * factor; + continue; + } + if (factor is Powf(var linear, Number.Rational power) && power is not Number.Integer + && TreeAnalyzer.TryGetPolyLinear(linear, x, out var slope, out _) && !TreeAnalyzer.IsZero(slope)) + { + if (radicand is not null) + return null; + (radicand, exponent) = (linear, underneath ? power.ERational.Negate() : power.ERational); + continue; + } + // A polynomial, or a whole power of one. + var (@base, whole) = factor is Powf(var raised, Number.Integer n) && n.EInteger.CanFitInInt32() ? (raised, n.EInteger.ToInt32Checked()) : (factor, 1); + if (!TreeAnalyzer.TryGetPolynomial(@base, x, out var read) || read.Keys.Any(degree => degree.Sign < 0) || read.Values.Any(coefficient => coefficient.ContainsNode(x))) + return null; + var written = System.Math.Abs(whole) == 1 ? @base : MathS.Pow(@base, System.Math.Abs(whole)); + if (underneath == whole > 0) + { + below = below == Number.Integer.One ? written : below * written; + belowFactors++; + } + else + above = above == Number.Integer.One ? written : above * written; + } + if (radicand is null) + return null; + // The whole part of the power goes into the rational function, and what is left of it + // is a root below the bar, `L^q = L^k / L^(k - q)` with `k` the least whole number past + // `q`: `1/(x (1 + x^2) (a + b x)^(3/2))` is split with `a + b x` among the factors and + // every term over `sqrt(a + b x)`, the shape the rules for one factor read. + var (top, bottom) = (exponent.Numerator, exponent.Denominator); + var wholePart = top.Sign > 0 ? top.Add(bottom).Subtract(EInteger.One).Divide(bottom) : top.Divide(bottom); + if (wholePart.Sign > 0) + above = above == Number.Integer.One ? MathS.Pow(radicand, Number.Integer.Create(wholePart)) : above * MathS.Pow(radicand, Number.Integer.Create(wholePart)); + else if (wholePart.Sign < 0) + { + var negated = wholePart.Negate(); + var power = negated.Equals(EInteger.One) ? radicand : MathS.Pow(radicand, Number.Integer.Create(negated)); + below = below == Number.Integer.One ? power : below * power; + belowFactors++; + } + var root = MathS.Pow(radicand, Number.Rational.Create(ERational.FromEInteger(wholePart).Subtract(exponent))); + if (belowFactors < 2 + || !Functions.PartialFractions.TrySplitOverWrittenFactors(above, below, x, out var decomposition)) + return null; + // The split comes over the denominator's constant, which goes in front. + if (decomposition is Divf(var split, var over) && !over.ContainsNode(x)) + (decomposition, constant) = (split, constant / over); + var terms = Sumf.LinearChildren(decomposition).Where(term => !TreeAnalyzer.IsZero(term)).ToList(); + if (terms.Count < 2) + return null; + Entity sum = Number.Integer.Zero; + foreach (var term in terms) + { + var question = term / root; + if (Integration.ComputeIndefiniteIntegral(question, x, integrateByParts) is not { } integral + || integral.Nodes.Any(node => node is Integralf)) + return null; + sum += integral; + } + return constant * sum; + } + /// /// A rational function over one block quadratic in a power of x, a + b x^n + c x^(2n) /// with a symbol in it, split at the block's roots in u = x^n: the single block diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index eb7ce8a5f..d2700310f 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -1107,6 +1107,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => // One block quadratic in a power of x, split at its roots, which are complex where its // discriminant is negative: as late, so that a rule answering the block whole goes first. if ((answer = IndefiniteIntegralSolver.SolveByOneBlockInAPowerOfXAtItsRoots(expr, x, integrateByParts)) is { }) return answer; + // A rational function with symbols in it beside a root of a linear, split into partial + // fractions first, each term the question of one factor beside the root. + if ((answer = IndefiniteIntegralSolver.SolveARationalFunctionBesideARootOfALinearSplitFirst(expr, x, integrateByParts)) is { }) return answer; // The sign of a real-valued factor is constant between its zeros, and goes in // front of the antiderivative of the rest. After every rule that reads the sign // where it stands: `cos(x) sgn(sin(x))` is `|sin(x)|` by the substitution, and diff --git a/Sources/Tests/UnitTests/Calculus/SplitBesideARootIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/SplitBesideARootIntegralTest.cs new file mode 100644 index 000000000..5b6984da7 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/SplitBesideARootIntegralTest.cs @@ -0,0 +1,54 @@ +// +// 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 rational function with symbols in it beside a root of a linear, split into partial + /// fractions first: 1/(x (1 + x^2) sqrt(a + b x)) is + /// 1/(x sqrt(a + b x)) - x/((1 + x^2) sqrt(a + b x)), each answered alone, and was + /// declined; so was what the tangent substitution makes of Rubi's 4.3.2.1, + /// cot(x)/sqrt(a + b tan(x)). + /// #718 + /// + [Trait("Area", "Calculus")] + public sealed class SplitBesideARootIntegralTest + { + [Theory] + [InlineData("1/(x*(1 + x^2)*sqrt(a + b*x))")] + [InlineData("1/(x*(1 + x^2)*(a + b*x)^(3/2))")] + [InlineData("1/((x + c)*(x + d)*sqrt(a + b*x))")] + [InlineData("cot(x)/sqrt(a + b*tan(x))")] + [InlineData("cot(x)^2/(a + b*tan(x))^(3/2)")] + public void TermByTerm(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("integral(", integral.Stringize()); + Assert.DoesNotContain("NaN", integral.Stringize()); + Entity Pinned(Entity e) => e.Substitute("a", 2.3).Substitute("b", 0.7).Substitute("c", 1.3).Substitute("d", 0.6); + var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x"); + var original = Pinned(integrand.ToEntity()); + var compared = 0; + foreach (var at in new[] { -1.7, -0.9, 0.3, 0.8, 1.6, 2.9 }) + { + var want = original.Substitute("x", at).EvalNumerical(); + if (want.IsNaN || Math.Abs((double)want.ImaginaryPart) > 1e-12) + continue; + compared++; + var got = derivative.Substitute("x", at).EvalNumerical(); + Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart) + < 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)), + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); + } + Assert.True(compared >= 5, $"only {compared} points could be compared for {integrand}"); + } + } +}