Skip to content

Commit d8b8903

Browse files
Integrate the polygamma functions, and give an odd order its exact values (#2073)
Each order integrates to the one before, int psi^(n)(u) = psi^(n - 1)(u), and the digamma function to ln Gamma(u) = ln((u - 1)!), for an argument linear in the variable; the integrator reads them from the table it reads the other special functions from. An odd order at a whole or half-whole argument is exact: psi^(n)(1) = n! zeta(n + 1) and psi^(n)(1/2) = (2^(n + 1) - 1) n! zeta(n + 1) (DLMF 5.15.2, 5.15.3), with zeta(2m) a rational times pi^(2m) from the exact Bernoulli numbers, and the recurrence moves the argument to either: polygamma(1, 1) is pi^2/6 and polygamma(1, 3) is pi^2/6 - 5/4. An even order wants zeta at an odd number or Euler's constant, and stays as written. Each exact value is checked against the numerical one. Part of #171. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 9c4fb8e commit d8b8903

6 files changed

Lines changed: 114 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ as `a - b = 0`, as a statement's own solve reads it: `"x + 1 = 3".Solve("x")` wa
103103
- **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.
104104
- **Evaluation.** Each is evaluated to the working precision anywhere in the complex plane but at `0, -1, -2, ...`, its poles, where it is `NaN`.
105105
- **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+
- **Integrals.** Each order integrates to the one before, and `digamma(x)` to `ln((x - 1)!)`.
107+
- **Exact values.** An odd order at a whole or half-whole argument is a rational times a power of `pi` plus a rational: `polygamma(1, 1)` is `1/6 * pi ^ 2`.
106108

107109
| Input | Was (2.6.0) | Now |
108110
|---|---|---|
@@ -111,6 +113,8 @@ as `a - b = 0`, as a statement's own solve reads it: `"x + 1 = 3".Solve("x")` wa
111113
| `"polygamma(1, 1)".ToEntity()` | an `UnhandledParseException` | `polygamma(1, 1)`, which evaluates to `1.6449340668482264…` |
112114
| `"x!".ToEntity().Differentiate("x")` | `derivative(x!, x)`, unevaluated | `x! * digamma(x + 1)` |
113115
| `"gamma(x)".ToEntity().Differentiate("x")` | `derivative((x - 1)!, x)` | `(x - 1)! * digamma(x - 1 + 1)` |
116+
| `"polygamma(1, 3)".ToEntity().InnerSimplified` | an `UnhandledParseException` | `1/6 * pi ^ 2 - 5/4` |
117+
| `"digamma(x)".ToEntity().Integrate("x")` | `digamma * x ^ 2 / 2 + C`, of the product | `ln((x - 1)!) + C` |
114118

115119
### Numerical equality is to half the evaluation's digits
116120

‎Sources/AngouriMath/Core/Entity/Continuous/Number/Polygamma.cs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,56 @@ public static Complex Polygamma(EInteger order, Complex z)
5959
return Complex.Create(valueRe.RoundToPrecision(context), valueIm.RoundToPrecision(context));
6060
}
6161

62+
/// <summary>
63+
/// <c>psi^(n)(q)</c> exactly, for an odd order <c>n</c> and a whole or half-whole <c>q</c> other than a pole,
64+
/// or <see langword="null"/>: a rational times <c>pi^(n + 1)</c>, plus a rational.
65+
/// </summary>
66+
/// <remarks>
67+
/// <c>psi^(n)(1) = n! zeta(n + 1)</c> and <c>psi^(n)(1/2) = (2^(n + 1) - 1) n! zeta(n + 1)</c> for an odd
68+
/// <c>n</c> (DLMF 5.15.2, 5.15.3), where <c>zeta(2m) = (-1)^(m + 1) B_(2m) (2 pi)^(2m)/(2 (2m)!)</c> is a
69+
/// rational times <c>pi^(2m)</c>; the recurrence <c>psi^(n)(z + 1) = psi^(n)(z) - n!/z^(n + 1)</c> moves
70+
/// the argument to either. An even order wants <c>zeta</c> at an odd number, or Euler's constant, which
71+
/// have no closed form here. https://github.com/asc-community/AngouriMath/issues/171
72+
/// </remarks>
73+
internal static Entity? ExactPolygamma(EInteger order, ERational argument)
74+
{
75+
if (order.Sign <= 0 || order.IsEven || order.CompareTo(100) > 0)
76+
return null;
77+
var half = ERational.Create(EInteger.One, EInteger.FromInt32(2));
78+
var start = argument.Denominator.Equals(EInteger.One) ? ERational.One
79+
: argument.Denominator.Equals(EInteger.FromInt32(2)) ? half : null;
80+
if (start is null || argument.Denominator.Equals(EInteger.One) && argument.Sign <= 0)
81+
return null;
82+
var steps = argument.Subtract(start).ToEInteger();
83+
if (steps.Abs().CompareTo(1000) > 0)
84+
return null;
85+
var n = order.ToInt32Checked();
86+
var m = (n + 1) / 2;
87+
var factorial = Factorial(n);
88+
// zeta(2m) / pi^(2m) = (-1)^(m + 1) B_(2m) 2^(2m) / (2 (2m)!)
89+
var zetaOverPiPower = Bernoulli(m).Multiply(ERational.FromEInteger(EInteger.One.ShiftLeft(2 * m)))
90+
.Divide(ERational.FromEInteger(Factorial(2 * m).Multiply(2)));
91+
if (m % 2 == 0)
92+
zetaOverPiPower = zetaOverPiPower.Negate();
93+
var piCoefficient = zetaOverPiPower.Multiply(ERational.FromEInteger(factorial));
94+
if (start.Equals(half))
95+
piCoefficient = piCoefficient.Multiply(ERational.FromEInteger(EInteger.One.ShiftLeft(n + 1).Subtract(EInteger.One)));
96+
// The recurrence, up from the start or down to the argument.
97+
var rest = ERational.Zero;
98+
var power = n + 1;
99+
if (steps.Sign > 0)
100+
for (var j = EInteger.Zero; j.CompareTo(steps) < 0; j = j.Add(EInteger.One))
101+
rest = rest.Subtract(ERational.FromEInteger(factorial).Divide(Pow(start.Add(ERational.FromEInteger(j)), power)));
102+
else
103+
for (var j = EInteger.One; j.CompareTo(steps.Negate()) <= 0; j = j.Add(EInteger.One))
104+
rest = rest.Add(ERational.FromEInteger(factorial).Divide(Pow(start.Subtract(ERational.FromEInteger(j)), power)));
105+
var piTerm = Rational.Create(piCoefficient) * MathS.Pow(MathS.pi, Integer.Create(power));
106+
return (rest.Sign < 0 ? piTerm - Rational.Create(rest.Negate()) : piTerm + Rational.Create(rest)).InnerSimplified;
107+
108+
static ERational Pow(ERational x, int exponent)
109+
=> ERational.Create(x.Numerator.Pow(exponent), x.Denominator.Pow(exponent));
110+
}
111+
62112
private static (EDecimal Re, EDecimal Im) Polygamma(int n, EDecimal re, EDecimal im, EContext work)
63113
{
64114
if (re.CompareTo(EDecimal.FromString("0.5")) < 0)

‎Sources/AngouriMath/Docs/Usage/Syntax.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,11 @@ undefined at `0`; `Si(+oo)` is `pi / 2` and `Ci(+oo)` is `0`.
332332
to the working precision anywhere in the complex plane but at `0, -1, -2, ...`, its poles, and each
333333
order is the derivative of the one before: the derivative of `polygamma(n, x)` is
334334
`polygamma(n + 1, x)`. The derivative of `x!` is `x! * digamma(x + 1)`, and of `gamma(x)` it is
335-
`(x - 1)! * digamma(x)`. Special values, such as `digamma(1) = -gamma`, stay as written, since the
336-
library has no symbol for Euler's constant.
335+
`(x - 1)! * digamma(x)`. Integrating goes the other way: `polygamma(n, x)` integrates to
336+
`polygamma(n - 1, x)`, and `digamma(x)` to `ln((x - 1)!)`. An odd order at a whole or half-whole
337+
argument is exact, a rational times a power of `pi` plus a rational: `polygamma(1, 1)` is
338+
`1/6 * pi ^ 2` and `polygamma(1, 3)` is `1/6 * pi ^ 2 - 5/4`. Other special values, such as
339+
`digamma(1) = -gamma`, stay as written, since the library has no symbol for Euler's constant.
337340

338341
**Calculus** — `derivative(expr, var, order)`, `integral(expr, var)`,
339342
`integral(expr, var, from, to)`, `limit(expr, var, dest)`, `limitleft(...)`, `limitright(...)`;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ internal static Entity AntiderivativeLog(Entity arg)
6767
Entity.Cif(var arg) => arg,
6868
Entity.Shif(var arg) => arg,
6969
Entity.Chif(var arg) => arg,
70+
Entity.Polygammaf(var order, var arg) when !order.ContainsNode(x) => arg,
7071
Entity.Arcsinf(var arg) => arg,
7172
Entity.Arccosf(var arg) => arg,
7273
Entity.Arctanf(var arg) => arg,
@@ -384,6 +385,18 @@ _ when GaussianMoment(expr, x) is { } moment => moment,
384385
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
385386
(arg * MathS.Chi(arg) - MathS.Hyperbolic.Sinh(arg)) / a,
386387

388+
// The polygamma functions, each the derivative of the one before: int psi^(n)(u) =
389+
// psi^(n - 1)(u) for n >= 1, and int psi(u) = ln Gamma(u) = ln((u - 1)!), whose value is
390+
// i pi more where Gamma is negative, a constant on each interval between the poles.
391+
// https://github.com/asc-community/AngouriMath/issues/171
392+
Entity.Polygammaf(Entity.Number.Integer { EInteger: var order }, var arg) when order.Sign > 0 &&
393+
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
394+
MathS.Polygamma(Entity.Number.Integer.Create(order - 1), arg) / a,
395+
396+
Entity.Polygammaf(Entity.Number.Integer { IsZero: true }, var arg) when
397+
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
398+
MathS.Ln(MathS.Factorial(arg - 1)) / a,
399+
387400
Entity.Absf(var arg) when
388401
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) => // ∫ |ax + b| dx = sgn(ax + b) * (ax + b)^2 / (2a)
389402
MathS.Signum(arg) * MathS.Pow(arg, 2) / (2 * a),

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ protected override Entity InnerSimplify(bool isExact)
602602
=> ExpandOnTwoArguments(Order, Argument,
603603
(order, argument) => (order, argument) switch
604604
{
605+
(Integer { EInteger: var n }, Rational q) when isExact && Number.ExactPolygamma(n, q.ERational) is { } exact => exact,
605606
(Integer { EInteger: var n }, Complex z) when !isExact && n.Sign >= 0 => Number.Polygamma(n, z),
606607
(Integer { EInteger: var n }, Real r) when n.Sign >= 0 && r.EDecimal.IsPositiveInfinity() => n.IsZero ? Real.PositiveInfinity : Integer.Zero,
607608
_ => null

‎Sources/Tests/UnitTests/Convenience/PolygammaTest.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,47 @@ public void EachOrderIsTheDerivativeOfTheOneBefore()
7878
Assert.Equal((MathS.Polygamma(3, "x^2") * (2 * MathS.Var("x"))).InnerSimplified, "polygamma(2, x^2)".ToEntity().Differentiate("x").InnerSimplified);
7979
}
8080

81+
/// <summary>
82+
/// An odd order at a whole or half-whole argument is a rational times <c>pi^(n + 1)</c> plus a rational
83+
/// (DLMF 5.15.2, 5.15.3); each is checked against its numerical value.
84+
/// </summary>
85+
[Theory]
86+
[InlineData("polygamma(1, 1)", "1/6 * pi ^ 2")]
87+
[InlineData("polygamma(1, 1/2)", "1/2 * pi ^ 2")]
88+
[InlineData("polygamma(3, 1)", "1/15 * pi ^ 4")]
89+
[InlineData("polygamma(1, 3)", "1/6 * pi ^ 2 - 5/4")]
90+
[InlineData("polygamma(1, -1/2)", "1/2 * pi ^ 2 + 4")]
91+
[InlineData("polygamma(3, 5/2)", "pi ^ 4 - 2624/27")]
92+
public void AnOddOrderAtAWholeOrHalfWholeArgumentIsExact(string expression, string exact)
93+
{
94+
var simplified = expression.ToEntity().InnerSimplified;
95+
Assert.Equal(exact.ToEntity().InnerSimplified, simplified);
96+
AssertClose(expression.EvalNumerical().Stringize(), Assert.IsType<Real>(simplified.EvalNumerical()).EDecimal);
97+
}
98+
99+
/// <summary>An even order wants zeta at an odd number, or Euler's constant: it stays as written.</summary>
100+
[Theory]
101+
[InlineData("digamma(1)")]
102+
[InlineData("polygamma(2, 1)")]
103+
[InlineData("polygamma(1, 1/3)")]
104+
public void OtherwiseItStaysAsWritten(string expression)
105+
=> Assert.IsType<Entity.Polygammaf>(expression.ToEntity().InnerSimplified);
106+
107+
/// <summary>Each order integrates to the one before, and the digamma function to <c>ln Gamma</c>.</summary>
108+
[Theory]
109+
[InlineData("polygamma(2, x)", "polygamma(1, x)")]
110+
[InlineData("polygamma(1, 3 * x + 2)", "digamma(3 * x + 2) / 3")]
111+
[InlineData("digamma(x)", "ln((x - 1)!)")]
112+
[InlineData("digamma(2 * x)", "ln((2 * x - 1)!) / 2")]
113+
public void IntegratesToTheOrderBefore(string integrand, string antiderivative)
114+
{
115+
var integral = integrand.ToEntity().Integrate("x").Substitute("C", 0).Simplify();
116+
Assert.Equal(antiderivative.ToEntity().Simplify(), integral);
117+
var back = integral.Differentiate("x").Substitute("x", Rational.Create(7, 3)).EvalNumerical();
118+
var expected = integrand.ToEntity().Substitute("x", Rational.Create(7, 3)).EvalNumerical();
119+
AssertClose(expected.Stringize(), Assert.IsType<Real>(back).EDecimal);
120+
}
121+
81122
[Theory]
82123
[InlineData("digamma(x)", "digamma(x)", @"\psi\left(x\right)", "sympy.digamma(x)")]
83124
[InlineData("polygamma(2, x)", "polygamma(2, x)", @"\psi^{(2)}\left(x\right)", "sympy.polygamma(2, x)")]

0 commit comments

Comments
 (0)