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 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); + } +}