From 4ede03c1a01c6bc2ad176b196d8d34b27436ec03 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 10 Nov 2026 00:06:07 +0000 Subject: [PATCH] Invert a power of the variable in a product l'Hopital's rule reads at infinity A product of something vanishing and something diverging is written as a quotient before l'Hopital's rule reads it, the diverging factor on top and the reciprocal of the vanishing one underneath. Where the diverging factor is a power of the variable, that arrangement grows on every step: the reciprocal of the vanishing factor squares it under differentiation, so x^2 * ln(1 - 1 / x) at +oo went to x^3 * ln(1 - 1 / x)^2, then x^4 * ln(1 - 1 / x)^3, for three seconds before answering -oo. The power is inverted instead at an infinity, where its reciprocal is another power that differentiating lowers. A vanishing factor that leaves the reals on the approach keeps the old arrangement. (sqrt(x) - erf(x)) / x at 0 from the left reaches here at +oo with sqrt(-1 / x) in it, and over the power came out a directed complex infinity where it is left unevaluated. Faster, with the same answers: x^2 * ln(1 - 1 / x) at +oo 3.1 s to 0.19 s; (-1 - x) / log(x, 2)^(x - 1) at 1 from the left 6.0 s to 1.6 s, which is 2.6.0's time again; sinh((x - 1)^ln(x)) at 1 from the right 2.0 s to 0.6 s; ln(1 + 1 / x) * ln(1 / x) at +oo 2.1 s to 0.5 s. And some that were declined are answered, because the inverted quotient differentiates to something the rule can read: x^2 * (1 - cos(1 / x)) at +oo is 1/2, x * abs(2 / x) at +oo and -oo is 2 and -2, log(x, x^3 / abs(x)) at 0 from the right is 2, and log(2 * (-1)^x, arcsin(x) / (2 * x)) at 0 is -1, where master said NaN. 2.6.0 answers none of them. tanh(log(arcsin(x), x)) at 0 from the left is slower, 2.8 s to 6.7 s, same answer (2.6.0 declines it after 11 s): x^3 * arccsc(-x) inside it is now differentiated, and the caller declines the result. Only inverting where the old arrangement would differentiate too gave that time back, and lost x^2 * (1 - cos(1 / x)). Co-Authored-By: Claude Opus 5.5 --- .../Continuous/Limits/Transformations.cs | 12 +++++ .../APowerOfTheVariableIsInvertedTest.cs | 50 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 Sources/Tests/UnitTests/Calculus/APowerOfTheVariableIsInvertedTest.cs diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs index 771660b7c..c439ffcc1 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs @@ -1780,7 +1780,19 @@ private static bool IsIndeterminateQuotient(Entity numerator, Entity denominator // input. Into the denominator it gives ln(x) / (cos(x) / sin(x)), which is oo/oo // and which the rule settles at 0. var (restNumerator, restDenominator) = SplitProduct(rest); + // At an infinity a power of the variable is the one exception, and it is inverted instead: its + // reciprocal is another power, which differentiating lowers, where the reciprocal of the vanishing + // factor squares it on every step. x^2 * ln(1 - 1 / x) at +oo went to x^3 * ln(1 - 1 / x)^2, then + // x^4 * ln(1 - 1 / x)^3, for three seconds; written ln(1 - 1 / x) / x^(-2) it is settled in one. A + // vanishing factor that leaves the reals keeps the old arrangement: over the power, + // (sqrt(x) - erf(x)) / x at 0 from the left, which reaches here at +oo with sqrt(-1 / x) in it, came + // out a directed complex infinity rather than unevaluated. + if (!dest.IsFinite && IsAPowerOfTheVariable(infinity, x) && !LeavesTheRealsOnTheApproach(zero, x, dest, side)) + return restNumerator * zero / (restDenominator * (1 / infinity)).InnerSimplified; return restNumerator * infinity / (restDenominator * (1 / zero)).InnerSimplified; + + static bool IsAPowerOfTheVariable(Entity factor, Variable x) + => factor == x || factor is Powf(var @base, Number) && @base == x; } /// diff --git a/Sources/Tests/UnitTests/Calculus/APowerOfTheVariableIsInvertedTest.cs b/Sources/Tests/UnitTests/Calculus/APowerOfTheVariableIsInvertedTest.cs new file mode 100644 index 000000000..46400c865 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/APowerOfTheVariableIsInvertedTest.cs @@ -0,0 +1,50 @@ +// +// 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 System.Threading.Tasks; +using AngouriMath.Core; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// l'Hopital's rule read a product of a power of the variable and a vanishing factor as the power over the + /// reciprocal of that factor, which differentiating makes worse on every step: x^2 * ln(1 - 1 / x) at +oo went to + /// x^3 * ln(1 - 1 / x)^2, then x^4 * ln(1 - 1 / x)^3, for three seconds, and (-1 - x) / log(x, 2)^(x - 1) at 1 + /// from the left reaches it and took six. Found by limitcheck; no issue was filed. + /// + [Trait("Area", "Calculus")] + public sealed class APowerOfTheVariableIsInvertedTest + { + [Theory] + [InlineData("x^2 * ln(1 - 1 / x)", "+oo", ApproachFrom.BothSides, "-oo")] + [InlineData("ln(1 + 1 / x) * ln(1 / x)", "+oo", ApproachFrom.BothSides, "0")] + [InlineData("(x - 1) * ln(ln(2) / ln(x))", "1", ApproachFrom.Left, "0")] + [InlineData("(ln(2) / ln(x))^(x - 1)", "1", ApproachFrom.Left, "1")] + public void TheProductFinishes(string input, string at, ApproachFrom side, string expected) + { + Entity? limit = null; + var task = Task.Factory.StartNew(() => limit = input.ToEntity().Limit("x", at, side), TaskCreationOptions.LongRunning); + Assert.True(task.Wait(TimeSpan.FromSeconds(30)), "the limit did not finish in 30 seconds"); + Assert.Equal(expected.ToEntity(), limit); + } + + // Products of a power and a vanishing factor, at a finite point and at infinity, and the remarkable limits. + [Theory] + [InlineData("x * ln(1 + 1 / x)", "+oo", "1")] + [InlineData("x * log(2, 1 + 1 / x)", "+oo", "1 / ln(2)")] + [InlineData("x^2 * (1 - cos(1 / x))", "+oo", "1/2")] + [InlineData("x^4 * e^(-x)", "+oo", "0")] + [InlineData("x^(-2) * (1 - cos(x))", "0", "1/2")] + [InlineData("ln(1 + x) / x", "0", "1")] + [InlineData("ln(x) / (x - 1)", "1", "1")] + public void TheLimitStillHolds(string input, string at, string expected) + => Assert.Equal(expected.ToEntity().Simplify(), input.ToEntity().Limit("x", at).Simplify()); + } +}