Skip to content

Commit 3b0889c

Browse files
The polygamma functions and the harmonic numbers compile (#2215)
polygamma, digamma and harmonicnumber compile in both compilers, through PolygammaFunction, a double-precision kernel by the interpreter's algorithm: reflected left of Re z = 1/2 near the axis, moved right by the recurrence to Re w >= 16 + n, and summed by the asymptotic series with the Bernoulli numbers from the tangent numbers. Off the axis the recurrence is taken without the reflection, whose cotangent polynomial cancels there to all but a few digits, and cot is taken with its argument reduced, since at a half-whole z its rounding is magnified by pi^(n + 1) and the polynomial's coefficients. The harmonic numbers are the sum for a whole n and the polygamma form elsewhere, as the interpreter has them. Both compilers agree with evaluation on a grid of orders and arguments, and both, with evaluation, agree with mpmath at 7,170 polygamma points and 2,997 harmonic ones. Part of #2195 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 33b0bd8 commit 3b0889c

11 files changed

Lines changed: 369 additions & 3 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,8 @@ other a parse error.** They are `1 + 1/2^m + ... + 1/n^m` for a whole `n >= 0`,
654654
`ζ(m) - ζ(m, n + 1)` elsewhere, as Mathematica's `HarmonicNumber` is: `ψ(n + 1) - ψ(1)` for `m = 1`, evaluated numerically
655655
through `digamma` and `polygamma` for a whole `m >= 1`, and exactly at a half-whole `n` for `m = 1` or an even `m`. A
656656
negative integer `n` has no value but for a whole `m <= 0`
657-
([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
657+
([#2182](https://github.com/asc-community/AngouriMath/issues/2182)). They compile, on the same terms, in both compilers
658+
([#2195](https://github.com/asc-community/AngouriMath/issues/2195)).
658659

659660
| Input | Was (2.6.0) | Now |
660661
|---|---|---|
@@ -866,6 +867,7 @@ no others. Where that proof does not go through, or a bound is symbolic, the int
866867
- **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)).
867868
- **Integrals.** Each order integrates to the one before, and `digamma(x)` to `ln((x - 1)!)`.
868869
- **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`. The digamma function there is a rational less Euler's constant, and at a half-whole argument less `2 ln 2` as well: `digamma(1)` is `-eulergamma`.
870+
- **Compiled forms.** Each compiles, in double precision and by the same reflection, recurrence and asymptotic series, in both compilers ([#2195](https://github.com/asc-community/AngouriMath/issues/2195)).
869871

870872
| Input | Was (2.6.0) | Now |
871873
|---|---|---|

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
369369
[Tests/UnitTests/Common/LambertWCompilationTest.cs]
370370
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
371371

372+
[AngouriMath/Numerics/PolygammaFunction.cs]
373+
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
374+
375+
[Tests/UnitTests/Common/PolygammaCompilationTest.cs]
376+
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
377+
372378
[Tests/UnitTests/Common/CompileMatrixTest.cs]
373379
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
374380

‎Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,31 @@ private protected override void CompileNode(Compiler compiler)
617617
}
618618
}
619619

620+
// The polygamma functions and the harmonic numbers. https://github.com/asc-community/AngouriMath/issues/2195
621+
public partial record Polygammaf
622+
{
623+
private protected override bool CompilesItself => true;
624+
625+
private protected override void CompileNode(Compiler compiler)
626+
{
627+
Argument.InnerCompile(compiler);
628+
Order.InnerCompile(compiler);
629+
compiler.Instructions.Add(new(InstructionType.CALL_POLYGAMMA));
630+
}
631+
}
632+
633+
public partial record HarmonicNumberf
634+
{
635+
private protected override bool CompilesItself => true;
636+
637+
private protected override void CompileNode(Compiler compiler)
638+
{
639+
Order.InnerCompile(compiler);
640+
Argument.InnerCompile(compiler);
641+
compiler.Instructions.Add(new(InstructionType.CALL_HARMONIC_NUMBER));
642+
}
643+
}
644+
620645
public partial record Maxf
621646
{
622647
private protected override bool CompilesItself => true;

‎Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ internal enum InstructionType
8282
CALL_FALLING_FACTORIAL,
8383
CALL_RISING_FACTORIAL,
8484
CALL_LAMBERTW,
85+
CALL_POLYGAMMA,
86+
CALL_HARMONIC_NUMBER,
8587

8688
// 3-arg: a value where its predicate holds, and the one below it elsewhere.
8789
CALL_SELECT = 150,
@@ -359,6 +361,12 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value
359361
case InstructionType.CALL_LAMBERTW:
360362
stack.Push(AngouriMath.Numerics.LambertWFunction.LambertW(stack.Pop(), stack.Pop()));
361363
break;
364+
case InstructionType.CALL_POLYGAMMA:
365+
stack.Push(AngouriMath.Numerics.PolygammaFunction.Polygamma(stack.Pop(), stack.Pop()));
366+
break;
367+
case InstructionType.CALL_HARMONIC_NUMBER:
368+
stack.Push(AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(stack.Pop(), stack.Pop()));
369+
break;
362370
case InstructionType.CALL_DOUBLE_FACTORIAL:
363371
stack.Push(AngouriMath.Numerics.CombinatorialFunctions.DoubleFactorial(stack.Pop()));
364372
break;

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ RisingFactorialf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLe
244244
=> Expression.Call(GetDef("RisingFactorial", 2, newLeft.Type), newLeft, newRight),
245245
LambertWf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
246246
=> Expression.Call(GetDef("LambertW", 2, newLeft.Type), newLeft, newRight),
247+
Polygammaf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
248+
=> Expression.Call(GetDef("Polygamma", 2, newLeft.Type), newLeft, newRight),
249+
HarmonicNumberf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
250+
=> Expression.Call(GetDef("HarmonicNumber", 2, newLeft.Type), newLeft, newRight),
247251
Maxf => Expression.Call(GetDef("Max", 2, left.Type), left, right),
248252
Minf => Expression.Call(GetDef("Min", 2, left.Type), left, right),
249253

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ public static System.Numerics.Complex Round(System.Numerics.Complex a)
347347
public static System.Numerics.Complex LambertW(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.LambertWFunction.LambertW(a, b);
348348
public static double LambertW(double a, double b) => AngouriMath.Numerics.LambertWFunction.LambertW(a, b);
349349

350+
// The polygamma functions of a whole order, and the harmonic numbers.
351+
// https://github.com/asc-community/AngouriMath/issues/2195
352+
public static System.Numerics.Complex Polygamma(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.PolygammaFunction.Polygamma(a, b);
353+
public static double Polygamma(double a, double b) => AngouriMath.Numerics.PolygammaFunction.Polygamma(a, b);
354+
public static System.Numerics.Complex HarmonicNumber(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(a, b);
355+
public static double HarmonicNumber(double a, double b) => AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(a, b);
356+
350357
/// <summary>
351358
/// True where <c>System.Numerics.Complex.IsNaN</c> is, spelled out because that overload is
352359
/// .NET 7 and later and this assembly also targets netstandard2.0. A complex number is NaN
@@ -540,7 +547,11 @@ public static bool IsNaN(System.Numerics.Complex a)
540547
{ ("RisingFactorial", 2, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>((a, b) => RisingFactorial(a, b)) },
541548
{ ("RisingFactorial", 2, typeof(double)), Def<double>((a, b) => RisingFactorial(a, b)) },
542549
{ ("LambertW", 2, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>((a, b) => LambertW(a, b)) },
550+
{ ("Polygamma", 2, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>((a, b) => Polygamma(a, b)) },
551+
{ ("HarmonicNumber", 2, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>((a, b) => HarmonicNumber(a, b)) },
543552
{ ("LambertW", 2, typeof(double)), Def<double>((a, b) => LambertW(a, b)) },
553+
{ ("Polygamma", 2, typeof(double)), Def<double>((a, b) => Polygamma(a, b)) },
554+
{ ("HarmonicNumber", 2, typeof(double)), Def<double>((a, b) => HarmonicNumber(a, b)) },
544555
};
545556

546557
// An expression tree is how C# spells "the MethodInfo of this method" without a string:

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ namespace AngouriMath.Core.Compilation.IntoLinq
237237
public static System.Numerics.Complex LambertW(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.LambertWFunction.LambertW(a, b);
238238
public static double LambertW(double a, double b) => AngouriMath.Numerics.LambertWFunction.LambertW(a, b);
239239

240+
// The polygamma functions of a whole order, and the harmonic numbers.
241+
// https://github.com/asc-community/AngouriMath/issues/2195
242+
public static System.Numerics.Complex Polygamma(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.PolygammaFunction.Polygamma(a, b);
243+
public static double Polygamma(double a, double b) => AngouriMath.Numerics.PolygammaFunction.Polygamma(a, b);
244+
public static System.Numerics.Complex HarmonicNumber(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(a, b);
245+
public static double HarmonicNumber(double a, double b) => AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(a, b);
246+
240247
/// <summary>
241248
/// True where <c>System.Numerics.Complex.IsNaN</c> is, spelled out because that overload is
242249
/// .NET 7 and later and this assembly also targets netstandard2.0. A complex number is NaN
@@ -293,6 +300,8 @@ namespace AngouriMath.Core.Compilation.IntoLinq
293300
<# } #>
294301
<# foreach (var type in new[]{ "System.Numerics.Complex", "double" }) { #>
295302
{ ("LambertW", 2, typeof(<#= type #>)), Def<<#= type #>>((a, b) => LambertW(a, b)) },
303+
{ ("Polygamma", 2, typeof(<#= type #>)), Def<<#= type #>>((a, b) => Polygamma(a, b)) },
304+
{ ("HarmonicNumber", 2, typeof(<#= type #>)), Def<<#= type #>>((a, b) => HarmonicNumber(a, b)) },
296305
<# } #>
297306
};
298307

0 commit comments

Comments
 (0)