Faster gamma calculation#524
Draft
tompng wants to merge 1 commit intoruby:masterfrom
Draft
Conversation
81fa97e to
b41db9e
Compare
94832ff to
4c1aac7
Compare
Calculates `gamma(x)` by Lagrange interpolation of `b^x/x!` where `b` is `x.round`. Implements Binary Splitting Method version for small-digit number and Baby-Step Giant-Step version for full-digit number. Fallback to Stirling's asymptotic expansion if `x` is extremely large.
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!Approach in this PR
Directly interpolating
$$f(x) = \frac{b^x}{x!}$$
gamma(x)with equidistant nodes fails due to its singularities and Runge's phenomenon, so interpolate the following scaled reciprocal function instead: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.Algorithm overview:
Lagrange interpolation of f(x) = b**x / x!
BSM(Binary Splitting Method) version for small digit numbers,
O(PREC*log(PREC)^3).BSGS(Baby-Step Giant-Step) version for full digit numbers,
O(PREC^2).Both magnitude of order faster than Spouge's approximation which is
O(PREC^2*log(PREC))Requires fast calculation of
factorial(nearly_x_integer).Factorial Doubling for fast calculation of large factorials:
Using Legendre duplication formula, we can calculate
factorial(2n)fromfactorial(n)andfactorial(n + 0.5).Calculating
factorial(n + 0.5)is done by an optimized BSM version of Lagrange interpolation in quasi-linear time.This will drastically reduce the cost of calculating large factorials.
O(PREC*log(PREC)^3*log(factorial_argument))Stirling's approximation with Bernoulli numbers
Only used in lgamma when x is extremely large, such as:
Benchmark
Comparison with mpmath(gmpy-backend)