← Back to Pull requests

improve complex digamma edge cases #24

Closed
unknown closed this pull request without merging on Nov 27, 2026 14:56 UTC
View on GitHub
unknown unknown commented on Nov 27, 2026 13:37 UTC
Author

A small tweak to squeeze better performance and accuracy out of digamma in edge cases.

Currently complex digamma loses accuracy as z approaches a pole off the real axis, eventually returning Inf/NaN (e.g. digamma(-3 + 1e-12*cis(-1.9635))). Real arguments are fine as they were.

The problem stems from reflection term in _cotpi: the denominator cosh 2πy − cos 2πx, formed as 1 − cospi(2x)·sech(2πy), cancels catastrophically near z ∈ ℤ. For |Im z| < 1 this PR now uses

cot(πz) = (sin πx cos πx − i sinh πy cosh πy) / (sin²πx + sinh²πy)

which doesn't have that problem and with sincospi and cosh πy = √(1 + sinh²πy) it is cheaper than before. Within about √floatmin of the pole at 0 that denominator would underflow, so there is another small fix to account for that.

Time for a comparison table again (using mpmath for accuracy checks). 😄 v1.2.2 is this PR.

digamma(::ComplexF64), 200 points per row

| region | v1.2.1 | v1.2.2 | SpecialFunctions |
|---|---|---|---|
| near a pole, 10⁻⁴ ≤ R < 10⁻¹ | 3e6 eps · 49 ns | 1.5 eps · 52 ns | 2.2 eps · 83 ns |
| near a pole, 10⁻¹² ≤ R < 10⁻⁴ | 3e15 eps, 93/200 Inf/NaN · 49 ns | 1.8 eps · 51 ns | 1.9 eps · 80 ns |
| near a pole, R < 10⁻¹² (to 10⁻³⁰⁰) | all Inf/NaN · 48 ns | 1.2 eps · 63 ns | 1.2 eps · 60 ns |
| Re z ≤ 0, \|Im z\| < 1 | 8.5 eps · 56 ns | 8.6 eps · 52 ns | 8.5 eps · 87 ns |
| Re z ≤ 0, \|Im z\| ≥ 1 | 1.9 eps · 55 ns | 1.9 eps · 54 ns | 2.8 eps · 94 ns |
| Re z > 0 | 3.3 eps · 30 ns | 3.3 eps · 30 ns | 3.3 eps · 30 ns |

digamma(::ComplexF32), 200 points per row

| region | v1.2.1 | v1.2.2 | SpecialFunctions |
|---|---|---|---|
| near a pole, 10⁻⁴ ≤ R < 10⁻¹ | 2e6 eps · 30 ns | 2.0 eps · 26 ns | 0.4 eps · 84 ns |
| near a pole, 10⁻¹² ≤ R < 10⁻⁴ | 6e6 eps, 194/200 Inf/NaN · 30 ns | 1.3 eps · 27 ns | 0.5 eps · 61 ns |
| near a pole, R < 10⁻¹² (to 10⁻³⁷) | all Inf/NaN · 30 ns | 1.2 eps · 34 ns | 0.4 eps · 52 ns |
| Re z ≤ 0, \|Im z\| < 1 | 18 eps · 29 ns | 5.6 eps · 28 ns | 0.5 eps · 87 ns |
| Re z ≤ 0, \|Im z\| ≥ 1 | 2.0 eps · 30 ns | 2.0 eps · 30 ns | 0.4 eps · 94 ns |
| Re z > 0 | 11 eps · 18 ns | 11 eps · 18 ns | 0.5 eps · 31 ns |

digamma(::Complex{BigFloat}), 256-bit precision, 40 points per row

| region | v1.2.1 | v1.2.2 | SpecialFunctions |
|---|---|---|---|
| near a pole, 10⁻⁴ ≤ R < 10⁻¹ | 2 bits · 127 µs | 0 · 123 µs | n/a |
| near a pole, 10⁻¹² ≤ R < 10⁻⁴ | 57 bits · 130 µs | 0 · 128 µs | n/a |
| near a pole, R < 10⁻¹² (to 10⁻⁶⁰) | 248 bits, 18/40 Inf/NaN · 133 µs | 0 · 135 µs | n/a |
| Re z ≤ 0, \|Im z\| < 1 | 0 · 128 µs | 0 · 125 µs | n/a |
| Re z ≤ 0, \|Im z\| ≥ 1 | 0 · 128 µs | 0 · 130 µs | n/a |
| Re z > 0 | 0 · 125 µs | 0 · 120 µs | n/a |

Note: SpecialFunctions converts ComplexF32 to Float64 internally, hence the sub-eps Float32 accuracy at ~3x the cost (that's cheating that I don't want to do, if someone puts in F32 they expect F32 performance gains not F64 timings with worse accuracy).

New tests were added to prevent future regression in these edge cases too.

Diff: 391 lines (+293 / -98) across 4 files Reviewer: oscardssmith Review duration: 79m
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 391 lines diff across 4 files
0.43
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.357
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #24"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 43.1% (MEDIUM) · Higher = larger blast radius
43.1%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 17% · Safe to merge
17%