Skip to content

Commit 865744b

Browse files
The upper incomplete gamma function (#2077)
gamma(a, z) with a second argument is the upper incomplete gamma function Gamma(a, z) = int_z^oo t^(a - 1) e^(-t) dt, as Mathematica's Gamma[a, z] and mpmath's gammainc(a, z) are; with one, gamma is still Gamma(x), and a second argument was a parse error. z^a takes its principal branch, so the cut is the negative real axis, which keeps the value from above. It is printed so, in LaTeX as \Gamma(a, z) and in SymPy as uppergamma, and its derivative in z is -z^(a - 1) e^(-z); a parameter that varies declines. A whole a >= 1 is (n - 1)! e^(-z) times a polynomial (DLMF 8.4.8), a half is sqrt(pi) erfc(sqrt(z)) (8.4.6), and Gamma(a, 0) is Gamma(a). It is evaluated to the working precision for any complex a and z. Where |z| > 2|a| + 10, in the right half-plane or past (P + 5) ln 10, it is Legendre's continued fraction (8.9.2) by the modified Lentz algorithm; elsewhere it is Gamma(a) less the lower one's series (8.7.1), with twice |z|/ln 10 guard digits and more where the difference shows it cancelled more; and at a whole a <= 0, where Gamma(a) has a pole, E_1(z) by its series and the recurrence (8.4.15). It is real exactly where it is real: for a positive z, and for a whole a >= 1 at any real z. It agrees with mpmath at 168 points to 1e-90 at a hundred digits and at 54 to 1e-290 at three hundred, near the poles and on the cut. 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 a9866da commit 865744b

24 files changed

Lines changed: 542 additions & 5 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ as `a - b = 0`, as a statement's own solve reads it: `"x + 1 = 3".Solve("x")` wa
129129
| `"polygamma(1, 3)".ToEntity().InnerSimplified` | an `UnhandledParseException` | `1/6 * pi ^ 2 - 5/4` |
130130
| `"digamma(x)".ToEntity().Integrate("x")` | `digamma * x ^ 2 / 2 + C`, of the product | `ln((x - 1)!) + C` |
131131

132+
### `gamma(a, z)` is the upper incomplete gamma function
133+
134+
**Not silent: what was a parse error is a function.** `gamma` took one argument, and a second was a
135+
`FunctionArgumentCountException`. With two it is now the upper incomplete gamma function,
136+
`Gamma(a, z) = int_z^oo t^(a - 1) e^(-t) dt`, as Mathematica's `Gamma[a, z]` is.
137+
- **Branch.** It takes the principal branch of `z^a`, with the cut on the negative real axis.
138+
- **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)`.
140+
- **Exact forms.** A whole `a >= 1` and `a = 1/2` are elementary.
141+
([#2075](https://github.com/asc-community/AngouriMath/issues/2075))
142+
143+
| Input | Was (2.6.0) | Now |
144+
|---|---|---|
145+
| `"gamma(1/3, x)".ToEntity()` | a `FunctionArgumentCountException` | `gamma(1/3, x)` |
146+
| `"gamma(1/3, 2)".EvalNumerical()` | a `FunctionArgumentCountException` | `0.068136414441459136…` |
147+
| `"gamma(3, x)".ToEntity().InnerSimplified` | a `FunctionArgumentCountException` | `2 * e ^ (-x) * (1 + x + x ^ 2 / 2)` |
148+
132149
### Numerical equality is to half the evaluation's digits
133150

134151
**Silent: wrong answers.** `MathS.UnsafeAndInternal.AreEqualNumerically` evaluated both sides at its check points and

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
699699
[AngouriMath/Core/Entity/Continuous/Number/Polygamma.cs]
700700
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
701701

702+
[AngouriMath/Core/Entity/Continuous/Number/IncompleteGamma.cs]
703+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
704+
705+
[AngouriMath/Core/Entity/Continuous/Entity.Continuous.IncompleteGamma.Classes.cs]
706+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
707+
702708
[AngouriMath/Core/Entity/Continuous/Entity.Continuous.Polygamma.Classes.cs]
703709
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
704710

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,19 @@ public static Entity VectorLength(Matrix v)
14741474
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14751475
public static Entity Digamma(Entity argument) => new Polygammaf(Integer.Zero, argument);
14761476

1477+
/// <summary>The upper incomplete gamma function, <c>Gamma(a, z) = int_z^oo t^(a - 1) e^(-t) dt</c></summary>
1478+
/// <param name="parameter">The parameter <c>a</c>, any complex number</param>
1479+
/// <param name="lowerLimit">The lower limit <c>z</c>, any complex number; <c>0</c> only where <c>Re a > 0</c></param>
1480+
/// <returns>The <see cref="Entity.IncompleteGammaf"/> node</returns>
1481+
/// <remarks>
1482+
/// <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
1484+
/// <c>gamma(a, z)</c>, as Mathematica's <c>Gamma[a, z]</c> and mpmath's <c>gammainc(a, z)</c> are.
1485+
/// https://github.com/asc-community/AngouriMath/issues/2075
1486+
/// </remarks>
1487+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1488+
public static Entity IncompleteGamma(Entity parameter, Entity lowerLimit) => new IncompleteGammaf(parameter, lowerLimit);
1489+
14771490
/// <summary>The sine integral, <c>Si(z) = int_0^z sin(t)/t dt</c></summary>
14781491
/// <param name="a">The argument, any complex number</param>
14791492
/// <returns>The <see cref="Entity.Sif"/> node</returns>

‎Sources/AngouriMath/Core/Antlr/AngouriMath.g‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,9 @@ atom returns[Entity value]
543543

544544

545545
| 'factorial(' args = function_arguments ')' { Assert("factorial", 1, $args.list.Count); $value = MathS.Factorial($args.list[0]); }
546-
| 'gamma(' args = function_arguments ')' { Assert("gamma", 1, $args.list.Count); $value = MathS.Gamma($args.list[0]); }
546+
/* With a second argument, the upper incomplete gamma function Gamma(a, z), as Mathematica's Gamma[a, z].
547+
https://github.com/asc-community/AngouriMath/issues/2075 */
548+
| 'gamma(' args = function_arguments ')' { if ($args.list.Count == 2) $value = MathS.IncompleteGamma($args.list[0], $args.list[1]); else { Assert("gamma", 1, $args.list.Count); $value = MathS.Gamma($args.list[0]); } }
547549
| 'derivative(' args = function_arguments ')'
548550
{
549551
if (Assert("derivative", (3, 2), $args.list.Count))

‎Sources/AngouriMath/Core/Antlr/AngouriMathParser.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4369,7 +4369,7 @@ public AtomContext atom() {
43694369
_localctx.args = function_arguments();
43704370
State = 886;
43714371
Match(T__30);
4372-
Assert("gamma", 1, _localctx.args.list.Count); _localctx.value = MathS.Gamma(_localctx.args.list[0]);
4372+
if (_localctx.args.list.Count == 2) _localctx.value = MathS.IncompleteGamma(_localctx.args.list[0], _localctx.args.list[1]); else { Assert("gamma", 1, _localctx.args.list.Count); _localctx.value = MathS.Gamma(_localctx.args.list[0]); }
43734373
}
43744374
break;
43754375
case 94:

‎Sources/AngouriMath/Core/Domains.Classes.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,14 @@ partial record Polygammaf
384384
internal override Domain DefaultCodomain => Domain.Complex;
385385
}
386386

387+
partial record IncompleteGammaf
388+
{
389+
/// <inheritdoc/>
390+
public override Domain Codomain { get; protected init; } = Domain.Complex;
391+
/// <inheritdoc/>
392+
internal override Domain DefaultCodomain => Domain.Complex;
393+
}
394+
387395
partial record Sif
388396
{
389397
/// <inheritdoc/>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System;
9+
10+
namespace AngouriMath
11+
{
12+
partial record Entity
13+
{
14+
/// <summary>
15+
/// The upper incomplete gamma function, <c>Gamma(a, z) = int_z^oo t^(a - 1) e^(-t) dt</c>, with the principal branch
16+
/// of <c>z^a</c>: the cut is the negative real axis.
17+
/// </summary>
18+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2075</remarks>
19+
public sealed partial record IncompleteGammaf(Entity Parameter, Entity LowerLimit) : Function, IBinaryNode
20+
{
21+
private IncompleteGammaf New(Entity parameter, Entity lowerLimit) =>
22+
ReferenceEquals(Parameter, parameter) && ReferenceEquals(LowerLimit, lowerLimit) ? this : new(parameter, lowerLimit) { Codomain = Codomain };
23+
24+
/// <inheritdoc/>
25+
public Entity NodeFirstChild => Parameter;
26+
27+
/// <inheritdoc/>
28+
public Entity NodeSecondChild => LowerLimit;
29+
30+
/// <inheritdoc/>
31+
public override Entity Replace(Func<Entity, Entity> func) => func(New(Parameter.Replace(func), LowerLimit.Replace(func)));
32+
/// <inheritdoc/>
33+
protected override Entity[] InitDirectChildren() => new[] { Parameter, LowerLimit };
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)