diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index c3d222c32..6bd53f143 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -166,8 +166,86 @@ private static Entity Normalized(Entity expr) => /// private const int MostAnswersKept = 4096; + /// + /// How deep the recursive descent may go before it declines to go further. + /// + /// + /// + /// Without this the descent has **no bound at all**, and a stack overflow is not an + /// exception a caller can handle: it takes the process down, so anything the process had + /// not finished is lost. Found by work/intbench against Rubi's independent test + /// suites, where the run died with SIGABRT partway through; the trace was thousands + /// of frames alternating with + /// . + /// https://github.com/asc-community/AngouriMath/issues/1232 + /// + /// + /// Why the memo above does not already stop it, which is the part worth stating. + /// SolveBySubstitution names its new variable with + /// Variable.CreateUnique, so every level integrates with respect to a *fresh* + /// variable. The key (expr, x, integrateByParts) therefore differs at every level + /// even when the level is the same problem renamed, and can never + /// fire on a cycle. Neither could a set of shapes already visited, for the same reason — + /// the shapes are alpha-equivalent rather than equal. A depth bound does not care what + /// the levels are called. + /// + /// + /// The number, which is measured rather than picked. Instrumenting the descent over + /// twenty-three integrands chosen from the hard end of the corpus — the ones that take + /// substitutions, by-parts chains and partial fractions — the deepest any *answered* + /// integral reaches is 13, for x^5*cosh(x). Next are x^2*sqrt(5-x^2) + /// at 8 and e^(x^(1/3)) at 7; everything else sits at 4 or less. 32 is therefore + /// about two and a half times the deepest real descent seen. + /// + /// + /// Why not far more, since a bound only has to stop the overflow. Because this + /// descent branches: the remark on above records a single call + /// entering the integrator 5,330 times for 23 distinct integrands. Depth that is never + /// legitimately used is still searched before it is abandoned, so a bound of 64 stopped + /// the crash and left the run crawling. A bound has to be tight enough to be a bound. + /// + /// + /// Declining is a legitimate answer here and a wrong one is not: an unevaluated + /// integral(...) says "I could not settle this", which is true, where an aborted + /// process says nothing at all. + /// + /// + private const int DeepestDescent = 32; + + /// How deep the current descent is. Per thread, like . + [System.ThreadStatic] private static int descentDepth; + + + /// + /// Whether anything in the current top-level call gave up on + /// rather than on the mathematics. A null produced that way must not be cached as + /// "this cannot be integrated", because the same key may well be answerable when it is + /// not reached from so deep. + /// + [System.ThreadStatic] private static bool descentTruncated; + /// Does not add the constant of integration because this is called recursively. internal static Entity? ComputeIndefiniteIntegral(Entity expr, Entity.Variable x, bool integrateByParts = true) + { + if (descentDepth >= DeepestDescent) + { + descentTruncated = true; + return null; + } + if (descentDepth == 0) + descentTruncated = false; + descentDepth++; + try + { + return ComputeIndefiniteIntegralBounded(expr, x, integrateByParts); + } + finally + { + descentDepth--; + } + } + + private static Entity? ComputeIndefiniteIntegralBounded(Entity expr, Entity.Variable x, bool integrateByParts) { expr = Normalized(expr); @@ -195,7 +273,12 @@ private static Entity Normalized(Entity expr) => // Only kept if those settings still hold. Working the answer out runs simplification, // which opens scopes of its own; one still open means this answer was computed under // settings the stamp does not describe. - if (SettingsState.StillHolds(stamp)) + // A null that came of running out of descent, rather than of the mathematics, is not + // an answer about this integrand and must not be remembered as one — the same key + // reached from less deep may well be answerable. A non-null answer is kept whatever + // happened elsewhere, since an antiderivative that was found is correct regardless of + // how deep the search that found it went. + if (SettingsState.StillHolds(stamp) && (computed is not null || !descentTruncated)) { // Emptied rather than grown without end. Nothing here expires on its own — the // settings holding still is the whole condition for keeping an answer — so a