Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,33 @@ internal static T WithinABudget<T>(int limits, System.Func<T> 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;
}

/// <summary>
/// The limits already found inside the outermost approach, keyed by the whole question.
/// </summary>
/// <remarks>
/// 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.
/// <para/>
/// 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.
/// </remarks>
[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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -354,7 +355,7 @@ internal static void LeaveApproach(Approach? previous)
{
currentApproach = previous;
if (--approachDepth == 0)
signMemo = null;
(signMemo, limitMemo) = (null, null);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// </summary>
[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);
}
}
Loading