Draft
Conversation
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.
This is a proof of concept implementation of faster gamma calculation using localized Lagrange interpolation of
b**x / x!The Problem with Direct Interpolation
Directly interpolating
gamma(x)with equidistant nodes fails due to its singularities and Runge's phenomenon.Shared Philosophy: Spouge as Lagrange Interpolation
Although Spouge's approximation is analytically derived from contour integration, from a different perspective, it can be interpreted as a Lagrange interpolation of the scaled function:
$$f(x) = \frac{\left(x+a-1\right)!}{\left(2(x+a)\right)^{x+\frac{1}{2}}e^{-x-a}}$$ $x \to \infty$ .
at the nodes
x = -a+1, -a+2, ..., -1, with a constant adjusted to converge asThe proposed approach shares this foundational philosophy—scaling the function to tame its growth, then interpolating. However, while Spouge's formula converges for any
x > 0, it is computationally heavy, requiring numerous expensive full-precision divisions and square root calculations. The method proposed in this PR is essentially a highly computation-optimized specialization of this interpolation philosophy.Approach in this PR
Interpolate the following scaled reciprocal function instead:
$$f(x) = \frac{b^x}{x!}$$
Since this is an entire function (no poles), it avoids Runge's phenomenon. Scaling by
b**xshapes the function into a nearly symmetric bell curve centered atb.By performing Lagrange interpolation at
x = b-l, b-l+1, ..., b+l, we achieve highly accurate localized interpolation. The centerbis dynamically determined based on the inputx.The formula is simple, node values
f(integer)can be easily calculated, and it opens up room for various optimizations.Small digits case
e.g.,
BigMath.gamma(1.25, prec)Uses Binary Splitting Method (BSM). Quasilinear time complexity.
Full-digit case
e.g.,
BigMath.gamma(BigDecimal(1).div(3, prec), prec)Uses Baby-Step Giant-Step (BSGS) combined with Montgomery batch inversion to eliminate heavy full-precision divisions, reducing them to scalar multiplications. Time complexity is roughly
O(N^2).xmust not be too large.x > threshold_depend_on_preccaseFalls back to Spouge's approximation. Time complexity is
O(N*M(N))whereM(N)is the time complexity of full-precision multiplication/division.Benchmark
Comparison with mpmath(gmpy-backend)