@@ -68,6 +68,7 @@ internal static Entity AntiderivativeLog(Entity arg)
6868 Entity . Shif ( var arg ) => arg ,
6969 Entity . Chif ( var arg ) => arg ,
7070 Entity . Polygammaf ( var order , var arg ) when ! order . ContainsNode ( x ) => arg ,
71+ Entity . IncompleteGammaf ( var order , var arg ) when ! order . ContainsNode ( x ) => arg ,
7172 Entity . Arcsinf ( var arg ) => arg ,
7273 Entity . Arccosf ( var arg ) => arg ,
7374 Entity . Arctanf ( var arg ) => arg ,
@@ -268,6 +269,188 @@ private static bool TakeMomentFactor(Entity factor, bool inverted, Entity.Variab
268269 _ => MathS . Pow ( x , k + 1 ) * bell / ( k + 1 ) + ( - 2 * A / ( k + 1 ) ) . InnerSimplified * Moment ( k + 2 , A , bell , x ) ,
269270 } ;
270271
272+ /// <summary>
273+ /// <c>int P Gamma(n, u) dx</c> for the upper incomplete gamma function of a linear
274+ /// <c>u = a + b x</c> with <c>n</c> free of <c>x</c>, where <c>P</c> is a polynomial in
275+ /// <c>x</c>, or, without an offset <c>a</c>, a power <c>(d x)^m</c> for any <c>m</c> but -1.
276+ /// By parts against an antiderivative <c>Q</c> of <c>P</c>: the derivative of
277+ /// <c>Gamma(n, u)</c> is <c>-b u^(n - 1) e^(-u)</c>, and with <c>Q</c> written in powers of
278+ /// <c>u</c>, each <c>u^k</c> lands on <c>u^(n + k - 1) e^(-u)</c>, the derivative in <c>u</c>
279+ /// of <c>-Gamma(n + k, u)</c>. Rubi's 8.6.
280+ /// https://github.com/asc-community/AngouriMath/issues/2075
281+ /// </summary>
282+ private static Entity ? IncompleteGammaBesideAPower ( Entity expr , Entity . Variable x )
283+ {
284+ // Asked of every integrand that reaches the table, so a type test comes first, and
285+ // nothing here captures a local.
286+ if ( expr is not ( Entity . Mulf or Entity . Divf or Entity . IncompleteGammaf ) )
287+ return null ;
288+ var ( numerator , denominator ) = expr is Entity . Divf ( var n , var d ) ? ( n , d ) : ( expr , ( Entity ) Entity . Number . Integer . One ) ;
289+ Entity coefficient = Entity . Number . Integer . One ;
290+ Entity . IncompleteGammaf ? gamma = null ;
291+ Entity ? above = null ;
292+ Entity ? below = null ;
293+ foreach ( var factor in Entity . Mulf . LinearChildren ( numerator ) )
294+ if ( ! factor . ContainsNode ( x ) )
295+ coefficient *= factor ;
296+ else if ( gamma is null && factor is Entity . IncompleteGammaf incompleteGamma )
297+ gamma = incompleteGamma ;
298+ else
299+ above = above is null ? factor : above * factor ;
300+ foreach ( var factor in Entity . Mulf . LinearChildren ( denominator ) )
301+ if ( ! factor . ContainsNode ( x ) )
302+ coefficient /= factor ;
303+ else if ( below is null )
304+ below = factor ;
305+ else
306+ return null ;
307+ if ( gamma is null || gamma . Parameter . ContainsNode ( x )
308+ || ! TreeAnalyzer . TryGetPolyLinear ( gamma . LowerLimit , x , out var slope , out var offset ) )
309+ return null ;
310+ var withoutAnOffset = TreeAnalyzer . IsZero ( offset ) ;
311+ if ( below is null && ThePowersOf ( above , x , withoutAnOffset ? 100 : 32 ) is { } inX )
312+ return ( withoutAnOffset
313+ ? BesideAPolynomialWithoutAnOffset ( inX , gamma , slope , x )
314+ : BesideAPolynomial ( above ?? Entity . Number . Integer . One , gamma , slope , offset , x ) ) is { } answer
315+ ? coefficient * answer
316+ : null ;
317+ if ( ! withoutAnOffset )
318+ return null ;
319+
320+ // A power of a multiple of x, of any exponent but -1, whose integral is not a gamma
321+ // function: x^m Gamma(n, b x) is x^(m + 1) Gamma(n, b x)/(m + 1) - x^m (b x)^(-m)
322+ // Gamma(m + n + 1, b x)/(b (m + 1)), and the quotient x^m/(b x)^m is b^(-m) for a whole m
323+ // only: elsewhere the two powers take their branches separately, and it is constant only
324+ // between the cuts.
325+ Entity written ;
326+ Entity @base ;
327+ Entity scale ;
328+ Entity m ;
329+ if ( below is null && above is not null && TryReadAPowerOfAMultiple ( above , x , out @base , out scale , out m ) )
330+ written = above ;
331+ else if ( above is null && below is not null && TryReadAPowerOfAMultiple ( below , x , out @base , out scale , out var mBelow ) )
332+ {
333+ written = 1 / below ;
334+ m = ( - mBelow ) . InnerSimplified ;
335+ }
336+ else
337+ return null ;
338+ var mPlusOne = ( m + 1 ) . InnerSimplified ;
339+ if ( TreeAnalyzer . IsZero ( mPlusOne ) )
340+ return null ;
341+ // (d x)^(m + 1)/d, which is (d x)^m x on every branch.
342+ var integrated = @base == x ? MathS . Pow ( x , mPlusOne ) : MathS . Pow ( @base , mPlusOne ) / scale ;
343+ var shifted = MathS . IncompleteGamma ( ( gamma . Parameter + mPlusOne ) . InnerSimplified , gamma . LowerLimit ) ;
344+ var quotient = m is Entity . Number . Integer
345+ ? MathS . Pow ( ( scale / slope ) . InnerSimplified , m )
346+ : written * MathS . Pow ( gamma . LowerLimit , - m ) ;
347+ return coefficient * ( integrated * gamma - quotient * shifted / slope ) / mPlusOne ;
348+ }
349+
350+ /// <summary>
351+ /// <paramref name="polynomial"/>'s coefficients by power of <paramref name="x"/>, with
352+ /// <see langword="null"/> read as 1, or <see langword="null"/> where it is not a polynomial
353+ /// of a degree from 0 to <paramref name="highest"/>.
354+ /// </summary>
355+ private static Dictionary < EInteger , Entity > ? ThePowersOf ( Entity ? polynomial , Entity . Variable x , int highest )
356+ {
357+ if ( polynomial is null )
358+ return new Dictionary < EInteger , Entity > { [ EInteger . Zero ] = Entity . Number . Integer . One } ;
359+ if ( ! TreeAnalyzer . TryGetPolynomial ( polynomial , x , out var powers ) )
360+ return null ;
361+ foreach ( var power in powers . Keys )
362+ if ( power . Sign < 0 || power . CompareTo ( EInteger . FromInt32 ( highest ) ) > 0 )
363+ return null ;
364+ return powers ;
365+ }
366+
367+ /// <summary>
368+ /// <c>int P(x) Gamma(n, b x) dx</c> for <c>P</c> read into <paramref name="powers"/>: each
369+ /// <c>c x^j</c> is <c>c (x^(j + 1) Gamma(n, b x) - Gamma(n + j + 1, b x)/b^(j + 1))/(j + 1)</c>.
370+ /// </summary>
371+ private static Entity BesideAPolynomialWithoutAnOffset ( Dictionary < EInteger , Entity > powers , Entity . IncompleteGammaf gamma , Entity slope , Entity . Variable x )
372+ {
373+ Entity antiderivative = Entity . Number . Integer . Zero ;
374+ Entity shifted = Entity . Number . Integer . Zero ;
375+ foreach ( var monomial in powers )
376+ {
377+ var next = Entity . Number . Integer . Create ( monomial . Key . Add ( 1 ) ) ;
378+ antiderivative += monomial . Value * Power ( x , next . EInteger . ToInt32Checked ( ) ) / next ;
379+ shifted += ( monomial . Value / ( next * Power ( slope , next . EInteger . ToInt32Checked ( ) ) ) ) . InnerSimplified
380+ * MathS . IncompleteGamma ( ( gamma . Parameter + next ) . InnerSimplified , gamma . LowerLimit ) ;
381+ }
382+ return antiderivative . InnerSimplified * gamma - shifted ;
383+ }
384+
385+ /// <summary>
386+ /// <c>int P(x) Gamma(n, u) dx</c> for <c>u = a + b x</c> with an offset: with <c>P</c> written
387+ /// in powers of <c>u</c>, each <c>p u^k</c> is <c>p (u^(k + 1) Gamma(n, u) - Gamma(n + k + 1, u))/(b (k + 1))</c>.
388+ /// </summary>
389+ private static Entity ? BesideAPolynomial ( Entity polynomial , Entity . IncompleteGammaf gamma , Entity slope , Entity offset , Entity . Variable x )
390+ {
391+ // A power (c + d x)^k of a linear, the shape the integrand is usually written in, as
392+ // the binomial it is in u: with s = b c - a d, c + d x is (s + d u)/b, so the weight of
393+ // u^j is C(k, j) s^(k - j) d^j/((j + 1) b^(k + 1)), and the antiderivative vanishing at
394+ // u = 0 is ((c + d x)^(k + 1) - (s/b)^(k + 1))/((k + 1) d).
395+ var ( linear , power ) = polynomial is Entity . Powf ( var written , Entity . Number . Integer exponent ) ? ( written , exponent . EInteger ) : ( polynomial , EInteger . One ) ;
396+ if ( power . Sign > 0 && power . CompareTo ( EInteger . FromInt32 ( 32 ) ) <= 0 && linear . ContainsNode ( x )
397+ && TreeAnalyzer . TryGetPolyLinear ( linear , x , out var d , out var c ) )
398+ {
399+ var k = power . ToInt32Checked ( ) ;
400+ var s = ( slope * c - offset * d ) . InnerSimplified ;
401+ var kPlusOne = Entity . Number . Integer . Create ( k + 1 ) ;
402+ Entity weighted = Entity . Number . Integer . Zero ;
403+ // Without a zeroth power, which is 1 provided its base is not 0.
404+ for ( var j = 0 ; j <= k ; j ++ )
405+ weighted += ( Entity . Number . Rational . Create ( Binomial ( k , j ) . EInteger , EInteger . FromInt32 ( j + 1 ) ) * Power ( s , k - j ) * Power ( d , j ) ) . InnerSimplified
406+ * MathS . IncompleteGamma ( ( gamma . Parameter + ( j + 1 ) ) . InnerSimplified , gamma . LowerLimit ) ;
407+ return ( MathS . Pow ( linear , kPlusOne ) / d - ( MathS . Pow ( s , kPlusOne ) / ( d * MathS . Pow ( slope , kPlusOne ) ) ) . InnerSimplified ) / kPlusOne * gamma
408+ - weighted / MathS . Pow ( slope , kPlusOne ) ;
409+ }
410+ var u = Entity . Variable . CreateUnique ( polynomial + gamma + x , "u" ) ;
411+ if ( ThePowersOf ( polynomial . Substitute ( x , ( u - offset ) / slope ) , u , 32 ) is not { } inU )
412+ return null ;
413+ Entity antiderivative = Entity . Number . Integer . Zero ;
414+ Entity shifted = Entity . Number . Integer . Zero ;
415+ foreach ( var monomial in inU )
416+ {
417+ var next = Entity . Number . Integer . Create ( monomial . Key . Add ( 1 ) ) ;
418+ var weight = ( monomial . Value / ( next * slope ) ) . InnerSimplified ;
419+ antiderivative += weight * Power ( gamma . LowerLimit , next . EInteger . ToInt32Checked ( ) ) ;
420+ shifted += weight * MathS . IncompleteGamma ( ( gamma . Parameter + next ) . InnerSimplified , gamma . LowerLimit ) ;
421+ }
422+ return antiderivative * gamma - shifted ;
423+ }
424+
425+ private static Entity Power ( Entity @base , int power ) => power switch
426+ {
427+ 0 => Entity . Number . Integer . One ,
428+ 1 => @base ,
429+ _ => MathS . Pow ( @base , power )
430+ } ;
431+
432+ /// <summary>
433+ /// <paramref name="factor"/> read as <c>(d x)^m</c>, with <c>d</c> and <c>m</c> free of
434+ /// <paramref name="x"/>: <c>x</c> itself, a power of it, or a power of a multiple of it.
435+ /// </summary>
436+ private static bool TryReadAPowerOfAMultiple ( Entity factor , Entity . Variable x , out Entity @base , out Entity scale , out Entity exponent )
437+ {
438+ @base = x ;
439+ scale = exponent = Entity . Number . Integer . One ;
440+ if ( factor == x )
441+ return true ;
442+ if ( factor is not Entity . Powf ( var written , var power ) || power . ContainsNode ( x ) )
443+ return false ;
444+ exponent = power ;
445+ if ( written == x )
446+ return true ;
447+ if ( ! TreeAnalyzer . TryGetPolyLinear ( written , x , out var multiple , out var offset ) || ! TreeAnalyzer . IsZero ( offset ) )
448+ return false ;
449+ @base = written ;
450+ scale = multiple ;
451+ return true ;
452+ }
453+
271454 internal static Entity ? TryStandardIntegrals ( Entity expr , Entity . Variable x ) => expr switch
272455 {
273456 // Every rule below divides by the linear rate of its integrand's argument, and
@@ -397,6 +580,10 @@ _ when GaussianMoment(expr, x) is { } moment => moment,
397580 TreeAnalyzer . TryGetPolyLinear ( arg , x , out var a , out _ ) =>
398581 MathS . Ln ( MathS . Factorial ( arg - 1 ) ) / a ,
399582
583+ // The upper incomplete gamma function of a linear, alone or beside a power of x or a
584+ // polynomial. https://github.com/asc-community/AngouriMath/issues/2075
585+ _ when IncompleteGammaBesideAPower ( expr , x ) is { } gamma => gamma ,
586+
400587 Entity . Absf ( var arg ) when
401588 TreeAnalyzer . TryGetPolyLinear ( arg , x , out var a , out _ ) => // ∫ |ax + b| dx = sgn(ax + b) * (ax + b)^2 / (2a)
402589 MathS . Signum ( arg ) * MathS . Pow ( arg , 2 ) / ( 2 * a ) ,
0 commit comments