Skip to content

Commit b21f013

Browse files
Integrate the upper incomplete gamma function beside a power or a polynomial (#2078)
By parts against the antiderivative Q of what is beside it. The derivative of Gamma(n, u) is -b u^(n - 1) e^(-u) for u = a + b x, so with Q written in powers of u each u^k lands on u^(n + k - 1) e^(-u), the derivative of -Gamma(n + k, u). Beside a polynomial with an offset that is Q Gamma(n, u) - sum_k q_k Gamma(n + k, u), in Rubi's b c - a d for a power of a linear. Beside (d x)^m without one, for any m but -1, it is x^(m + 1) Gamma(n, b x)/(m + 1) - x^m (b x)^(-m) Gamma(m + n + 1, b x)/ (b (m + 1)), where x^m (b x)^(-m) is b^(-m) for a whole m only. Rubi's 8.6, read with a harness copy that maps Maxima's GAMMA(a, x): 63 of 177 solved with the node alone, 144 with this, 0 wrong. The shared harness does not read GAMMA and its corpus stays at 48,305/48,305 with no row moved. MathS.IncompleteGamma now says that its derivative, read through the power, takes the real root at a negative z for an exact order of small odd denominator, where the value takes the principal branch (#204). Part of #2075. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 865744b commit b21f013

4 files changed

Lines changed: 305 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,21 @@ as `a - b = 0`, as a statement's own solve reads it: `"x + 1 = 3".Solve("x")` wa
136136
`Gamma(a, z) = int_z^oo t^(a - 1) e^(-t) dt`, as Mathematica's `Gamma[a, z]` is.
137137
- **Branch.** It takes the principal branch of `z^a`, with the cut on the negative real axis.
138138
- **Evaluation.** It is evaluated to the working precision for any complex `a` and `z`.
139-
- **Derivative.** In `z` it is `-z^(a - 1) e^(-z)`.
139+
- **Derivative.** In `z` it is `-z^(a - 1) e^(-z)`. At a negative `z` that power takes the real root for an exact
140+
`a` of small odd denominator, so there it is not the derivative of the value
141+
([#204](https://github.com/asc-community/AngouriMath/issues/204)).
140142
- **Exact forms.** A whole `a >= 1` and `a = 1/2` are elementary.
143+
- **Integrals.** Of a linear `u`, beside a polynomial, or beside any power of `x` but `1/x` where `u` is a multiple
144+
of `x`, by parts, in gamma functions of `u`.
141145
([#2075](https://github.com/asc-community/AngouriMath/issues/2075))
142146

143147
| Input | Was (2.6.0) | Now |
144148
|---|---|---|
145149
| `"gamma(1/3, x)".ToEntity()` | a `FunctionArgumentCountException` | `gamma(1/3, x)` |
146150
| `"gamma(1/3, 2)".EvalNumerical()` | a `FunctionArgumentCountException` | `0.068136414441459136…` |
147151
| `"gamma(3, x)".ToEntity().InnerSimplified` | a `FunctionArgumentCountException` | `2 * e ^ (-x) * (1 + x + x ^ 2 / 2)` |
152+
| `"x^2*gamma(0, a*x)".ToEntity().Integrate("x")` | a `FunctionArgumentCountException` | `x ^ 3 / 3 * gamma(0, a * x) - 1 / (3 * a ^ 3) * 2 * e ^ (-a * x) * (1 + a * x + (a * x) ^ 2 / 2) + C` |
153+
| `"x^m*gamma(n, b*x)".ToEntity().Integrate("x")` | a `FunctionArgumentCountException` | `(x ^ (m + 1) * gamma(n, b * x) - x ^ m * (b * x) ^ (-m) * gamma(n + m + 1, b * x) / b) / (m + 1) + C` |
148154

149155
### Numerical equality is to half the evaluation's digits
150156

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,10 @@ public static Entity VectorLength(Matrix v)
14801480
/// <returns>The <see cref="Entity.IncompleteGammaf"/> node</returns>
14811481
/// <remarks>
14821482
/// <c>z^a</c> takes its principal branch, so the cut is the negative real axis, which keeps the value from above.
1483-
/// <c>Gamma(a, 0)</c> is <c>Gamma(a)</c>, and the derivative in <c>z</c> is <c>-z^(a - 1) e^(-z)</c>. It is written
1483+
/// <c>Gamma(a, 0)</c> is <c>Gamma(a)</c>, and the derivative in <c>z</c> is <c>-z^(a - 1) e^(-z)</c>. At a negative
1484+
/// <c>z</c> the power there takes the real root for an exact <c>a</c> of small odd denominator, not the principal
1485+
/// branch, so for such an <c>a</c> it is not the derivative of the value
1486+
/// (https://github.com/asc-community/AngouriMath/issues/204). It is written
14841487
/// <c>gamma(a, z)</c>, as Mathematica's <c>Gamma[a, z]</c> and mpmath's <c>gammainc(a, z)</c> are.
14851488
/// https://github.com/asc-community/AngouriMath/issues/2075
14861489
/// </remarks>

‎Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)