@@ -76,17 +76,25 @@ public Entity Integrate(Variable x, Entity from, Entity to) =>
7676 private static Entity ? AcrossTheRange ( Entity antiderivative , Variable x , Entity from , Entity to )
7777 {
7878 if ( from . Evaled is not Number . Real { IsNaN : false } lower || to . Evaled is not Number . Real { IsNaN : false } upper
79- || lower == upper
80- || ( lower < upper
81- ? AntiderivativeBreaks . Inside ( antiderivative , x , lower , upper )
82- : AntiderivativeBreaks . Inside ( antiderivative , x , upper , lower ) ) is not { Count : > 0 } breaks )
83- return AtTheBound ( antiderivative , x , to , from ) is { } atTheTop && AtTheBound ( antiderivative , x , from , to ) is { } atTheBottom
79+ || lower == upper )
80+ return AtTheBound ( antiderivative , x , to , from , continuousInside : false ) is { } atTheTop
81+ && AtTheBound ( antiderivative , x , from , to , continuousInside : false ) is { } atTheBottom
82+ ? atTheTop - atTheBottom
83+ : null ;
84+ var breaks = lower < upper
85+ ? AntiderivativeBreaks . Inside ( antiderivative , x , lower , upper )
86+ : AntiderivativeBreaks . Inside ( antiderivative , x , upper , lower ) ;
87+ if ( breaks is not { Count : > 0 } )
88+ // An empty list is a range the antiderivative was read to be continuous across; no list, one where a
89+ // break could not be listed.
90+ return AtTheBound ( antiderivative , x , to , from , continuousInside : breaks is not null ) is { } atTheTop
91+ && AtTheBound ( antiderivative , x , from , to , continuousInside : breaks is not null ) is { } atTheBottom
8492 ? atTheTop - atTheBottom
8593 : null ;
8694 if ( lower > upper )
8795 breaks . Reverse ( ) ;
8896 // Not across a break of an antiderivative in the incomplete gamma function: see AtTheBound.
89- if ( HoldsAnIncompleteGammaFunction ( antiderivative ) || AtTheBound ( antiderivative , x , from , to ) is not { } atTheStart )
97+ if ( HoldsAnIncompleteGammaFunction ( antiderivative ) || AtTheBound ( antiderivative , x , from , to , continuousInside : true ) is not { } atTheStart )
9098 return null ;
9199 var total = - atTheStart ;
92100 foreach ( var ( at , _) in breaks )
@@ -99,7 +107,7 @@ public Entity Integrate(Variable x, Entity from, Entity to) =>
99107 return null ;
100108 total += before - after ;
101109 }
102- return AtTheBound ( antiderivative , x , to , from ) is { } atTheEnd ? total + atTheEnd : null ;
110+ return AtTheBound ( antiderivative , x , to , from , continuousInside : true ) is { } atTheEnd ? total + atTheEnd : null ;
103111 }
104112
105113 private static bool HoldsAnIncompleteGammaFunction ( Entity expr )
@@ -125,12 +133,19 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
125133 /// undefined, the value stays undefined, as it is for <c>sin(x)</c> from 0 to <c>+oo</c>,
126134 /// whose antiderivative has no limit there. Where the limit is not decided there is no value,
127135 /// <see langword="null"/>, and the integral is left as written.
136+ /// <para>
137+ /// An infinite bound substituted can also leave an infinity standing inside the value, where the
138+ /// arithmetic cannot settle it because a parameter is in the way: <c>erf(+oo - y)</c>, the antiderivative
139+ /// of <c>e^(-(t - y)^2)</c> at <c>+oo</c>. That is not a value either, and the limit is taken there too
140+ /// (<paramref name="continuousInside"/> permitting; see <see cref="AtAnInfiniteBound"/>).
141+ /// https://github.com/asc-community/AngouriMath/issues/2451
142+ /// </para>
128143 /// </remarks>
129- private static Entity ? AtTheBound ( Entity antiderivative , Variable x , Entity bound , Entity otherBound )
144+ private static Entity ? AtTheBound ( Entity antiderivative , Variable x , Entity bound , Entity otherBound , bool continuousInside )
130145 {
131146 var value = antiderivative . Substitute ( x , bound ) ;
132147 if ( ! value . InnerSimplified . IsNaN )
133- return value ;
148+ return continuousInside && AtAnInfiniteBound ( antiderivative , x , bound , value ) is { } atInfinity ? atInfinity : value ;
134149 // The bounds' values, so that a bound written as pi or 2 pi is placed as well as 3.
135150 ApproachFrom ? side = ( bound . Evaled , otherBound . Evaled ) switch
136151 {
@@ -157,6 +172,56 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
157172 return limit . IsNaN ? value : limit ;
158173 }
159174
175+ /// <summary>
176+ /// The antiderivative's limit at an infinite bound, where substituting the bound left an infinity inside
177+ /// the value, or <see langword="null"/> to keep the substituted value as it was.
178+ /// </summary>
179+ /// <remarks>
180+ /// <para>
181+ /// <c>erf(+oo - y)</c> is left as it is by the arithmetic, since <c>y</c> could itself be infinite; but an
182+ /// integral with a parameter is a function of that parameter at each of its finite values, and the limit
183+ /// of <c>erf(t - y)</c> as <c>t</c> grows reads <c>y</c> as exactly that, a finite constant, and is 1. So
184+ /// <c>e^(-(t - y)^2)</c> over the line is <c>sqrt(pi)</c> rather than a difference of two unsettled error
185+ /// functions. https://github.com/asc-community/AngouriMath/issues/2451
186+ /// </para>
187+ /// <para>
188+ /// It is taken only where the antiderivative was read to be continuous across the range, so that the limit
189+ /// at the end is the whole integral and not the last piece of it: <c>1/(t^2 + y^2)</c>'s antiderivative
190+ /// for <c>y^2 < 0</c> is a logarithm with zeros at <c>t = ±sqrt(-y^2)</c>, which cannot be listed for a
191+ /// symbolic <c>y</c>, and taking its limits would answer 0, the principal value, where a concrete <c>y = i</c>
192+ /// answers <c>NaN</c>. What the breaks read is the real line with the parameters real: <c>arctan(t - y)</c>
193+ /// is continuous in <c>t</c> for every real <c>y</c>, but for <c>|Im y| > 1</c> it crosses arctan's cut,
194+ /// and the integral of <c>1/(1 + (t - y)^2)</c> over the line is 0 there and not <c>pi</c>. That is the
195+ /// answer at a concrete such <c>y</c> as well, and before this limit was taken; it is the antiderivative's
196+ /// continuity that is not read off the real line, not this limit.
197+ /// </para>
198+ /// <para>
199+ /// A limit that is undecided, undefined, or still holds an infinity inside it -- <c>e^(-y t)</c> at
200+ /// <c>+oo</c> depends on the sign of <c>y</c>, and the limit says so by staying <c>e^(+oo * -y)</c> -- leaves
201+ /// the substituted value as it was.
202+ /// </para>
203+ /// </remarks>
204+ private static Entity ? AtAnInfiniteBound ( Entity antiderivative , Variable x , Entity bound , Entity value )
205+ {
206+ if ( bound . Evaled is not Number . Real { EDecimal : var at } || ! at . IsInfinity ( ) )
207+ return null ;
208+ var settled = value . InnerSimplified ;
209+ if ( settled is Number || ! HoldsAnInfinity ( settled ) || HoldsAnIncompleteGammaFunction ( antiderivative ) )
210+ return null ;
211+ var limit = LimitAtTheBound ( antiderivative , x , bound , at . IsPositiveInfinity ( ) ? ApproachFrom . Left : ApproachFrom . Right ) ;
212+ if ( limit . IsNaN || limit . Nodes . Any ( node => node is Limitf ) || ( limit is not Number && HoldsAnInfinity ( limit ) ) )
213+ return null ;
214+ return limit ;
215+ }
216+
217+ private static bool HoldsAnInfinity ( Entity expr )
218+ {
219+ foreach ( var node in expr . Nodes )
220+ if ( node is Number . Complex number && ( ! number . RealPart . EDecimal . IsFinite || ! number . ImaginaryPart . EDecimal . IsFinite ) )
221+ return true ;
222+ return false ;
223+ }
224+
160225 /// <summary>
161226 /// The antiderivative's limit at a bound, remembered under the settings it was taken in, as
162227 /// the cache of answers remembers an antiderivative. A definite integral left as written is evaluated
0 commit comments