From 3137bda48974c1276c2d4c8d24206200e749077b Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 9 Nov 2026 18:03:58 +0000 Subject: [PATCH] Ask a compared side's whole limit as that side's own question A two-sided limit at a finite point compares its sides by descending into each, and the descent asks the whole expression's limit from that side. That ask took the one level of whole limits a descent may ask, so inside it the side could not ask the regrouping that answers it alone. x^pi / log(x, 2) at 0 from the right reads as x^pi * ln(x), asked whole, in 0.2 s; inside the comparison that was refused, and the side spent 9.5 s on l'Hopital's rule and the rewrites at infinity before coming back 0/0. The two-sided limit took 10.3 s. The expression whose sides are compared, asked whole from one side, is that side's own question, and no longer counts as a level. The two-sided limit takes 0.74 s, with the same answer; x^pi / log(x, 2) - 2 takes 0.92 s, from 13 s. Co-Authored-By: Claude Opus 5.5 --- .../Limits/Solvers/Limit.Classes.cs | 11 +++++- .../Limits/Solvers/Solvers.Definition.cs | 22 +++++++++--- .../AComparedSideIsItsOwnQuestionTest.cs | 36 +++++++++++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/AComparedSideIsItsOwnQuestionTest.cs diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs index ecb329a4e..6179c9e30 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs @@ -294,7 +294,16 @@ private static bool IsZeroTimesAnInfinity(Entity first, Entity second) if (Functions.Algebra.LimitFunctional.IsTheWholeQuestion(quotient)) return null; quotientsAskedWhole ??= new(); - if (quotientsAskedWhole.Count > 0 || !quotientsAskedWhole.Add(quotient)) + if (quotientsAskedWhole.Count > 0) + return null; + // The expression whose two sides a two-sided limit is comparing, asked whole from one side, is that side's + // own question rather than one asked inside another, and does not use the one level up. Counted, it left + // that side nothing to ask of its own regrouping: x^pi / log(x, 2) at 0 from the right reads as + // x^pi * ln(x), asked whole, in a fifth of a second alone, and from inside the comparison it was refused + // and the side spent ten seconds on other readings that did not answer. + if (Functions.Algebra.LimitFunctional.IsTheComparedQuestion(quotient)) + return Functions.Algebra.LimitFunctional.ComputeLimit(quotient, x, dist, side); + if (!quotientsAskedWhole.Add(quotient)) return null; try { return Functions.Algebra.LimitFunctional.ComputeLimit(quotient, x, dist, side); } finally { quotientsAskedWhole.Remove(quotient); } diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs index 2279192a8..6bed93880 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs @@ -312,6 +312,18 @@ internal static bool MayReadAPowerThroughItsExponent(Entity power) /// internal static bool IsTheWholeQuestion(Entity expr) => ReferenceEquals(expr, descentRoot); + /// + /// The expression whose sides a two-sided limit is comparing. Its whole limit asked from + /// one side inside that comparison is the question of that side, not one asked inside another. + /// + [System.ThreadStatic] private static Entity? comparedRoot; + + /// + /// Whether is the expression whose two sides are being compared, so + /// that its whole limit from one side is that side's own question. + /// + internal static bool IsTheComparedQuestion(Entity expr) => ReferenceEquals(expr, comparedRoot); + /// /// A logarithm of a power to a constant real exponent, log(f^c), as c log(f), or as /// c log(-f) + log((-1)^c) where f is negative near the point. @@ -431,12 +443,12 @@ internal static T WithinABudget(int limits, System.Func compute) if (expr.Nodes.Any(static node => node is Derivativef)) expr = WithTheDerivativesTaken(expr, x, dest, side); var enclosingApproach = EnterApproach(x, dest); - var (outerComparing, outerRoot) = (comparingSides, descentRoot); - (comparingSides, descentRoot) = (false, null); + var (outerComparing, outerRoot, outerCompared) = (comparingSides, descentRoot, comparedRoot); + (comparingSides, descentRoot, comparedRoot) = (false, null, null); try { return ComputeLimitWithinApproach(expr, x, dest, side, acceptNaN); } finally { - (comparingSides, descentRoot) = (outerComparing, outerRoot); + (comparingSides, descentRoot, comparedRoot) = (outerComparing, outerRoot, outerCompared); LeaveApproach(enclosingApproach); } } @@ -692,9 +704,9 @@ node is Derivativef(var expression, Variable variable, var iterations) && iterat Entity? DescendComparingSides(ApproachFrom oneSide) { var stepped = WithStepsAtTheirLimits(expr, x, dest, oneSide); - comparingSides = true; + (comparingSides, comparedRoot) = (true, stepped); try { return stepped.ComputeLimitDivideEtImpera(x, dest, oneSide); } - finally { comparingSides = false; } + finally { (comparingSides, comparedRoot) = (false, null); } } } diff --git a/Sources/Tests/UnitTests/Calculus/AComparedSideIsItsOwnQuestionTest.cs b/Sources/Tests/UnitTests/Calculus/AComparedSideIsItsOwnQuestionTest.cs new file mode 100644 index 000000000..52a4df8b7 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/AComparedSideIsItsOwnQuestionTest.cs @@ -0,0 +1,36 @@ +// +// 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.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// A two-sided limit compares its sides by descending into each, and the descent asks the whole expression's limit + /// from that side. That ask used the one level of whole limits a descent may ask, so the side could not ask the + /// regrouping that answers it: x^pi / log(x, 2) at 0 took ten seconds two-sided, against a fifth of one from the + /// right alone. Found by limitcheck; no issue was filed. + /// + [Trait("Area", "Calculus")] + public sealed class AComparedSideIsItsOwnQuestionTest + { + [Theory] + [InlineData("x^pi / log(x, 2)", "0")] + [InlineData("x^pi / log(x, 2) - 2", "-2")] + [InlineData("x^e / log(x, 2)", "0")] + public void ATwoSidedLimitAtZeroFinishes(string input, string expected) + { + Entity? limit = null; + var task = Task.Factory.StartNew(() => limit = input.ToEntity().Limit("x", 0), TaskCreationOptions.LongRunning); + Assert.True(task.Wait(TimeSpan.FromSeconds(30)), "the limit did not finish in 30 seconds"); + Assert.Equal(expected.ToEntity(), limit); + } + } +}