Skip to content

Commit db5573d

Browse files
The polygamma functions, and the derivative of the factorial (#2068)
* The polygamma functions, and the derivative of the factorial polygamma(n, z) is psi^(n)(z), the (n + 1)-th derivative of ln Gamma(z), and digamma(z) is polygamma(0, z) and prints so. Both are parsed and printed in text, LaTeX and SymPy; neither name was recognised, so digamma(1) was the variable digamma, and polygamma(1, 1) did not parse. Each order differentiates to the next, and an order that depends on the variable declines. The derivative of x! is x! digamma(x + 1), where it declined (#958), so gamma(x) differentiates to (x - 1)! digamma(x). It is evaluated to the working precision anywhere in the complex plane but at 0, -1, -2, ..., its poles: reflected left of Re z = 1/2 (DLMF 5.15.6), with the derivatives of cot(pi z) as polynomials in cot; moved by the recurrence (5.15.5) to Re w at least the working digits; and summed by the asymptotic series (5.11.2, 5.15.8), with the Bernoulli numbers exact from the tangent numbers by Brent and Harvey's recurrence. It agrees with mpmath to the last digit at 100, 300 and 2000 digits. Special values stay as written, since digamma(1) = -gamma wants a symbol for Euler's constant. Two derivative tests used the factorial as the node that cannot be differentiated; they use a polygamma function of a varying order. Part of #171. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * Name the polygamma functions in the MCP server's syntax resource The server's syntax document lists every function the grammar reads, and its test fails on a name the grammar accepts that the document does not. Part of #171. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 2045021 commit db5573d

31 files changed

Lines changed: 1585 additions & 965 deletions

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ as `a - b = 0`, as a statement's own solve reads it: `"x + 1 = 3".Solve("x")` wa
9797
| `MathS.Equations("x + y = 3", "x - y = 1").Solve("x", "y")` | `null` | `[[2, 1]]` |
9898
| `MathS.Equations("x + y = 3", "x - y = 1").ToString()` | `x + y = 3 = 0` and `x - y = 1 = 0` | `x + y - 3 = 0` and `x - y - 1 = 0` |
9999

100+
### The polygamma functions, and the derivative of the factorial
101+
102+
**Silent: a new reading, and an answer where there was none.**
103+
- **New functions.** `polygamma(n, z)` and `digamma(z)` are the polygamma functions `psi^(n)(z)`, the `(n + 1)`-th derivatives of `ln Gamma(z)`, and the digamma function, which is `polygamma(0, z)`. Neither name was recognised, so each was read as a product with an undeclared variable.
104+
- **Evaluation.** Each is evaluated to the working precision anywhere in the complex plane but at `0, -1, -2, ...`, its poles, where it is `NaN`.
105+
- **Derivatives.** Each order differentiates to the next. The derivative of `x!`, which was left unevaluated, is `x! * digamma(x + 1)` ([#171](https://github.com/asc-community/AngouriMath/issues/171)).
106+
107+
| Input | Was (2.6.0) | Now |
108+
|---|---|---|
109+
| `"digamma(1)".ToEntity()` | `digamma * 1`, the variable `digamma` | `digamma(1)`, which evaluates to `-0.5772156649015328…` |
110+
| `"digamma(x)".ToEntity()` | `digamma * x` | `digamma(x)` |
111+
| `"polygamma(1, 1)".ToEntity()` | an `UnhandledParseException` | `polygamma(1, 1)`, which evaluates to `1.6449340668482264…` |
112+
| `"x!".ToEntity().Differentiate("x")` | `derivative(x!, x)`, unevaluated | `x! * digamma(x + 1)` |
113+
| `"gamma(x)".ToEntity().Differentiate("x")` | `derivative((x - 1)!, x)` | `(x - 1)! * digamma(x - 1 + 1)` |
114+
100115
### Numerical equality is to half the evaluation's digits
101116

102117
**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
@@ -696,6 +696,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
696696
[AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs]
697697
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
698698

699+
[AngouriMath/Core/Entity/Continuous/Number/Polygamma.cs]
700+
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
701+
702+
[AngouriMath/Core/Entity/Continuous/Entity.Continuous.Polygamma.Classes.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+
699705
[AngouriMath/Core/Entity/Continuous/Entity.Continuous.ExponentialIntegrals.Classes.cs]
700706
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
701707

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,25 @@ public static Entity VectorLength(Matrix v)
14551455
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14561456
public static Entity Li(Entity a) => new Lif(a);
14571457

1458+
/// <summary>The polygamma function, <c>psi^(n)(z)</c>, the <c>(n + 1)</c>-th derivative of <c>ln Gamma(z)</c></summary>
1459+
/// <param name="order">The order <c>n</c>, a whole number <c>n >= 0</c></param>
1460+
/// <param name="argument">The argument, any complex number but <c>0, -1, -2, ...</c>, its poles</param>
1461+
/// <returns>The <see cref="Entity.Polygammaf"/> node</returns>
1462+
/// <remarks>
1463+
/// <c>psi^(0)</c> is the digamma function, <c>Gamma'(z)/Gamma(z)</c>, which is what the derivative of
1464+
/// <c>x!</c> is written with: <c>(x!)' = x! psi(x + 1)</c>. Each order is the derivative of the one before.
1465+
/// https://github.com/asc-community/AngouriMath/issues/171
1466+
/// </remarks>
1467+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1468+
public static Entity Polygamma(Entity order, Entity argument) => new Polygammaf(order, argument);
1469+
1470+
/// <summary>The digamma function, <c>psi(z) = Gamma'(z)/Gamma(z)</c>, which is <c>polygamma(0, z)</c></summary>
1471+
/// <param name="argument">The argument, any complex number but <c>0, -1, -2, ...</c>, its poles</param>
1472+
/// <returns>The <see cref="Entity.Polygammaf"/> node of order <c>0</c></returns>
1473+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/171</remarks>
1474+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1475+
public static Entity Digamma(Entity argument) => new Polygammaf(Integer.Zero, argument);
1476+
14581477
/// <summary>The sine integral, <c>Si(z) = int_0^z sin(t)/t dt</c></summary>
14591478
/// <param name="a">The argument, any complex number</param>
14601479
/// <returns>The <see cref="Entity.Sif"/> node</returns>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,9 @@ atom returns[Entity value]
616616
/* The exponential and logarithmic integrals. https://github.com/asc-community/AngouriMath/issues/1501 */
617617
| 'Ei(' args = function_arguments ')' { Assert("Ei", 1, $args.list.Count); $value = MathS.Ei($args.list[0]); }
618618
| 'li(' args = function_arguments ')' { Assert("li", 1, $args.list.Count); $value = MathS.Li($args.list[0]); }
619+
/* The polygamma functions, and the digamma function, which is the one of order 0. https://github.com/asc-community/AngouriMath/issues/171 */
620+
| 'polygamma(' args = function_arguments ')' { Assert("polygamma", 2, $args.list.Count); $value = MathS.Polygamma($args.list[0], $args.list[1]); }
621+
| 'digamma(' args = function_arguments ')' { Assert("digamma", 1, $args.list.Count); $value = MathS.Digamma($args.list[0]); }
619622
/* The trigonometric and hyperbolic integrals. https://github.com/asc-community/AngouriMath/issues/1501 */
620623
| 'Si(' args = function_arguments ')' { Assert("Si", 1, $args.list.Count); $value = MathS.Si($args.list[0]); }
621624
| 'Ci(' args = function_arguments ')' { Assert("Ci", 1, $args.list.Count); $value = MathS.Ci($args.list[0]); }

‎Sources/AngouriMath/Core/Antlr/AngouriMath.interp‎

Lines changed: 5 additions & 1 deletion
Large diffs are not rendered by default.

‎Sources/AngouriMath/Core/Antlr/AngouriMath.tokens‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,16 @@ T__179=180
181181
T__180=181
182182
T__181=182
183183
T__182=183
184-
NEWLINE=184
185-
NUMBER=185
186-
SPECIALSET=186
187-
BOOLEAN=187
188-
NAN=188
189-
VARIABLE=189
190-
COMMENT=190
191-
WS=191
184+
T__183=184
185+
T__184=185
186+
NEWLINE=186
187+
NUMBER=187
188+
SPECIALSET=188
189+
BOOLEAN=189
190+
NAN=190
191+
VARIABLE=191
192+
COMMENT=192
193+
WS=193
192194
'!'=1
193195
'^'=2
194196
'-'=3
@@ -362,14 +364,16 @@ WS=191
362364
'erfi('=171
363365
'Ei('=172
364366
'li('=173
365-
'Si('=174
366-
'Ci('=175
367-
'Shi('=176
368-
'Chi('=177
369-
'trunc('=178
370-
'conjugate('=179
371-
'domain('=180
372-
'piecewise('=181
373-
'apply('=182
374-
'lambda('=183
375-
'NaN'=188
367+
'polygamma('=174
368+
'digamma('=175
369+
'Si('=176
370+
'Ci('=177
371+
'Shi('=178
372+
'Chi('=179
373+
'trunc('=180
374+
'conjugate('=181
375+
'domain('=182
376+
'piecewise('=183
377+
'apply('=184
378+
'lambda('=185
379+
'NaN'=190

0 commit comments

Comments
 (0)