Repository navigation
The polygamma functions and the harmonic numbers compile - #2215
Merged
Merged
Conversation
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
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #2195, item 3:
polygamma,digammaandharmonicnumbercompile, in FastExpression and in the LINQ compiler.Numerics/PolygammaFunctionis a double-precision kernel that follows the interpreter's algorithm:Re z = 1/2near the axis.Re w >= 16 + n.NaNat the poles and at an order that is not whole.Two things the decimal algorithm can afford and double precision cannot:
cot(pi z)is near±i, where1 + cot^2vanishes, andQ_n's terms cancelled to seven digits atpolygamma(7, -2 - 3i). The recurrence alone passes no pole off the axis.cotis taken with its argument reduced by the nearest integer, and near a half astan(pi (1/2 - r)). At a half-wholez,cos(pi z)is a rounding away from0, andpi^(n + 1)andQ_n's coefficients magnify it:polygamma(10, -9/2)was off by2e-3relative.Complex.Tanis taken only of a small argument; nearpi/2it computes its imaginary part as0/0.The harmonic numbers are the sum for a whole
nup to 10000. For a negative wholenthey 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:
-5to5off the poles, and 6,000 random points, a third of them near the negative axis.All agree to
1e-9, and so does evaluation.Tests:
PolygammaCompilationTestcompares both compilers with evaluation on orders0, 1, 2, 3, 7and1/2, at 15 arguments: poles, half-integers, left of1/2, far out, and off the axis. The same grid is run forharmonicnumber, anddigammais checked on its own.UserInvalidExceptionspinsgamma(2, x)in place ofpolygamma(1, x)as a node with no compiled form.🤖 Generated with Claude Code
https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura