diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index c8d388420..d3d2ae92b 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -1463,6 +1463,26 @@ positive. The shift `h` is simplified where it holds a symbol: `2 a b/(2 b^2)` i Rubi's 1.2.2.2, 1.2.2.4, 1.2.2.7 and 1.2.3.2, the 574 problems that count with a perfect square written with symbols: 182 to 388, no row lost, 19 timeouts to 9. +### A linear below the bar that divides the radicand is written over it + +`x^2/((d + e x) sqrt(d^2 - e^2 x^2))` was left unevaluated. The linear divides the radicand, +`d^2 - e^2 x^2 = (d + e x)(d - e x)`, so `1/(d + e x)` is `(d - e x)/(d^2 - e^2 x^2)`, and the +integrand is the polynomial `x^2 (d - e x)` over `(d^2 - e^2 x^2)^(3/2)`, which the rules for a +polynomial beside a half-odd power of a quadratic answer. The rules that read a linear beside a +root take it as a pole to split off, and this one has nothing to split off: its residue is the +radicand at its root, zero. A whole power `L^(-n)` below the bar, where `P = L M`, is now +`M^n P^(-n)`, exactly, for the question as asked. + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"x^2/((d+pe*x)*sqrt(d^2-pe^2*x^2))".Integrate("x")` | left unevaluated | the antiderivative | +| `"x^2*sqrt(1-a^2*x^2)/(1-a*x)^5".Integrate("x")` | left unevaluated | the antiderivative, in `x/sqrt(1 - a^2 x^2)` | +| `"sqrt(a*d*pe+(c*d^2+a*pe^2)*x+c*d*pe*x^2)/(x*(d+pe*x))".Integrate("x")` | left unevaluated | the antiderivative | + +Rubi's 1.2.1.4, all 632 problems that count: 250 to 409, no row lost, 61 timeouts to 67. The six +new timeouts have a radicand of four symbols beside a power of `x`, and went from a quick decline +to one past the budget. + ### `binomial(n, k)` is a function **Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index f2d62e9e1..f13856533 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -9250,6 +9250,130 @@ Dictionary PowerOfQ(int j) } } + /// + /// A linear below the bar that divides the radicand of a half-odd power beside it, written + /// over that radicand: the quadratic is P = L M, so 1/L is M/P and + /// L^(-n) P^s is M^n P^(s - n), exactly, for a whole n. + /// x/((d + e x) sqrt(d^2 - e^2 x^2)) is the polynomial x (d - e x) over + /// (d^2 - e^2 x^2)^(3/2), which the rules for a polynomial beside a half-odd power + /// of a quadratic answer. Rubi's 1.2.1.4 writes most of its rows this way. + /// + /// + /// The rules in front read a linear beside the root as a pole to take apart, and a linear + /// that divides the radicand leaves nothing to take apart: its residue is the radicand's + /// value at its root, zero. Only below the bar, so the rewrite never makes the shape it + /// reads -- M^n is written above -- and a whole power only, where the rewrite + /// needs no branch. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + internal static Entity? SolveByWritingALinearBelowTheBarOverTheRadicand(Entity expr, Entity.Variable x, bool integrateByParts) + { + // At the question asked only. Below it the rule rewrote what the substitution search + // asks of each of its candidates, and every rewrite was a search of its own: + // `1/(x^2 (d + e x) sqrt(a d e + (c d^2 + a e^2) x + c d e x^2))` declined in 80 ms + // before the rule, ran past ninety seconds with it at every depth, and is answered + // in half a second with it here. + if (!Integration.AnsweringTheQuestionAsked) + return null; + if (!expr.Nodes.Any(node => node is Powf(var radicand, Number.Rational { ERational: var power }) + && power.Denominator.Equals(EInteger.FromInt32(2)) && radicand.ContainsNode(x))) + return null; + var (above, below) = Functions.SingleQuotient.Of(Functions.SingleQuotient.Combine(expr)); + if (!below.ContainsNode(x)) + return null; + // The factors on both sides of the bar as powers, those below with the exponent negated. + var factors = new List<(Entity Base, Entity Exponent)>(); + foreach (var factor in Mulf.LinearChildren(above)) + factors.Add(factor is Powf(var @base, var exponent) ? (@base, exponent) : (factor, Number.Integer.One)); + foreach (var factor in Mulf.LinearChildren(below)) + factors.Add(factor is Powf(var @base, var exponent) ? (@base, (-exponent).InnerSimplified) : (factor, Number.Integer.MinusOne)); + // One half-odd power of a quadratic. + var rootAt = -1; + Entity? a = null, b = null, c = null; + for (var i = 0; i < factors.Count; i++) + if (factors[i].Exponent is Number.Rational { ERational: var power } and not Number.Integer + && power.Denominator.Equals(EInteger.FromInt32(2)) && factors[i].Base.ContainsNode(x)) + { + if (rootAt >= 0 || !TreeAnalyzer.TryGetPolyQuadratic(factors[i].Base, x, out a, out b, out c) + || a.Evaled is Number.Complex { IsZero: true }) + return null; + rootAt = i; + } + if (rootAt < 0 || a is null || b is null || c is null) + return null; + // And nothing else in the integrand a root of x: this is a rational function beside + // one root. Where the radicand's own factors stand under roots elsewhere -- + // `(1 - x^2)^(1/4)` beside `sqrt(1 - x)` and `sqrt(1 + x)`, Timofeev's 314 -- the + // integrand is the two-linear-radicals rules', and the rewrite took it down a route + // whose answer held `sgn(sqrt(1 - x))`, which nothing differentiates. + for (var k = 0; k < factors.Count; k++) + if (k != rootAt && factors[k].Base.ContainsNode(x) + && (factors[k].Exponent is not Number.Integer + || factors[k].Base.Nodes.Any(node => node is Powf(var inner, var power) && power is not Number.Integer && inner.ContainsNode(x)))) + return null; + for (var j = 0; j < factors.Count; j++) + { + if (factors[j].Exponent is not Number.Integer { IsNegative: true } negated + || !TreeAnalyzer.TryGetPolyLinear(factors[j].Base, x, out var slope, out var intercept) + || slope.Evaled is Number.Complex { IsZero: true }) + continue; + // L = l1 x + l0 divides P = a x^2 + b x + c where P vanishes at -l0/l1, which + // times l1^2 is a l0^2 - b l0 l1 + c l1^2; the other factor is then + // M = (a/l1) x + (b l1 - a l0)/l1^2. + if (!VanishesIdentically(a * intercept * intercept - b * intercept * slope + c * slope * slope)) + continue; + // Each coefficient simplified where it holds a symbol: the normalisation leaves + // `((c d^2 + a e^2) e - c d e d)/e^2` as written where it is `a e`, and the rules + // below searched forty seconds on the one spelling and answered the other at once. + var leading = (a / slope).InnerSimplified; + var constant = ((b * slope - a * intercept) / (slope * slope)).InnerSimplified; + if (leading.Vars.Any()) + leading = Functions.PartialFractions.Bare(leading.Simplify()); + if (constant.Vars.Any()) + constant = Functions.PartialFractions.Bare(constant.Simplify()); + var other = (leading * x + constant).InnerSimplified; + var times = -negated; + var (radicand, power) = factors[rootAt]; + Entity written = MathS.Pow(other, times) * MathS.Pow(radicand, (power - times).InnerSimplified); + for (var k = 0; k < factors.Count; k++) + if (k != rootAt && k != j) + written = written * MathS.Pow(factors[k].Base, factors[k].Exponent); + var rewritten = written.InnerSimplified; + if (rewritten == expr || rewritten == expr.InnerSimplified) + return null; + return Integration.ComputeAsTheSameQuestion(rewritten, x, integrateByParts); + } + return null; + } + + /// + /// Whether , free of the variable, is zero for every value of its + /// symbols: at a point first, which settles most of them without simplifying, and then + /// simplified, since leaves a^2 b^2 - a^2 b^2 + /// written as two terms. + /// + private static bool VanishesIdentically(Entity expr) + { + var inner = expr.InnerSimplified; + if (inner.Evaled is Number.Complex { IsZero: true }) + return true; + if (!inner.Vars.Any()) + return false; + var pinned = inner; + var values = new[] { 1.37, 0.61, 2.23, 1.91, 0.83, 1.13 }; + var index = 0; + foreach (var symbol in inner.Vars.ToList()) + pinned = pinned.Substitute(symbol, values[index++ % values.Length]); + try + { + if (pinned.EvalNumerical() is Number.Complex { IsNaN: false } value + && ((Number.Real)value.Abs()).EDecimal.ToDouble() > 1e-9) + return false; + } + catch (Core.Exceptions.CannotEvalException) { } + return Functions.PartialFractions.Bare(inner.Simplify()).Evaled is Number.Complex { IsZero: true }; + } + /// /// A rational function of x and one square root of a quadratic in x, /// rationalised by an Euler substitution and handed to the rational integrator. diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index b12321821..c18599886 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -818,6 +818,10 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => if ((answer = IndefiniteIntegralSolver.SolveAnExponentialTimesAnOddHalfPowerOfAQuadratic(expr, x)) is { }) return answer; if ((answer = IndefiniteIntegralSolver.SolveALinearBesideTheRootOfAQuadratic(expr, x)) is { }) return answer; if ((answer = IndefiniteIntegralSolver.SolveARationalFunctionBesideTheRootOfAQuadratic(expr, x)) is { }) return answer; + // A linear below the bar that divides the radicand, written over it: after the two + // rules above, which read a linear beside the root as a pole and find nothing to take + // apart where its residue, the radicand at its root, is zero. + if ((answer = IndefiniteIntegralSolver.SolveByWritingALinearBelowTheBarOverTheRadicand(expr, x, integrateByParts)) is { }) return answer; if ((answer = IndefiniteIntegralSolver.SolveByEulerSubstitution(expr, x)) is { }) return answer; // And a root of a palindromic quartic, which is a root of a quadratic in x -+ 1/x: // Charlwood's `(1 + x^2)/((1 - x^2) sqrt(1 + x^4))` is `-du/(u sqrt(u^2 + 2))`. diff --git a/Sources/Tests/UnitTests/Calculus/RationalFunctionBesideTheRootOfAQuadraticTest.cs b/Sources/Tests/UnitTests/Calculus/RationalFunctionBesideTheRootOfAQuadraticTest.cs index 2b37fabfd..2f847349f 100644 --- a/Sources/Tests/UnitTests/Calculus/RationalFunctionBesideTheRootOfAQuadraticTest.cs +++ b/Sources/Tests/UnitTests/Calculus/RationalFunctionBesideTheRootOfAQuadraticTest.cs @@ -78,6 +78,34 @@ private static Entity DifferentiatesBack(string integrand, double[] points, para [InlineData("sqrt(1 + x^2)/(1 + x)", new[] { -3.0, -1.7, -0.4, 0.9, 2.6 })] public void TakenApartOverTheRoot(string integrand, double[] points) => DifferentiatesBack(integrand, points); + /// + /// A linear below the bar that divides the radicand leaves nothing to take apart -- its + /// residue is the radicand at its root, zero -- and is written over the radicand + /// instead: 1/(d + e x) is (d - e x)/(d^2 - e^2 x^2), a whole power of it + /// the same power of that. Rubi's 1.2.1.4. + /// + [Theory] + [InlineData("x*(d^2 - pe^2*x^2)^(5/2)/(d + pe*x)")] + [InlineData("x^2/((d + pe*x)*sqrt(d^2 - pe^2*x^2))")] + [InlineData("(1 - a^2*x^2)^(3/2)/((1 - a*x)^2*(c + d*x))")] + [InlineData("x^2*sqrt(1 - a^2*x^2)/(1 - a*x)^5")] + public void ALinearDividingTheRadicandIsWrittenOverIt(string integrand) + => DifferentiatesBack(integrand, new[] { -1.5, -0.9, -0.2, 0.4, 1.1, 1.6 }, + ("d", 1.3), ("pe", 0.7), ("a", 0.6), ("c", 2.1)); + + /// + /// The same with the radicand a product of the linear and another, every coefficient a + /// symbol: a d e + (c d^2 + a e^2) x + c d e x^2 is (d + e x)(a e + c d x), + /// and the other factor is written as that, not as the quotient the division leaves. + /// Checked where the radicand is positive. + /// + [Theory] + [InlineData("sqrt(a*d*pe + (c*d^2 + a*pe^2)*x + c*d*pe*x^2)/(x*(d + pe*x))")] + [InlineData("1/(x^2*(d + pe*x)*sqrt(a*d*pe + (c*d^2 + a*pe^2)*x + c*d*pe*x^2))")] + public void ALinearDividingARadicandOfSymbols(string integrand) + => DifferentiatesBack(integrand, new[] { 0.3, 0.6, 1.1, 1.6, 2.2 }, + ("d", 1.3), ("pe", 0.7), ("a", 0.6), ("c", 2.1)); + /// /// Moses', with symbols: B^2 over the root and A^2/2 over each linear. And /// a symbolic quadratic under the root beside a written linear, on both signs of its