Skip to content

Commit cc5e13b

Browse files
Ask a compared side's whole limit as that side's own question (#2402)
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 <noreply@anthropic.com>
1 parent 42af5e0 commit cc5e13b

3 files changed

Lines changed: 63 additions & 6 deletions

File tree

‎Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,16 @@ private static bool IsZeroTimesAnInfinity(Entity first, Entity second)
294294
if (Functions.Algebra.LimitFunctional.IsTheWholeQuestion(quotient))
295295
return null;
296296
quotientsAskedWhole ??= new();
297-
if (quotientsAskedWhole.Count > 0 || !quotientsAskedWhole.Add(quotient))
297+
if (quotientsAskedWhole.Count > 0)
298+
return null;
299+
// The expression whose two sides a two-sided limit is comparing, asked whole from one side, is that side's
300+
// own question rather than one asked inside another, and does not use the one level up. Counted, it left
301+
// that side nothing to ask of its own regrouping: x^pi / log(x, 2) at 0 from the right reads as
302+
// x^pi * ln(x), asked whole, in a fifth of a second alone, and from inside the comparison it was refused
303+
// and the side spent ten seconds on other readings that did not answer.
304+
if (Functions.Algebra.LimitFunctional.IsTheComparedQuestion(quotient))
305+
return Functions.Algebra.LimitFunctional.ComputeLimit(quotient, x, dist, side);
306+
if (!quotientsAskedWhole.Add(quotient))
298307
return null;
299308
try { return Functions.Algebra.LimitFunctional.ComputeLimit(quotient, x, dist, side); }
300309
finally { quotientsAskedWhole.Remove(quotient); }

‎Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,18 @@ internal static bool MayReadAPowerThroughItsExponent(Entity power)
312312
/// </summary>
313313
internal static bool IsTheWholeQuestion(Entity expr) => ReferenceEquals(expr, descentRoot);
314314

315+
/// <summary>
316+
/// The expression whose sides a two-sided limit is comparing. Its whole limit asked from
317+
/// one side inside that comparison is the question of that side, not one asked inside another.
318+
/// </summary>
319+
[System.ThreadStatic] private static Entity? comparedRoot;
320+
321+
/// <summary>
322+
/// Whether <paramref name="expr"/> is the expression whose two sides are being compared, so
323+
/// that its whole limit from one side is that side's own question.
324+
/// </summary>
325+
internal static bool IsTheComparedQuestion(Entity expr) => ReferenceEquals(expr, comparedRoot);
326+
315327
/// <summary>
316328
/// A logarithm of a power to a constant real exponent, log(f^c), as c log(f), or as
317329
/// c log(-f) + log((-1)^c) where f is negative near the point.
@@ -431,12 +443,12 @@ internal static T WithinABudget<T>(int limits, System.Func<T> compute)
431443
if (expr.Nodes.Any(static node => node is Derivativef))
432444
expr = WithTheDerivativesTaken(expr, x, dest, side);
433445
var enclosingApproach = EnterApproach(x, dest);
434-
var (outerComparing, outerRoot) = (comparingSides, descentRoot);
435-
(comparingSides, descentRoot) = (false, null);
446+
var (outerComparing, outerRoot, outerCompared) = (comparingSides, descentRoot, comparedRoot);
447+
(comparingSides, descentRoot, comparedRoot) = (false, null, null);
436448
try { return ComputeLimitWithinApproach(expr, x, dest, side, acceptNaN); }
437449
finally
438450
{
439-
(comparingSides, descentRoot) = (outerComparing, outerRoot);
451+
(comparingSides, descentRoot, comparedRoot) = (outerComparing, outerRoot, outerCompared);
440452
LeaveApproach(enclosingApproach);
441453
}
442454
}
@@ -692,9 +704,9 @@ node is Derivativef(var expression, Variable variable, var iterations) && iterat
692704
Entity? DescendComparingSides(ApproachFrom oneSide)
693705
{
694706
var stepped = WithStepsAtTheirLimits(expr, x, dest, oneSide);
695-
comparingSides = true;
707+
(comparingSides, comparedRoot) = (true, stepped);
696708
try { return stepped.ComputeLimitDivideEtImpera(x, dest, oneSide); }
697-
finally { comparingSides = false; }
709+
finally { (comparingSides, comparedRoot) = (false, null); }
698710
}
699711
}
700712

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System;
9+
using System.Threading.Tasks;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
13+
namespace AngouriMath.Tests.Calculus
14+
{
15+
/// <summary>
16+
/// A two-sided limit compares its sides by descending into each, and the descent asks the whole expression's limit
17+
/// from that side. That ask used the one level of whole limits a descent may ask, so the side could not ask the
18+
/// regrouping that answers it: x^pi / log(x, 2) at 0 took ten seconds two-sided, against a fifth of one from the
19+
/// right alone. Found by limitcheck; no issue was filed.
20+
/// </summary>
21+
[Trait("Area", "Calculus")]
22+
public sealed class AComparedSideIsItsOwnQuestionTest
23+
{
24+
[Theory]
25+
[InlineData("x^pi / log(x, 2)", "0")]
26+
[InlineData("x^pi / log(x, 2) - 2", "-2")]
27+
[InlineData("x^e / log(x, 2)", "0")]
28+
public void ATwoSidedLimitAtZeroFinishes(string input, string expected)
29+
{
30+
Entity? limit = null;
31+
var task = Task.Factory.StartNew(() => limit = input.ToEntity().Limit("x", 0), TaskCreationOptions.LongRunning);
32+
Assert.True(task.Wait(TimeSpan.FromSeconds(30)), "the limit did not finish in 30 seconds");
33+
Assert.Equal(expected.ToEntity(), limit);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)