|
5 | 5 | // Website: https://am.angouri.org. |
6 | 6 | // |
7 | 7 |
|
| 8 | +using AngouriMath.Convenience; |
8 | 9 | using AngouriMath.Core.Transformations; |
9 | 10 | using AngouriMath.Extensions; |
10 | 11 | using AngouriMath.Functions.Algebra; |
@@ -109,10 +110,98 @@ internal static partial class Integration |
109 | 110 | private static Entity Normalized(Entity expr) => |
110 | 111 | expr.Replace(Patterns.GatherPowersOfOneBase); |
111 | 112 |
|
| 113 | + /// <summary> |
| 114 | + /// The integrals already answered under the settings in force, so that the same question |
| 115 | + /// is not worked out again. |
| 116 | + /// </summary> |
| 117 | + /// <remarks> |
| 118 | + /// <para> |
| 119 | + /// <b>Why an integral is asked for twice at all.</b> Two layers of it. Within one call the |
| 120 | + /// solvers overlap — substitution, partial fractions, splitting a sum and integration by |
| 121 | + /// parts each decompose the integrand differently and produce pieces that coincide — and |
| 122 | + /// across calls <c>Simplify</c> asks about one integral through every |
| 123 | + /// rewritten candidate it generates. Traced on <c>sin(x)/(x^2 + 1)^2</c>, which has no |
| 124 | + /// elementary antiderivative and so runs the search to exhaustion, that is <b>562 |
| 125 | + /// top-level calls for 3 distinct integrands</b>, one of them asked 500 times; on |
| 126 | + /// <c>e^x/(x^2 + 1)^2</c> a single call enters the integrator <b>5330 times for 23</b>. |
| 127 | + /// </para> |
| 128 | + /// <para> |
| 129 | + /// <b>The answers are held across calls and not only within one</b>, which is where the |
| 130 | + /// repetition is: discarding them at the end of each top-level call leaves the 562 to be |
| 131 | + /// paid in full. Measured on those three integrands, keeping them takes a |
| 132 | + /// <c>Simplify</c> from 115, 86 and 51 seconds to about 1.2, 1.5 and 0.2, and a single |
| 133 | + /// cold <c>Integrate</c> of <c>sqrt(tan(x))</c> from 155 ms to 79. |
| 134 | + /// https://github.com/asc-community/AngouriMath/issues/1156 |
| 135 | + /// </para> |
| 136 | + /// <para> |
| 137 | + /// <b>Why it is sound to hold them.</b> An answer depends on the ambient settings — |
| 138 | + /// <see cref="MathS.Settings.Codomain"/> decides whether the logarithms carry an |
| 139 | + /// <c>abs</c>, <c>MaxExpansionTermCount</c> bounds the by-parts recursion — and a setting |
| 140 | + /// is scoped to a flow rather than to a thread, so "the settings have not changed" is not |
| 141 | + /// something a per-thread cache can assume. <see cref="SettingsState"/> answers it |
| 142 | + /// exactly, by comparing what every setting reads as rather than by counting changes — |
| 143 | + /// which matters, because the library opens and closes thousands of balanced scopes while |
| 144 | + /// simplifying and a change count is therefore never still. |
| 145 | + /// </para> |
| 146 | + /// </remarks> |
| 147 | + [System.ThreadStatic] private static Dictionary<(Entity, Entity.Variable, bool), Entity?>? answered; |
| 148 | + |
| 149 | + /// <summary>The settings <see cref="answered"/> was filled under.</summary> |
| 150 | + [System.ThreadStatic] private static object?[]? answeredUnder; |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// How many answers to hold before starting over. Comfortably above what one call needs |
| 154 | + /// — the worst measured is 23 distinct integrands within a call and 3 across the |
| 155 | + /// candidates of one <c>Simplify</c> — and low enough that a process integrating |
| 156 | + /// different things all day does not accumulate them. |
| 157 | + /// </summary> |
| 158 | + private const int MostAnswersKept = 4096; |
| 159 | + |
112 | 160 | /// <summary>Does not add the constant of integration because this is called recursively.</summary> |
113 | 161 | internal static Entity? ComputeIndefiniteIntegral(Entity expr, Entity.Variable x, bool integrateByParts = true) |
114 | 162 | { |
115 | 163 | expr = Normalized(expr); |
| 164 | + |
| 165 | + if (answered is null || answeredUnder is null || !SettingsState.StillHolds(answeredUnder)) |
| 166 | + { |
| 167 | + // A fresh dictionary rather than Clear(), because this flow may have inherited |
| 168 | + // the reference from the one that started it, and emptying it in place would |
| 169 | + // empty that one's too. |
| 170 | + answered = new(); |
| 171 | + answeredUnder = SettingsState.Snapshot(); |
| 172 | + } |
| 173 | + |
| 174 | + // Held as locals over the recursive call, which reaches this method again and may |
| 175 | + // replace both fields on the way. Writing the answer into whatever the fields hold |
| 176 | + // afterwards would put it in a dictionary stamped with different settings. |
| 177 | + var into = answered; |
| 178 | + var stamp = answeredUnder; |
| 179 | + |
| 180 | + var key = (expr, x, integrateByParts); |
| 181 | + if (into.TryGetValue(key, out var already)) |
| 182 | + return already; |
| 183 | + |
| 184 | + var computed = ComputeIndefiniteIntegralUncached(expr, x, integrateByParts); |
| 185 | + |
| 186 | + // Only kept if those settings still hold. Working the answer out runs simplification, |
| 187 | + // which opens scopes of its own; one still open means this answer was computed under |
| 188 | + // settings the stamp does not describe. |
| 189 | + if (SettingsState.StillHolds(stamp)) |
| 190 | + { |
| 191 | + // Emptied rather than grown without end. Nothing here expires on its own — the |
| 192 | + // settings holding still is the whole condition for keeping an answer — so a |
| 193 | + // long-lived process integrating a stream of different expressions would hold |
| 194 | + // every one of them for ever. What this is for is being asked the same question |
| 195 | + // again, which needs the recent answers and not all of them. |
| 196 | + if (into.Count >= MostAnswersKept) |
| 197 | + into.Clear(); |
| 198 | + into[key] = computed; |
| 199 | + } |
| 200 | + return computed; |
| 201 | + } |
| 202 | + |
| 203 | + private static Entity? ComputeIndefiniteIntegralUncached(Entity expr, Entity.Variable x, bool integrateByParts) |
| 204 | + { |
116 | 205 | if (!expr.ContainsNode(x)) return expr * x; // base case, handle here |
117 | 206 | if ((IntegralPatterns.TryStandardIntegrals(expr, x)) is { } answer) return answer; |
118 | 207 | // The flag has to be handed on. Every one of these recurses, and a solver that |
|
0 commit comments