diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 658f31ec2..e15106ce6 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -1391,6 +1391,25 @@ A secant above the bar is still declined: `sec(x)^2 e^(-3 i x)` is rational in ` imaginary rate, which nothing answers yet. Rubi's 4.7.2: 237 to 249 of 290, no row lost, 16 timeouts to 14. +### A power of a constant multiple of the radicand `arcosh` holds is written over that radicand + +`arcosh(a x)^2/sqrt(1 - a^2 x^2)` and `x (a + b arcosh(c x))/(d - c^2 d x^2)^3` ran out of time. +`arcosh(c x)` is `ln(c x + sqrt(c^2 x^2 - 1))`, and the substitution `u = arcosh(c x)` wants the +root its derivative carries, `sqrt(c^2 x^2 - 1)`; beside it the integrand holds a constant multiple +of that radicand, `d - c^2 d x^2 = -d (c^2 x^2 - 1)`, which nothing read as one. A whole power of +the multiple is now the product of the powers, and a root `(lambda M)^(k/2)` is `K^k M^(k/2)` with +`K = sqrt(lambda M)/sqrt(M)` -- plus or minus `i`, or `sqrt(lambda)`, constant on every interval +where it is defined -- which stands in front of the answer the way Rubi writes it. The answer +is checked where the integrand is real as well as where it is not: `arcosh(2 x)^2/sqrt(1 - 4 x^2)` +is real on `(-1/2, 1/2)`, where `arcosh(2x)` is `i arccos(2x)`. + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"arcosh(a*x)^2/sqrt(1-a^2*x^2)".Integrate("x")` | left unevaluated | `arcosh(a x)^3/(3a)` over `sqrt(1 - a^2 x^2)/sqrt(a^2 x^2 - 1)` | +| `"x*(a+b*arcosh(c*x))/(d-c^2*d*x^2)^3".Integrate("x")` | left unevaluated | in `arcosh(c x)` and powers of `c^2 x^2 - 1` | + +Rubi's 7.2.4 and 7.2.5, all 277 problems that count: 127 to 255, no row lost, 121 timeouts to 18. + ### `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 d0b5435e4..42586d2c4 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -14954,6 +14954,171 @@ static bool Through(Entity node, Entity.Variable x) } } + /// + /// A power of a constant multiple of a radicand that a logarithm holds under a root, + /// written over that radicand: acosh(c x) is ln(c x + sqrt(c^2 x^2 - 1)), + /// and beside it d - c^2 d x^2 is -d (c^2 x^2 - 1). A whole power is the + /// product of the powers; a root (lambda M)^(k/2) is K^k M^(k/2) with + /// K = sqrt(lambda M)/sqrt(M), which is constant wherever it is defined -- plus or + /// minus i, or plus or minus sqrt(lambda) -- so it stands in front of the + /// answer the way Rubi writes it. + /// + /// + /// The substitution u = acosh(c x) needs the root its derivative carries, + /// sqrt(c^2 x^2 - 1); written over 1 - c^2 x^2 the integrand is the same + /// function times i and nothing reads it: acosh(a x)^2/sqrt(1 - a^2 x^2) ran + /// out of time where acosh(a x)^2/sqrt(a^2 x^2 - 1) takes a third of a second, and + /// x (a + b acosh(c x))/(d - c^2 d x^2)^3 -- a whole power, no root at all -- the same. + /// K is carried through the integration as a symbol: it is constant, so an + /// antiderivative in it is one in x once it is written out, whatever the branches. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + internal static Entity? SolveByWritingAConstantMultipleOfARadicandOverIt(Entity expr, Entity.Variable x, bool integrateByParts) + { + // At the question asked only. The inverse cosine is in the integrand as asked, and a + // round of parts differentiates it away, so what is asked below holds no logarithm + // this rule needs -- but the substitutions below write logarithms of their own, and + // there the rule found a genuine multiple, rewrote it and asked a whole search again + // that was going to decline anyway: `atan(c x/sqrt(a - c^2 x^2))^2/sqrt(d - c^2 d x^2/a)`, + // with no logarithm in it as asked, took thirty-one seconds to decline where it took + // seven, twelve of them in each of two such re-asks. + if (!Integration.AnsweringTheQuestionAsked) + return null; + // The radicands a logarithm holds under a square root. + var references = new List(); + foreach (var logarithm in expr.Nodes.OfType()) + foreach (var node in logarithm.Nodes) + if (node is Powf(var radicand, Number.Rational { ERational: var half }) && half.Denominator.Equals(EInteger.FromInt32(2)) + && radicand.ContainsNode(x) && TreeAnalyzer.TryGetPolynomial(radicand, x, out var read) && read.Keys.Max() >= EInteger.One + && !references.Contains(radicand)) + references.Add(radicand); + if (references.Count == 0) + return null; + var multiple = Variable.CreateUnique(expr, "k_rad"); + Entity? written = null, root = null, reference = null; + var rewritten = expr.Replace(node => + { + if (node is not Powf(var @base, Number.Rational exponent) || !@base.ContainsNode(x) || references.Contains(@base)) + return node; + foreach (var candidate in references) + { + if (TryReadAsAConstantMultiple(@base, candidate, x) is not { } lambda) + continue; + if (exponent is Number.Integer) + return MathS.Pow(lambda, exponent) * MathS.Pow(candidate, exponent); + if (!exponent.ERational.Denominator.Equals(EInteger.FromInt32(2))) + return node; + // Only one such root per integrand: K is `sqrt(base)/sqrt(candidate)`, and two + // different bases would each want their own. + if (written is not null && (written != @base || reference != candidate)) + return node; + (written, reference) = (@base, candidate); + var numeratorOfExponent = Number.Integer.Create(exponent.ERational.Numerator); + return MathS.Pow(multiple, numeratorOfExponent) * MathS.Pow(candidate, exponent); + } + return node; + }); + if (rewritten == expr) + return null; + var simplified = rewritten.InnerSimplified; + if (simplified == expr || simplified == expr.InnerSimplified) + return null; + if (Integration.ComputeAsTheSameQuestion(simplified, x, integrateByParts) is not { } answer) + return null; + if (written is not null && reference is not null) + { + root = MathS.Sqrt(written) / MathS.Sqrt(reference); + answer = answer.Substitute(multiple, root); + } + return answer.ContainsNode(multiple) ? null : answer; + } + + /// + /// Whether / takes the same value at two + /// points, every other symbol pinned to a fixed value -- a necessary condition for a + /// constant ratio, and a cheap one. Where either side cannot be evaluated the question is + /// left to the symbolic check. + /// + private static bool HasTheSameRatioAtTwoPoints(Entity expr, Entity reference, Entity.Variable x) + { + var pinned = new Dictionary(); + var values = new[] { 1.37, 0.61, 2.23, 1.91, 0.83, 1.13 }; + var index = 0; + foreach (var symbol in expr.Vars.Concat(reference.Vars).Distinct()) + if (symbol != x) + pinned[symbol] = values[index++ % values.Length]; + Number.Complex? RatioAt(double at) + { + var top = expr.Substitute(x, at); + var bottom = reference.Substitute(x, at); + foreach (var pair in pinned) + { + top = top.Substitute(pair.Key, pair.Value); + bottom = bottom.Substitute(pair.Key, pair.Value); + } + try + { + return top.EvalNumerical() is Number.Complex a && bottom.EvalNumerical() is Number.Complex b && !b.IsZero + ? (Number.Complex)(a / b) : null; + } + catch (AngouriMath.Core.Exceptions.CannotEvalException) { return null; } + } + if (RatioAt(0.37) is not { } first || RatioAt(1.71) is not { } second) + return true; + var difference = (first - second).Abs() as Number.Real; + var scale = System.Math.Max(1.0, (double)((Number.Real)first.Abs()).EDecimal.ToDouble()); + return difference is null || (double)difference.EDecimal.ToDouble() <= 1e-9 * scale; + } + + /// + /// as a constant times , both + /// polynomials in of the same degree: the ratio of the leading + /// coefficients, where every other coefficient agrees with it once simplified. + /// + private static Entity? TryReadAsAConstantMultiple(Entity expr, Entity reference, Entity.Variable x) + { + // Read as written, or simplified first: the normalisation makes a power of a + // polynomial with a symbolic leading coefficient monic and writes the constant term as + // `d^0 (-1)^(-1) c^(-2)`, which the polynomial reader does not take -- and that is the + // spelling `(d - c^2 d x^2)^3` below the bar arrives in. Bare, because `d^0` simplifies + // to `1 provided not d = 0`, and a condition is not a polynomial either. + if (!TreeAnalyzer.TryGetPolynomial(expr, x, out var mine) + && !TreeAnalyzer.TryGetPolynomial(Functions.PartialFractions.Bare(expr.InnerSimplified), x, out mine)) + return null; + if (!TreeAnalyzer.TryGetPolynomial(reference, x, out var theirs) + || mine.Count != theirs.Count || mine.Values.Any(c => c.ContainsNode(x)) || theirs.Values.Any(c => c.ContainsNode(x))) + return null; + var top = theirs.Keys.Max()!; + if (!mine.TryGetValue(top, out var leading) || theirs[top].Evaled is Number.Complex { IsZero: true }) + return null; + // A constant multiple has the same ratio everywhere, so two points settle most pairs + // before anything is simplified: the check below simplifies symbolic differences, and + // asked of every power in a by-parts remainder of `atan(c x/sqrt(a - c^2 x^2))^2/ + // sqrt(d - c^2 d x^2/a)` -- two hundred of them -- it took twenty-five seconds. + if (!HasTheSameRatioAtTwoPoints(expr, reference, x)) + return null; + // Simplified where it holds symbols: the ratio of the leading coefficients of + // `d - c^2 d x^2` and `c^2 x^2 - 1` is `-c^2 d/c^2` to the inner simplification, and + // written into a whole power that way it cost the integrand its time -- `-d` is + // what the rules below want to see. + var lambda = (leading / theirs[top]).InnerSimplified; + if (lambda.Vars.Any()) + lambda = Functions.PartialFractions.Bare(lambda.Simplify()); + if (lambda.Evaled is Number.Complex { IsZero: true } || lambda == Number.Integer.One) + return null; + foreach (var pair in theirs) + { + if (!mine.TryGetValue(pair.Key, out var own)) + return null; + var difference = (own - lambda * pair.Value).InnerSimplified; + if (difference.Evaled is Number.Complex { IsZero: true }) + continue; + if (!difference.Vars.Any() || Functions.PartialFractions.Bare(difference.Simplify()).Evaled is not Number.Complex { IsZero: true }) + return null; + } + return lambda; + } + /// /// A fractional or symbolic power of a monomial, distributed: (c x^n)^b is /// c^b x^(n b) for a positive c and a n that is not whole. diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index ba3cb32b3..b12321821 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -516,6 +516,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => // And a fractional or symbolic power of a monomial: `(c x^n)^b` is `c^b x^(n b)` // for a positive `c`, on the `x > 0` where a symbolic `n` leaves the integrand real. if ((answer = IndefiniteIntegralSolver.SolveByDistributingAPowerOfAMonomial(expr, x, integrateByParts)) is { }) return answer; + // A power of a constant multiple of the radicand a logarithm holds under a root, + // written over that radicand: `acosh(c x)` beside `(d - c^2 d x^2)^(5/2)`. + if ((answer = IndefiniteIntegralSolver.SolveByWritingAConstantMultipleOfARadicandOverIt(expr, x, integrateByParts)) is { }) return answer; // A power of the variable times a sine or cosine of a logarithm, where two rounds of // parts close on the integrand: `int x^m sin(a + b ln(c x^n))` in closed form. if ((answer = IndefiniteIntegralSolver.SolveAPowerTimesATrigonometricOfALogarithm(expr, x, integrateByParts)) is { }) return answer; diff --git a/Sources/Tests/UnitTests/Calculus/InverseHyperbolicSubstitutionTest.cs b/Sources/Tests/UnitTests/Calculus/InverseHyperbolicSubstitutionTest.cs index 33f5c0ddd..11091a0de 100644 --- a/Sources/Tests/UnitTests/Calculus/InverseHyperbolicSubstitutionTest.cs +++ b/Sources/Tests/UnitTests/Calculus/InverseHyperbolicSubstitutionTest.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) 2019-2026 Angouri. // AngouriMath is licensed under MIT. // Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. @@ -79,6 +79,24 @@ private static void DifferentiatesBack(string integrand, double[] points) [InlineData("artanh(2*x)/(1 - 4*x^2)^(3/2)")] public void TheTangent(string integrand) => DifferentiatesBack(integrand, InsideTheHalf); + /// + /// A power of a constant multiple of the radicand the inverse cosine holds, written over + /// that radicand: arcosh(c x) is ln(c x + sqrt(c^2 x^2 - 1)), and + /// (d - c^2 d x^2)^(k/2) beside it is K^k (c^2 x^2 - 1)^(k/2) for + /// K = sqrt(d - c^2 d x^2)/sqrt(c^2 x^2 - 1), which is constant on each interval + /// where it is defined and stands in front of the answer; a whole power is the product + /// of the powers. These are answered now, not only right when answered: the first is + /// real on (-1/2, 1/2), where arcosh(2x) is i arccos(2x) and its + /// square real, and is checked there. Rubi's 7.2.4 and 7.2.5, 130 rows. + /// + [Theory] + [InlineData("arcosh(2*x)^2/sqrt(1 - 4*x^2)", new[] { -0.45, -0.2, 0.1, 0.3, 0.45 })] + [InlineData("x*(5 - 20*x^2)^(3/2)*(3 + 2*arcosh(2*x))", new[] { -0.4, 0.1, 0.3, 0.7, 1.2 })] + [InlineData("(3 + 2*arcosh(2*x))/(5 - 20*x^2)^(5/2)", new[] { -0.4, 0.1, 0.3, 0.7, 1.2 })] + [InlineData("x*(3 + 2*arcosh(2*x))/(5 - 20*x^2)^3", new[] { -0.4, 0.1, 0.3, 0.7, 1.2 })] + public void AConstantMultipleOfTheRadicandIsWrittenOverIt(string integrand, double[] points) + => DifferentiatesBack(integrand, points); + /// /// A root of a *negative* multiple of the quadratic is not the multiple's root times /// the quadratic's: arcosh(a x)^2/sqrt(1 - a^2 x^2) came back with i a