@@ -172,6 +172,239 @@ private static bool PeriodicZeros(Entity argument, Variable x, Real lower, Real
172172 return true ;
173173 }
174174
175+ /// <summary>
176+ /// The points of <c>[<paramref name="lower"/>, <paramref name="upper"/>]</c> where a node of
177+ /// <paramref name="antiderivative"/> whose argument leaves the real line crosses its branch cut, each with the
178+ /// node, the point, its decimal value and the unit its jump there is a whole multiple of; an empty list where no
179+ /// argument leaves the real line, or none crosses a cut; and <see langword="null"/> where a crossing cannot be
180+ /// ruled out or its jump cannot be settled. Bounds that are not numbers are passed as <see langword="null"/>, and
181+ /// any crossing at all is then one that cannot be placed.
182+ /// </summary>
183+ /// <remarks>
184+ /// <para>
185+ /// <see cref="Inside"/> reads the real line with the parameters real, where <c>arctan</c> and the logarithm of a
186+ /// real argument have no cut to cross. A complex constant takes the argument off the line, and the path then
187+ /// can cross a cut where the integrand is continuous: <c>arctan(t - 3i)</c>, the antiderivative of
188+ /// <c>1/(1 + (t - 3i)^2)</c>, meets the imaginary axis at <c>-3i</c>, on <c>arctan</c>'s cut, and jumps by
189+ /// <c>pi</c> there, so its values at -2 and 2 differ by <c>pi</c> more than the integral between them does.
190+ /// https://github.com/asc-community/AngouriMath/issues/2460
191+ /// </para>
192+ /// <para>
193+ /// The cuts placed are <c>arctan</c>'s, the imaginary axis beyond <c>±i</c>, and the logarithm's, the negative
194+ /// real axis: for an argument that is a polynomial with numeric coefficients, where its real or its imaginary
195+ /// part, a real polynomial, is zero; and for the logarithm of the cosine or the sine of <c>a t + b</c>, which
196+ /// <c>tan</c> and <c>cot</c> integrate to, where its imaginary part is zero (see
197+ /// <see cref="TrigonometricCrossings"/>). Their jumps are whole multiples of <c>pi</c> and of
198+ /// <c>2 pi i / ln(base)</c>, and which multiple is read from the values a little either side. A power with an
199+ /// argument off the line is taken where its base does not cross the negative real axis, since its jump there
200+ /// is not a constant; a tangent, secant, cotangent or cosecant where its argument does not meet the real line,
201+ /// where its poles are. Anything else with an argument off the line is not read at all.
202+ /// </para>
203+ /// </remarks>
204+ internal static List < ( Entity Node , Entity At , Real Value , Entity Unit ) > ? CutCrossings ( Entity antiderivative , Variable x , Real ? lower , Real ? upper )
205+ {
206+ var crossings = new List < ( Entity Node , Entity At , Real Value , Entity Unit ) > ( ) ;
207+ var read = new HashSet < Entity > ( ) ;
208+ foreach ( var node in antiderivative . Nodes )
209+ {
210+ if ( ! read . Add ( node ) || ! node . DirectChildren . Any ( child => child . ContainsNode ( x ) && OffTheRealLine ( child ) ) )
211+ continue ;
212+ var settled = node switch
213+ {
214+ Sumf or Minusf or Mulf or Divf or Absf or Signumf or Sinf or Cosf
215+ or Erff or Erfcf or Erfif or Sif or Shif or Conjugatef or Ref or Imf => true ,
216+ // b^g = e^(g ln b) for a constant b, and an integer power of anything, are continuous wherever their
217+ // argument is.
218+ Powf ( var @base , _) when ! @base . ContainsNode ( x ) || ! OffTheRealLine ( @base ) => true ,
219+ Powf ( _, Integer ) => true ,
220+ Powf ( var @base , _) => PolynomialCrossings ( @base , x , imaginaryAxis : false ) is { } points
221+ && points . All ( point => lower is not null && upper is not null && ( point . Value < lower || point . Value > upper ) ) ,
222+ Tanf ( var argument ) => MeetsTheRealLine ( argument , x ) is false ,
223+ Secantf ( var argument ) => MeetsTheRealLine ( argument , x ) is false ,
224+ Cotanf ( var argument ) => MeetsTheRealLine ( argument , x ) is false ,
225+ Cosecantf ( var argument ) => MeetsTheRealLine ( argument , x ) is false ,
226+ Arctanf ( var argument ) => Add ( node , PolynomialCrossings ( argument , x , imaginaryAxis : true ) , MathS . pi ) ,
227+ Logf ( var @base , Cosf ( var argument ) ) when ! @base . ContainsNode ( x ) =>
228+ Add ( node , TrigonometricCrossings ( argument , x , lower , upper , cosine : true ) , 2 * MathS . pi * MathS . i / MathS . Ln ( @base ) ) ,
229+ Logf ( var @base , Sinf ( var argument ) ) when ! @base . ContainsNode ( x ) =>
230+ Add ( node , TrigonometricCrossings ( argument , x , lower , upper , cosine : false ) , 2 * MathS . pi * MathS . i / MathS . Ln ( @base ) ) ,
231+ Logf ( var @base , var argument ) when ! @base . ContainsNode ( x ) =>
232+ Add ( node , PolynomialCrossings ( argument , x , imaginaryAxis : false ) , 2 * MathS . pi * MathS . i / MathS . Ln ( @base ) ) ,
233+ _ => false
234+ } ;
235+ if ( ! settled )
236+ return null ;
237+ }
238+ return crossings ;
239+
240+ bool Add ( Entity node , List < ( Entity At , Real Value ) > ? points , Entity unit )
241+ {
242+ if ( points is null )
243+ return false ;
244+ foreach ( var ( at , value ) in points )
245+ {
246+ if ( lower is null || upper is null )
247+ return false ;
248+ if ( value >= lower && value <= upper )
249+ crossings . Add ( ( node , at , value , unit ) ) ;
250+ }
251+ return true ;
252+ }
253+ }
254+
255+ /// <summary>Whether <paramref name="expression"/> holds a constant that is not real.</summary>
256+ private static bool OffTheRealLine ( Entity expression )
257+ {
258+ foreach ( var node in expression . Nodes )
259+ if ( node is Complex { ImaginaryPart . IsZero : false } )
260+ return true ;
261+ return false ;
262+ }
263+
264+ /// <summary>
265+ /// Whether the linear <paramref name="argument"/> takes a real value at a real <paramref name="x"/>, or
266+ /// <see langword="null"/> where it is not linear with numeric coefficients.
267+ /// </summary>
268+ private static bool ? MeetsTheRealLine ( Entity argument , Variable x )
269+ => TreeAnalyzer . TryGetPolyLinear ( argument , x , out var a , out var b )
270+ && a . Evaled is Complex { RealPart . EDecimal . IsFinite : true , ImaginaryPart . EDecimal . IsFinite : true } slope
271+ && b . Evaled is Complex { RealPart . EDecimal . IsFinite : true , ImaginaryPart . EDecimal . IsFinite : true } intercept
272+ ? ! slope . ImaginaryPart . IsZero || intercept . ImaginaryPart . IsZero
273+ : null ;
274+
275+ /// <summary>
276+ /// The real <paramref name="x"/> where the polynomial <paramref name="argument"/> crosses the imaginary axis
277+ /// beyond <c>±i</c> (<paramref name="imaginaryAxis"/>) or the negative real axis, or <see langword="null"/> where
278+ /// it is not a polynomial with numeric coefficients, its crossings cannot be listed, it runs along the axis, or it
279+ /// meets the end of the cut, which is a singularity of the integrand.
280+ /// </summary>
281+ private static List < ( Entity At , Real Value ) > ? PolynomialCrossings ( Entity argument , Variable x , bool imaginaryAxis )
282+ {
283+ if ( ! TreeAnalyzer . TryGetPolynomial ( argument , x , out var coefficients ) )
284+ return null ;
285+ // The part that is zero on the axis, and the one that decides whether a point of it is on the cut.
286+ Entity across = Integer . Zero , along = Integer . Zero ;
287+ var acrossIsZero = true ;
288+ foreach ( var pair in coefficients )
289+ {
290+ var ( power , coefficient ) = ( pair . Key , pair . Value ) ;
291+ if ( power . Sign < 0 || coefficient . Evaled is not Complex { RealPart . EDecimal . IsFinite : true , ImaginaryPart . EDecimal . IsFinite : true } value )
292+ return null ;
293+ var ( acrossPart , alongPart ) = imaginaryAxis ? ( value . RealPart , value . ImaginaryPart ) : ( value . ImaginaryPart , value . RealPart ) ;
294+ if ( ! acrossPart . IsZero && ! power . IsZero )
295+ acrossIsZero = false ;
296+ // x^0 is written as 1, since 0^0 is undefined here and would take the zero at 0 out of the solution.
297+ Entity monomial = power . IsZero ? Integer . One : MathS . Pow ( x , Integer . Create ( power ) ) ;
298+ if ( ! acrossPart . IsZero )
299+ across += acrossPart * monomial ;
300+ if ( ! alongPart . IsZero )
301+ along += alongPart * monomial ;
302+ }
303+ // Constant across the axis: off it everywhere, or along it everywhere.
304+ if ( acrossIsZero )
305+ return across . Evaled is Real { IsZero : true } ? null : new ( ) ;
306+ if ( across . InnerSimplified . SolveEquation ( x ) is not FiniteSet roots )
307+ return null ;
308+ var points = new List < ( Entity At , Real Value ) > ( ) ;
309+ foreach ( var root in roots )
310+ {
311+ if ( root . ContainsNode ( x ) || root . Vars . Any ( ) )
312+ return null ;
313+ switch ( root . Evaled )
314+ {
315+ case Real { EDecimal . IsFinite : true } at :
316+ if ( along . Substitute ( x , root ) . Evaled is not Real { EDecimal . IsFinite : true } there )
317+ return null ;
318+ var onTheCut = imaginaryAxis ? there > 1 || there < - 1 : there < 0 ;
319+ var atItsEnd = imaginaryAxis ? there >= 1 && there <= 1 || there <= - 1 && there >= - 1 : there . IsZero ;
320+ if ( atItsEnd )
321+ return null ;
322+ if ( onTheCut )
323+ points . Add ( ( root , at ) ) ;
324+ break ;
325+ case Complex { ImaginaryPart . IsZero : false } :
326+ break ;
327+ default :
328+ return null ;
329+ }
330+ }
331+ return points ;
332+ }
333+
334+ /// <summary>
335+ /// The real <paramref name="x"/> in the range where the cosine (<paramref name="cosine"/>) or the sine of
336+ /// <c>a x + b</c>, with <c>a</c> and <c>b</c> complex numbers, crosses the negative real axis, or
337+ /// <see langword="null"/> where the argument is not of that shape, the range is not finite where it would have
338+ /// to be, or the path meets a zero.
339+ /// </summary>
340+ /// <remarks>
341+ /// With <c>p = Re(a x + b)</c> and <c>q = Im(a x + b)</c>, both linear in <c>x</c>,
342+ /// <c>cos(p + i q) = cos p cosh q - i sin p sinh q</c> is real where <c>sin p = 0</c> or <c>q = 0</c>: at the
343+ /// multiples of <c>pi</c>, negative at the odd ones, and at the one point where <c>q = 0</c>, negative where
344+ /// <c>cos p</c> is there. <c>sin(p + i q) = sin p cosh q + i cos p sinh q</c> is real where <c>cos p = 0</c> or
345+ /// <c>q = 0</c>: negative at <c>-pi/2 + 2 k pi</c>, and where <c>sin p</c> is at the point <c>q = 0</c>. A point
346+ /// where the value touches the axis without crossing it jumps by no multiple, which the caller reads.
347+ /// </remarks>
348+ private static List < ( Entity At , Real Value ) > ? TrigonometricCrossings ( Entity argument , Variable x , Real ? lower , Real ? upper , bool cosine )
349+ {
350+ if ( lower is null || upper is null
351+ || ! TreeAnalyzer . TryGetPolyLinear ( argument , x , out var a , out var b )
352+ || a . Evaled is not Complex { RealPart . EDecimal . IsFinite : true , ImaginaryPart . EDecimal . IsFinite : true } slope
353+ || b . Evaled is not Complex { RealPart . EDecimal . IsFinite : true , ImaginaryPart . EDecimal . IsFinite : true } intercept
354+ || slope . RealPart . IsZero && slope . ImaginaryPart . IsZero
355+ || MathS . pi . Evaled is not Real pi )
356+ return null ;
357+ var points = new List < ( Entity At , Real Value ) > ( ) ;
358+ // Where q = 0, if q moves at all: the value there is the real cos p or sin p.
359+ if ( ! slope . ImaginaryPart . IsZero )
360+ {
361+ var at = ( - ( Entity ) intercept . ImaginaryPart / slope . ImaginaryPart ) . InnerSimplified ;
362+ if ( at . Evaled is not Real { EDecimal . IsFinite : true } value )
363+ return null ;
364+ if ( value >= lower && value <= upper )
365+ {
366+ var p = ( slope . RealPart * ( Entity ) at + intercept . RealPart ) . InnerSimplified ;
367+ if ( ( cosine ? MathS . Cos ( p ) : MathS . Sin ( p ) ) . EvalNumerical ( ) is not { ImaginaryPart . IsZero : true , RealPart : var there } )
368+ return null ;
369+ // A zero on the path is a pole of the integrand's tan or cot.
370+ if ( there . EDecimal . Abs ( ) . CompareTo ( EDecimal . Create ( 1 , - 30 ) ) < 0 )
371+ return null ;
372+ if ( there < 0 )
373+ points . Add ( ( at , value ) ) ;
374+ }
375+ }
376+ // Where sin p = 0, or cos p = 0, if p moves at all; if it does not, q does and the point above is all.
377+ if ( ! slope . RealPart . IsZero )
378+ {
379+ if ( ! lower . EDecimal . IsFinite || ! upper . EDecimal . IsFinite )
380+ return null ;
381+ // p = c + 2 k pi, so k runs over the whole numbers between the ends mapped back.
382+ Entity first = cosine ? MathS . pi : - MathS . pi / 2 ;
383+ var shift = cosine ? pi : - pi / 2 ;
384+ var atLower = ( slope . RealPart * lower + intercept . RealPart - shift ) / ( 2 * pi ) ;
385+ var atUpper = ( slope . RealPart * upper + intercept . RealPart - shift ) / ( 2 * pi ) ;
386+ var ( from , to ) = atLower < atUpper ? ( atLower , atUpper ) : ( atUpper , atLower ) ;
387+ var k0 = from . EDecimal . RoundToIntegerNoRoundedFlag ( EContext . CliDecimal . WithRounding ( ERounding . Floor ) ) . ToEInteger ( ) ;
388+ var k1 = to . EDecimal . RoundToIntegerNoRoundedFlag ( EContext . CliDecimal . WithRounding ( ERounding . Ceiling ) ) . ToEInteger ( ) ;
389+ if ( ( k1 - k0 ) . CompareTo ( EInteger . FromInt32 ( MostBreaks ) ) > 0 )
390+ return null ;
391+ for ( var k = k0 ; k . CompareTo ( k1 ) <= 0 ; k += EInteger . One )
392+ {
393+ var at = ( ( first + 2 * Integer . Create ( k ) * MathS . pi - intercept . RealPart ) / slope . RealPart ) . InnerSimplified ;
394+ if ( at . Evaled is not Real { EDecimal . IsFinite : true } value )
395+ return null ;
396+ // The same point as the one where q = 0 is one crossing.
397+ if ( value >= lower && value <= upper && ! points . Any ( point => Coincide ( point . Value , value ) ) )
398+ points . Add ( ( at , value ) ) ;
399+ }
400+ }
401+ else if ( ( cosine ? MathS . Sin ( intercept . RealPart ) : MathS . Cos ( intercept . RealPart ) ) . EvalNumerical ( ) is not { RealPart : var constant }
402+ || constant . EDecimal . Abs ( ) . CompareTo ( EDecimal . Create ( 1 , - 30 ) ) < 0 )
403+ // p is constant at a point where the value is real all along the path.
404+ return null ;
405+ return points ;
406+ }
407+
175408 /// <summary>
176409 /// Two decimal values of the same point, reached by different exact spellings.
177410 /// </summary>
0 commit comments