@@ -230,6 +230,188 @@ static bool OnAJump(Entity argument, System.Func<Real, bool> jump)
230230 }
231231 }
232232
233+ /// <summary>
234+ /// The other shapes the squeeze theorem's premise settles, where the bounded part is also
235+ /// kept away from 0, or is a term rather than a factor: a part vanishing over one bounded
236+ /// away from 0 tends to 0, a part diverging times or over one bounded away from 0 diverges
237+ /// with the sign the product takes, and a diverging term beside a bounded one diverges.
238+ /// </summary>
239+ /// <remarks>
240+ /// <c>e^x / e^(cos(x))</c> at -oo is 0, and was answered NaN: the descent put
241+ /// <c>e^(cos(x))</c>'s own limit, which does not exist, in place of the divisor, and a
242+ /// quotient of a limit and no limit read as no limit. The divisor has no limit and still
243+ /// lies in <c>[1/e, e]</c> all the way in, which is all the quotient needs of it. The
244+ /// bound is read off the shape (<see cref="Enclosure"/>), never off sampled values, and
245+ /// where the enclosure reaches 0 nothing is claimed: <c>x / (1 + cos(x))</c> stays
246+ /// unanswered, as it has poles all the way in.
247+ /// </remarks>
248+ internal static Entity ? SolveWithABoundedPart ( Entity expr , Variable x )
249+ {
250+ // Not over an expression with an infinity or a NaN written in it. Such a part "diverges"
251+ // only because the infinity is already there: tanh(log(x + 1, 0)) at -1 from the right
252+ // reaches e^(+oo / ln(x)) - 1, which the descent answers +oo, and its quotient by
253+ // e^(+oo / ln(x)) + 1, read as +oo / +oo, was answered 0 where the values settle at 1.
254+ if ( expr . Nodes . Any ( static node => node is Number { IsFinite : false } ) )
255+ return null ;
256+ switch ( expr )
257+ {
258+ case Mulf ( var multiplier , var multiplicand ) :
259+ return DivergingTimesAwayFromZero ( multiplicand , multiplier , x )
260+ ?? DivergingTimesAwayFromZero ( multiplier , multiplicand , x ) ;
261+ case Divf ( var dividend , var divisor ) :
262+ if ( SignAwayFromZero ( divisor , x ) is not { } divisorSign )
263+ return null ;
264+ // Only a real-valued dividend: 0 swallows a side that is not real, which has no
265+ // limit under the convention x^x at 0 is held to. sinh(-1) (x^x - 1) at 0 is read
266+ // from the left as ((-1/x)^(-1/x) - 1) (1 - e^2) / e, and came to 0 two-sided.
267+ if ( IsRealValued ( dividend , x ) && VanishesAtInfinity ( dividend , x ) )
268+ return Integer . Zero ;
269+ return InfinityAtInfinity ( dividend , x ) is { } dividendInfinity
270+ ? Signed ( dividendInfinity , divisorSign )
271+ : null ;
272+ case Sumf ( var augend , var addend ) :
273+ return DivergingBesideBounded ( augend , addend , x , 1 )
274+ ?? DivergingBesideBounded ( addend , augend , x , 1 ) ;
275+ case Minusf ( var minuend , var subtrahend ) :
276+ return DivergingBesideBounded ( minuend , subtrahend , x , 1 )
277+ ?? DivergingBesideBounded ( subtrahend , minuend , x , - 1 ) ;
278+ default :
279+ return null ;
280+ }
281+
282+ static Entity ? DivergingTimesAwayFromZero ( Entity diverging , Entity bounded , Variable x )
283+ => SignAwayFromZero ( bounded , x ) is { } sign && InfinityAtInfinity ( diverging , x ) is { } infinity
284+ ? Signed ( infinity , sign )
285+ : null ;
286+
287+ // The diverging part, taken with the sign it carries in the sum, beside a bounded one.
288+ static Entity ? DivergingBesideBounded ( Entity diverging , Entity bounded , Variable x , int sign )
289+ => diverging . ContainsNode ( x ) && Enclosure ( bounded , x ) is { } && InfinityAtInfinity ( diverging , x ) is { } infinity
290+ ? Signed ( infinity , sign )
291+ : null ;
292+
293+ static Entity Signed ( Real infinity , int sign )
294+ => ( infinity == Real . PositiveInfinity ) == ( sign > 0 ) ? Real . PositiveInfinity : Real . NegativeInfinity ;
295+ }
296+
297+ /// <summary>
298+ /// The sign of an expression whose <see cref="Enclosure"/> excludes 0, or
299+ /// <see langword="null"/> where it does not or there is none. A constant counts:
300+ /// x - cos(x) at -oo is read at +oo as -1 * (x + cos(x)).
301+ /// </summary>
302+ private static int ? SignAwayFromZero ( Entity expr , Variable x )
303+ => Enclosure ( expr , x ) is var ( low , high )
304+ ? low > 0 ? 1 : high < 0 ? - 1 : null
305+ : null ;
306+
307+ /// <summary>
308+ /// The real infinity the expression tends to, where it is real on the way in, or
309+ /// <see langword="null"/>.
310+ /// </summary>
311+ /// <remarks>
312+ /// Real on the way in, and not only in its limit: a side on which the expression is not
313+ /// real has no limit under the convention x^x at 0 is held to, and a bounded part beside
314+ /// it must not give it one. tanh(x^cos(2)) at 0 from the left is read at +oo through
315+ /// (-1/x)^cos(2), complex for every x, and came to 1, and two-sided to 1 where it is NaN.
316+ /// </remarks>
317+ private static Real ? InfinityAtInfinity ( Entity expr , Variable x )
318+ => IsRealNearInfinity ( expr , x )
319+ && LimitFunctional . ComputeLimit ( expr , x , Real . PositiveInfinity ) is { } limit
320+ && limit . Evaled is Real { IsFinite : false , IsNaN : false } infinity
321+ ? infinity
322+ : null ;
323+
324+ /// <summary>
325+ /// Whether the expression is real for every large enough x: real wherever it is defined,
326+ /// or real where the bases of its roots and the arguments of its logarithms are positive,
327+ /// and each of those tends to a positive number or to +oo. One tending to 0 is declined,
328+ /// since it says nothing about the side it arrives from.
329+ /// </summary>
330+ private static bool IsRealNearInfinity ( Entity expr , Variable x )
331+ => IsRealValued ( expr , x )
332+ || TreeAnalyzer . WhereRealValued ( expr , x ) is { } where
333+ && where . Nodes . OfType < Greaterf > ( ) . ToList ( ) is { Count : > 0 } conditions
334+ && conditions . All ( condition =>
335+ LimitFunctional . ComputeLimit ( condition . Left , x , Real . PositiveInfinity ) is { } limit
336+ && limit . Evaled is Real { IsNaN : false , IsPositive : true , IsZero : false } ) ;
337+
338+ /// <summary>
339+ /// An interval the expression stays within for every real x, read off its shape: a
340+ /// sine, a cosine, a sign and an error function of a real argument lie in [-1, 1], an
341+ /// arctangent and an arccotangent in [-pi/2, pi/2], a positive constant to a power is
342+ /// monotone in the power, and sums, differences, products, quotients by an interval
343+ /// without 0, moduli and positive whole powers combine intervals. <see langword="null"/>
344+ /// for any other shape, including anything an x appears in bare: nothing is sampled.
345+ /// </summary>
346+ /// <remarks>
347+ /// In doubles, and widened outward by a relative 1e-12 at every step, so that rounding
348+ /// can only make the interval larger: 1 + cos(x) comes out reaching just below 0, and so
349+ /// is not taken to be bounded away from it, which it is not.
350+ /// </remarks>
351+ private static ( double Low , double High ) ? Enclosure ( Entity expr , Variable x )
352+ {
353+ if ( ! expr . ContainsNode ( x ) )
354+ return expr . Evaled is Real { IsFinite : true , IsNaN : false } value
355+ ? Widened ( value . AsDouble ( ) , value . AsDouble ( ) )
356+ : null ;
357+ switch ( expr )
358+ {
359+ case Sinf or Cosf or Signumf or Erff when IsRealValued ( expr . DirectChildren [ 0 ] , x ) :
360+ return ( - 1 , 1 ) ;
361+ case Arctanf or Arccotanf when IsRealValued ( expr . DirectChildren [ 0 ] , x ) :
362+ return Widened ( - System . Math . PI / 2 , System . Math . PI / 2 ) ;
363+ case Absf ( var argument ) :
364+ if ( Enclosure ( argument , x ) is not var ( low , high ) )
365+ return null ;
366+ return Widened ( low > 0 ? low : high < 0 ? - high : 0 , System . Math . Max ( System . Math . Abs ( low ) , System . Math . Abs ( high ) ) ) ;
367+ case Sumf ( var augend , var addend ) :
368+ if ( Enclosure ( augend , x ) is not var ( augendLow , augendHigh ) || Enclosure ( addend , x ) is not var ( addendLow , addendHigh ) )
369+ return null ;
370+ return Widened ( augendLow + addendLow , augendHigh + addendHigh ) ;
371+ case Minusf ( var minuend , var subtrahend ) :
372+ if ( Enclosure ( minuend , x ) is not var ( minuendLow , minuendHigh ) || Enclosure ( subtrahend , x ) is not var ( subtrahendLow , subtrahendHigh ) )
373+ return null ;
374+ return Widened ( minuendLow - subtrahendHigh , minuendHigh - subtrahendLow ) ;
375+ case Mulf ( var multiplier , var multiplicand ) :
376+ return Enclosure ( multiplier , x ) is { } left && Enclosure ( multiplicand , x ) is { } right
377+ ? Product ( left , right ) : null ;
378+ case Divf ( var dividend , var divisor ) :
379+ if ( Enclosure ( dividend , x ) is not { } over || Enclosure ( divisor , x ) is not var ( divisorLow , divisorHigh )
380+ || divisorLow <= 0 && divisorHigh >= 0 )
381+ return null ;
382+ return Product ( over , Widened ( 1 / divisorHigh , 1 / divisorLow ) ) ;
383+ // e^g, and any positive constant to a power, is monotone in the power.
384+ case Powf ( var @base , var exponent ) when ! @base . ContainsNode ( x ) :
385+ if ( @base . Evaled is not Real { IsFinite : true , IsPositive : true } positive
386+ || Enclosure ( exponent , x ) is not var ( exponentLow , exponentHigh ) )
387+ return null ;
388+ var ( atLow , atHigh ) = ( System . Math . Pow ( positive . AsDouble ( ) , exponentLow ) , System . Math . Pow ( positive . AsDouble ( ) , exponentHigh ) ) ;
389+ return IsAFiniteDouble ( atLow ) && IsAFiniteDouble ( atHigh )
390+ ? Widened ( System . Math . Min ( atLow , atHigh ) , System . Math . Max ( atLow , atHigh ) ) : null ;
391+ case Powf ( var @base , Integer { EInteger : var power } ) when power . Sign > 0 && power . CompareTo ( 64 ) <= 0 :
392+ if ( Enclosure ( @base , x ) is not { } baseEnclosure )
393+ return null ;
394+ var result = baseEnclosure ;
395+ for ( var i = 1 ; i < power . ToInt32Checked ( ) ; i ++ )
396+ result = Product ( result , baseEnclosure ) ;
397+ return IsAFiniteDouble ( result . Low ) && IsAFiniteDouble ( result . High ) ? result : null ;
398+ default :
399+ return null ;
400+ }
401+
402+ // double.IsFinite is not on netstandard2.0, which the library ships.
403+ static bool IsAFiniteDouble ( double value ) => ! double . IsNaN ( value ) && ! double . IsInfinity ( value ) ;
404+
405+ static ( double Low , double High ) Product ( ( double Low , double High ) left , ( double Low , double High ) right )
406+ {
407+ var corners = new [ ] { left . Low * right . Low , left . Low * right . High , left . High * right . Low , left . High * right . High } ;
408+ return Widened ( corners . Min ( ) , corners . Max ( ) ) ;
409+ }
410+
411+ static ( double Low , double High ) Widened ( double low , double high )
412+ => ( low - 1e-12 * ( 1 + System . Math . Abs ( low ) ) , high + 1e-12 * ( 1 + System . Math . Abs ( high ) ) ) ;
413+ }
414+
233415 /// <summary>
234416 /// That a sine or a cosine has no limit where its argument grows without bound: it takes
235417 /// every value in its range infinitely often on the way in and settles on none of them.
@@ -329,7 +511,7 @@ private static bool IsBoundedAtInfinity(Entity expr, Variable x)
329511 case Minusf ( var minuend , var subtrahend ) :
330512 return IsBoundedAtInfinity ( minuend , x ) && IsBoundedAtInfinity ( subtrahend , x ) ;
331513 default :
332- return false ;
514+ return Enclosure ( expr , x ) is { } ;
333515 }
334516 }
335517
0 commit comments