From a5b02c0ed4c9a9d12b26d6d882018fad74d3ce2e Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 10 Nov 2026 14:52:27 +0000 Subject: [PATCH 1/2] Remember the limits found within one question The limit machinery re-enters itself with questions it has already answered. In tanh(log(arcsin(x), x)) at 0 from the left, the limit of the exponent ln(x) / ln(arcsin(x)) was taken four times over, at a second or more each, and the whole took ten seconds. The limits found are now kept for as long as the outermost approach is read, beside the signs already kept there, and the same question asked again is answered from them. Only answers are kept. A decline can be the scope's and not the question's -- a budget of limits spent, a re-entry guard at its depth -- and a NaN can be an indeterminate form read on the way down, which callers search past. Co-Authored-By: Claude Opus 5.5 --- .../Limits/Solvers/Solvers.Definition.cs | 27 +++++++++++++++++ .../Continuous/Limits/Transformations.cs | 3 +- .../ALimitAlreadyFoundIsNotTakenAgainTest.cs | 30 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 Sources/Tests/UnitTests/Calculus/ALimitAlreadyFoundIsNotTakenAgainTest.cs diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs index 4ec47e127..cd4651035 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs @@ -421,6 +421,33 @@ internal static T WithinABudget(int limits, System.Func compute) } public static Entity? ComputeLimit(Entity expr, Variable x, Entity dest, ApproachFrom side = ApproachFrom.BothSides, bool acceptNaN = false) + { + var key = (expr, x, dest, side, acceptNaN); + if (limitMemo is { } memo && memo.TryGetValue(key, out var known)) + return known; + var found = ComputeLimitUnmemoised(expr, x, dest, side, acceptNaN); + if (found is { } && limitMemo is { } store && found.Evaled != MathS.NaN) + store[key] = found; + return found; + } + + /// + /// The limits already found inside the outermost approach, keyed by the whole question. + /// + /// + /// The machinery re-enters itself with questions it has already answered: in + /// tanh(log(arcsin(x), x)) at 0 from the left, the limit of the exponent ln(x) / ln(arcsin(x)) + /// was taken four times over, at a second or more each, and the whole took ten seconds. + /// + /// Only answers are kept. A decline can be the scope's and not the question's -- a budget + /// of limits spent, a re-entry guard at its depth -- and asked again elsewhere the same + /// question may be answered. A NaN is not kept either: it can be an indeterminate form + /// read on the way down, which callers withhold and search past rather than an answer. + /// Created and dropped with the sign memo, so nothing survives a call. + /// + [System.ThreadStatic] private static Dictionary<(Entity Expr, Variable X, Entity Dest, ApproachFrom Side, bool AcceptNaN), Entity>? limitMemo; + + private static Entity? ComputeLimitUnmemoised(Entity expr, Variable x, Entity dest, ApproachFrom side, bool acceptNaN) { if (remainingLimits is { } remaining) { diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs index c3cc95f89..e98d9ecb7 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs @@ -342,6 +342,7 @@ private static int MemoisedSign(Entity expr, Approach approach) var previous = currentApproach; approachDepth++; signMemo ??= new(); + limitMemo ??= new(); currentApproach = new Approach(x, dest); return previous; } @@ -354,7 +355,7 @@ internal static void LeaveApproach(Approach? previous) { currentApproach = previous; if (--approachDepth == 0) - signMemo = null; + (signMemo, limitMemo) = (null, null); } /// diff --git a/Sources/Tests/UnitTests/Calculus/ALimitAlreadyFoundIsNotTakenAgainTest.cs b/Sources/Tests/UnitTests/Calculus/ALimitAlreadyFoundIsNotTakenAgainTest.cs new file mode 100644 index 000000000..69d7e5f87 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ALimitAlreadyFoundIsNotTakenAgainTest.cs @@ -0,0 +1,30 @@ +// +// 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 AngouriMath.Core; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// The limit machinery remembers the limits it has found within one question, and does not take them again. In + /// tanh(log(arcsin(x), x)) at 0 the limit of the exponent ln(x) / ln(arcsin(x)) was taken four times over, at a + /// second or more each: ten seconds from the left, against under half a second with the answers kept. Found by + /// limitcheck; no issue was filed. + /// + [Trait("Area", "Calculus")] + public sealed class ALimitAlreadyFoundIsNotTakenAgainTest + { + [Theory] + [InlineData(ApproachFrom.Left)] + [InlineData(ApproachFrom.Right)] + [InlineData(ApproachFrom.BothSides)] + public void TheLimitIsAnswered(ApproachFrom side) + => Assert.Equal("tanh(1)".ToEntity().Evaled, "tanh(log(arcsin(x), x))".ToEntity().Limit("x", 0, side).Evaled); + } +} From 0611d361c53b6905353abee65a3824a894ef65d2 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 10 Nov 2026 16:15:12 +0000 Subject: [PATCH 2/2] Record tanh(log(arcsin(x), x)) at 0 as a changed answer From the right it was 1 on 2.6.0, where the limit is tanh(1); from the left and from both sides it was left unevaluated. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 1bae30766..36f63e4fd 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -877,12 +877,19 @@ for -oo/+oo too, and a form with no value is kept only where nothing better is f answered as well. A hyperbolic sine took ten seconds to be left unevaluated, and is answered in under three. `x * ln(x)` at 0 is still `NaN`, since ln(x) has no real values to the left of 0. +**Silent: a different answer.** `tanh(log(arcsin(x), x))` at 0 from the right was `1`, where the logarithm tends to 1 +and the limit is `tanh(1)`. From the left and from both sides it took ten seconds to be left unevaluated, and is +answered in under two. + | Input | Was (2.6.0) | Now | |---|---|---| | `"e^log(1/x, arcsin(x))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(e ^ log(1 / x, arcsin(x)), x, 0)` | `1 / e` | | `"sin(log(1/x, arcsin(x)))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(sin(log(1 / x, arcsin(x))), x, 0)` | `sin(-1)` | | `"sinh(log(1/x, arcsin(x)))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright((e ^ log(1 / x, arcsin(x)) - e ^ (-log(1 / x, arcsin(x)))) / 2, x, 0)` | `(1 / e - e) / 2` | | `"tanh(log(x, arcsin(x)))".ToEntity().Limit("x", 0, ApproachFrom.Left)` | `limitleft((e ^ (2 * log(x, arcsin(x))) - 1) / (e ^ (2 * log(x, arcsin(x))) + 1), x, 0)` | `(e ^ 2 - 1) / (e ^ 2 + 1)` | +| `"tanh(log(arcsin(x), x))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `1` | `(e ^ 2 - 1) / (e ^ 2 + 1)` | +| `"tanh(log(arcsin(x), x))".ToEntity().Limit("x", 0, ApproachFrom.Left)` | `limitleft((e ^ (2 * log(arcsin(x), x)) - 1) / (e ^ (2 * log(arcsin(x), x)) + 1), x, 0)` | `(e ^ 2 - 1) / (e ^ 2 + 1)` | +| `"tanh(log(arcsin(x), x))".ToEntity().Limit("x", 0)` | `limit((e ^ (2 * log(arcsin(x), x)) - 1) / (e ^ (2 * log(arcsin(x), x)) + 1), x, 0)` | `(e ^ 2 - 1) / (e ^ 2 + 1)` | ### A function of a quotient tending to 0/0 has the value at the quotient's limit