Skip to content

Commit e1fe08c

Browse files
Leave a definite integral as written where a bound's limit is not decided (#2101)
* Leave a definite integral as written where a bound's limit is not decided Where the antiderivative is undefined at a bound, its limit from inside the range is taken, and where that limit could not be decided the integral was NaN, which says it does not exist. The integral of ln(x) erf(x) over [0, 1] was NaN where it is -0.2613, and of ln(x) Si(x) where it is -0.2466. It is left as written now. Left as written, Simplify evaluates it again for each spelling of the integrand it tries, and one spelling of ln(x) Si(x)'s antiderivative at 0 sent the limit engine through more than 38,000 nested limits in forty seconds without deciding. So the limit at a bound is asked within a budget of 2,000 limits taken along the way, past which it is undecided, and each is remembered under the settings it was taken in. The most a decided limit at a bound took in the tests is 957. The two simplifications end in 2.2 and 7 s. Fixes #2081. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * Give the undecided-bound test a wall that cannot fire The limit budget bounds the work; the sixty-second wall measured the machine, and fired in the full suite on a loaded one, where the erf integral takes about nine seconds alone. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent a6f3d31 commit e1fe08c

4 files changed

Lines changed: 113 additions & 5 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ not seen, and a symmetric divergent one comes to its principal value
7575
| `"ln(x)".ToEntity().DefiniteIntegral("x", 0, 1)` | `NaN` | `-1.00000` |
7676
| `"1/x".ToEntity().DefiniteIntegral("x", 0, 1)` | `NaN` | `NaN` |
7777

78+
### A definite integral whose bound has no decided limit is left as written
79+
80+
**Not silent: what was `NaN` is the integral as written.** Where the antiderivative is undefined at a bound, its limit
81+
from inside the range is taken, and where that limit could not be decided the integral was `NaN`, which says it does not
82+
exist. It is left as written now. The limit at a bound is also asked within a budget of the limits it takes along the way,
83+
so that a simplification asking it for each spelling of the integrand ends.
84+
([#2081](https://github.com/asc-community/AngouriMath/issues/2081))
85+
86+
| Input | Was (2.6.0) | Now |
87+
|---|---|---|
88+
| `"integral(ln(x) * erf(x), x, 0, 1)".ToEntity().Simplify()` | `NaN` | `integral(erf(x) * ln(x), x, 0, 1)`, whose value is `-0.2613…` |
89+
| `"integral(ln(x) * Si(x), x, 0, 1)".ToEntity().Simplify()` | `NaN` | `integral(Si(x) * ln(x), x, 0, 1)`, whose value is `-0.2466…` |
90+
7891
### A root of a small or a large number is searched for at the size of one
7992

8093
**Silent: wrong answers.** The search for an exact root of a number rounded each candidate to six digits, absolutely.

‎Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs‎

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,10 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
121121
/// at 0 is <c>0 * -oo</c>, so the integral of <c>ln(x)</c> from 0 to 1 was <c>NaN</c> where it
122122
/// is -1. The limit is taken only where the value is undefined, so a bound the substitution
123123
/// answered is answered as before. Its side is known at an infinity, and at a finite bound
124-
/// from the other bound where both have numeric values. Elsewhere, and where the limit is not
125-
/// decided, the value stays undefined, as it is for <c>sin(x)</c> from 0 to <c>+oo</c>,
126-
/// whose antiderivative has no limit there.
124+
/// from the other bound where both have numeric values. Elsewhere, and where the limit is
125+
/// undefined, the value stays undefined, as it is for <c>sin(x)</c> from 0 to <c>+oo</c>,
126+
/// whose antiderivative has no limit there. Where the limit is not decided there is no value,
127+
/// <see langword="null"/>, and the integral is left as written.
127128
/// </remarks>
128129
private static Entity? AtTheBound(Entity antiderivative, Variable x, Entity bound, Entity otherBound)
129130
{
@@ -147,9 +148,51 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
147148
// and evaluating Gamma(3/4) at every step, for over a minute. https://github.com/asc-community/AngouriMath/issues/2075
148149
if (HoldsAnIncompleteGammaFunction(antiderivative))
149150
return null;
150-
var limit = antiderivative.Limit(x, bound, inside);
151-
return limit.IsNaN || limit.Nodes.Any(node => node is Limitf) ? value : limit;
151+
var limit = LimitAtTheBound(antiderivative, x, bound, inside);
152+
// A limit that is not decided is no value: the integral is left as written. Undefined would say it does not
153+
// exist, and the integral of ln(x) erf(x) from 0 to 1 was NaN where it is -0.2613, for want of erf's limit at
154+
// 0. https://github.com/asc-community/AngouriMath/issues/2081
155+
if (limit.Nodes.Any(node => node is Limitf))
156+
return null;
157+
return limit.IsNaN ? value : limit;
158+
}
159+
160+
/// <summary>
161+
/// The antiderivative's limit at a bound, remembered under the settings it was taken in, as
162+
/// the cache of answers remembers an antiderivative. A definite integral left as written is evaluated
163+
/// again by every candidate <c>Simplify</c> tries, and the limit that left it so is the slow part: at 0,
164+
/// the antiderivative of <c>ln(x) erf(x)</c> is an infinity less an infinity that l'Hopital's rule does
165+
/// not settle, two seconds of search each time, and the simplification ran past two minutes.
166+
/// https://github.com/asc-community/AngouriMath/issues/2081
167+
/// </summary>
168+
private static Entity LimitAtTheBound(Entity antiderivative, Variable x, Entity bound, ApproachFrom side)
169+
{
170+
if (limitsAtBounds is null || limitsAtBoundsUnder is null || !SettingsState.StillHolds(limitsAtBoundsUnder))
171+
{
172+
limitsAtBounds = new();
173+
limitsAtBoundsUnder = SettingsState.Snapshot();
174+
}
175+
var into = limitsAtBounds;
176+
var stamp = limitsAtBoundsUnder;
177+
var key = (antiderivative, x, bound, side);
178+
if (into.TryGetValue(key, out var known))
179+
return known;
180+
// Within a budget of the limits it asks along the way. Past it the limit is undecided and the integral left
181+
// as written, which is no wrong answer. The most a decided limit at a bound took in the tests is 957; one
182+
// spelling of the antiderivative of ln(x) Si(x) took over 38,000 in forty seconds without deciding.
183+
var limit = Functions.Algebra.LimitFunctional.WithinABudget(LimitsAtABound, () => antiderivative.Limit(x, bound, side));
184+
if (SettingsState.StillHolds(stamp))
185+
{
186+
if (into.Count >= 256)
187+
into.Clear();
188+
into[key] = limit;
189+
}
190+
return limit;
152191
}
192+
[System.ThreadStatic] private static Dictionary<(Entity, Variable, Entity, ApproachFrom), Entity>? limitsAtBounds;
193+
/// <summary>How many limits the limit at one bound may ask in all.</summary>
194+
private const int LimitsAtABound = 2000;
195+
[System.ThreadStatic] private static object?[]? limitsAtBoundsUnder;
153196

154197
/// <summary>
155198
/// Integrates numerically over <paramref name="x"/> between two bounds, without

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,38 @@ private static Entity ExpandLogarithm(Entity expr)
163163
_ => expr
164164
};
165165

166+
/// <summary>
167+
/// How many limits a budgeted computation may still take, every one it asks along the way counted, or
168+
/// <see langword="null"/> where there is no budget. Past it each is undecided. Set by <see cref="WithinABudget"/>.
169+
/// </summary>
170+
[System.ThreadStatic] private static int? remainingLimits;
171+
172+
/// <summary>
173+
/// <paramref name="compute"/> with at most <paramref name="limits"/> limits taken in all, for a caller that has a
174+
/// use only for a limit it can have cheaply, and leaves the question open otherwise.
175+
/// </summary>
176+
internal static T WithinABudget<T>(int limits, System.Func<T> compute)
177+
{
178+
var enclosing = remainingLimits;
179+
var start = enclosing is { } outer ? System.Math.Min(outer, limits) : limits;
180+
remainingLimits = start;
181+
try { return compute(); }
182+
finally
183+
{
184+
// What was spent here is spent from the enclosing budget too.
185+
var used = start - (remainingLimits ?? 0);
186+
remainingLimits = enclosing is { } stillOuter ? stillOuter - used : null;
187+
}
188+
}
189+
166190
public static Entity? ComputeLimit(Entity expr, Variable x, Entity dest, ApproachFrom side = ApproachFrom.BothSides, bool acceptNaN = false)
167191
{
192+
if (remainingLimits is { } remaining)
193+
{
194+
if (remaining <= 0)
195+
return null;
196+
remainingLimits = remaining - 1;
197+
}
168198
// The approach is stated for the whole computation, not only where the destination
169199
// has been normalised to +oo. Everything below simplifies as it goes, and a rule that
170200
// may only fire while the approach is in scope is otherwise declining through most of

‎Sources/Tests/UnitTests/Calculus/ImproperIntegralTest.cs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ public void ABoundWhereTheAntiderivativeIsUndefinedIsALimit(string integrand, st
5959
[Fact]
6060
public void AnAntiderivativeWithoutALimitLeavesNaN() => Assert.Equal(MathS.NaN, Definite("sin(x)", "0", "+oo"));
6161

62+
/// <summary>
63+
/// And as written where the limit is not decided: undefined would say the integral does not exist, and
64+
/// <c>ln(x) erf(x)</c> over <c>[0, 1]</c>, whose antiderivative is an infinity less an infinity at 0 that no rule
65+
/// settles, was <c>NaN</c> where it is -0.2613. The limit there is asked within a budget, and the simplification,
66+
/// which asks it again for each spelling of the integrand it tries, ran past two minutes without one.
67+
/// https://github.com/asc-community/AngouriMath/issues/2081
68+
/// </summary>
69+
[Theory]
70+
[InlineData("ln(x) * erf(x)")]
71+
[InlineData("ln(x) * Si(x)")]
72+
public void ABoundWhoseLimitIsNotDecidedLeavesTheIntegralAsWritten(string integrand)
73+
{
74+
Entity? result = null;
75+
var task = System.Threading.Tasks.Task.Run(() => result = Definite(integrand, "0", "1"));
76+
// The budget bounds the work, in limits; this wall is a backstop against a hang that the budget no longer
77+
// bounds, not a measure of time. The erf integral takes about nine seconds alone, and a wall of sixty
78+
// fired in the full suite on a loaded machine.
79+
Assert.True(task.Wait(System.TimeSpan.FromMinutes(5)), $"the integral of {integrand} from 0 to 1 was still being simplified");
80+
Assert.IsType<Entity.Integralf>(result);
81+
Assert.DoesNotContain(result!.Nodes, node => node == MathS.NaN);
82+
}
83+
6284
/// <summary>A bound the substitution answers is answered as it was.</summary>
6385
[Theory]
6486
[InlineData("e^(-x)", "0", "+oo", "1")]

0 commit comments

Comments
 (0)