Skip to content

The polygamma functions and the harmonic numbers compile - #2215

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
polygamma-and-the-harmonic-numbers-compile
Oct 30, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
polygamma-and-the-harmonic-numbers-compile

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Part of #2195, item 3: polygamma, digamma and harmonicnumber compile, in FastExpression and in the LINQ compiler.

Numerics/PolygammaFunction is a double-precision kernel that follows the interpreter's algorithm:

  • The reflection (DLMF 5.15.6), left of Re z = 1/2 near the axis.
  • The recurrence (DLMF 5.15.5), to Re w >= 16 + n.
  • The asymptotic series (DLMF 5.11.2, 5.15.8), with the Bernoulli numbers taken exactly from the tangent numbers, as the interpreter does, then rounded.
  • NaN at the poles and at an order that is not whole.

Two things the decimal algorithm can afford and double precision cannot:

  • Off the axis there is no reflection. There cot(pi z) is near ±i, where 1 + cot^2 vanishes, and Q_n's terms cancelled to seven digits at polygamma(7, -2 - 3i). The recurrence alone passes no pole off the axis.
  • cot is taken with its argument reduced by the nearest integer, and near a half as tan(pi (1/2 - r)). At a half-whole z, cos(pi z) is a rounding away from 0, and pi^(n + 1) and Q_n's coefficients magnify it: polygamma(10, -9/2) was off by 2e-3 relative. Complex.Tan is taken only of a small argument; near pi/2 it computes its imaginary part as 0/0.

The harmonic numbers are the sum for a whole n up to 10000. For a negative whole n they have no value, except for a whole order <= 0. Elsewhere, for a whole order from 1 to 64, they are the polygamma form, as #2200 has them. All three functions are new since 2.6.0, so their entries say they compile and there is no 2.6.0 row.

Checked against mpmath (dps 30), compiled and evaluated alike:

  • 7,170 polygamma points: orders 0 to 10 on the real axis from -5 to 5 off the poles, and 6,000 random points, a third of them near the negative axis.
  • 2,997 harmonic points of orders 1, 2 and 3, real and complex.

All agree to 1e-9, and so does evaluation.

Tests:

  • PolygammaCompilationTest compares both compilers with evaluation on orders 0, 1, 2, 3, 7 and 1/2, at 15 arguments: poles, half-integers, left of 1/2, far out, and off the axis. The same grid is run for harmonicnumber, and digamma is checked on its own.
  • UserInvalidExceptions pins gamma(2, x) in place of polygamma(1, x) as a node with no compiled form.
  • Full suite 17943/0, interactive trio, AOT smoke 70/0, all frameworks build.

🤖 Generated with Claude Code

https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura

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
@Rafael-SOWNet Rafael-SOWNet moved this from Todo to In review in AngouriMath Oct 30, 2026
@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.54962% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.62%. Comparing base (33b0bd8) to head (d1186d2).

Files with missing lines Patch % Lines
Sources/AngouriMath/Numerics/PolygammaFunction.cs 87.61% 6 Missing and 7 partials ⚠️
...gouriMath/Functions/Compilation/IntoFE/Compiler.cs 80.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2215      +/-   ##
==========================================
+ Coverage   86.60%   86.62%   +0.01%     
==========================================
  Files         304      305       +1     
  Lines       67207    67338     +131     
  Branches    19509    19544      +35     
==========================================
+ Hits        58206    58330     +124     
- Misses       4894     4897       +3     
- Partials     4107     4111       +4     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.17% <88.54%> (+0.01%) ⬆️
fsharp 32.97% <ø> (ø)
interactive 40.72% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ath/Functions/Compilation/IntoFE/FastExpression.cs 96.31% <100.00%> (+0.09%) ⬆️
...ctions/Compilation/IntoLinq/CompilationProtocol.cs 90.88% <100.00%> (+0.10%) ⬆️
...h/Functions/Compilation/IntoLinq/MathAllMethods.cs 71.11% <100.00%> (+0.65%) ⬆️
...gouriMath/Functions/Compilation/IntoFE/Compiler.cs 86.54% <80.00%> (-0.21%) ⬇️
Sources/AngouriMath/Numerics/PolygammaFunction.cs 87.61% <87.61%> (ø)

... and 11 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Rafael-SOWNet
Rafael-SOWNet merged commit 3b0889c into master Oct 30, 2026
36 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in AngouriMath Oct 30, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the polygamma-and-the-harmonic-numbers-compile branch October 30, 2026 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant