Skip to content

Faster gamma calculation - #524

Open
tompng wants to merge 1 commit into
ruby:masterfrom
tompng:gamma_lagrange
Open

Faster gamma calculation#524
tompng wants to merge 1 commit into
ruby:masterfrom
tompng:gamma_lagrange

Conversation

@tompng

@tompngtompng commented Apr 10, 2026

Copy link
Copy Markdown
Member

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 gamma(x) with equidistant nodes fails due to its singularities and Runge's phenomenon, so interpolate the following scaled reciprocal function instead:
$$f(x) = \frac{b^x}{x!}$$
Scaling by b**x shapes the function into a nearly symmetric bell curve centered at b.
By performing Lagrange interpolation at x = b-l, b-l+1, ..., b+l, we achieve highly accurate localized interpolation. The center b is dynamically determined based on the input x.
The formula is simple, node values f(integer) can be easily calculated, and it opens up room for various optimizations.

Design of f(x)

To calculate gamma(integer + small_rational) with Binary Splitting Method, f(n+1) needs to be calculated easily from f(n), such as making f(n+1)/f(n) a small rational.
Candidates of f(x) are x!/b**x and b**x/x!. x!/b**x requires constant times more interpolation nodes.

Other functions don't have the ability to calculate f(n+1) from f(n) easily:

  • x!/x**x, x!/x**b: Simple, but hard to optimize
  • x!*exp(x)/x**(x+1/2): Gives a formula similar to Spouge's approximation
  • (x+a-1)!*exp(-x-a)/(x+a)**(x+1/2) at nodes -a-1..-1: Gives exactly the same formula as Spouge's approximation when modified to converge at x → ∞

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*log(log(PREC))).
Both magnitude of order faster than Spouge's approximation which costs O(PREC^2*log(PREC))
Requires fast calculation of factorial(nearly_x_integer).

Complexities assume quasi-linear multiplication, counting large-by-small products as (n/m) * M(m) = n * log(m) bit ops. BigDecimal multiplies the small coefficients by schoolbook instead: an extra log factor asymptotically, but faster at any feasible precision. Measured time grows like PREC^2.

Factorial Doubling for fast calculation of large factorials:

Using Legendre duplication formula, we can calculate factorial(2n) from factorial(n) and factorial(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:

BigMath.lgamma(10000000000000000,10000);

Benchmark

Calculationmaster branchThis PRnote
BigMath.gamma(1.25, 10000)49s0.17sBSM
BigMath.gamma(1.25, 100000)6000s(estimated)2.3sBSM
BigMath.gamma(BigDecimal(1).div(3, 10000), 10000)56s8.2sFull-digit, BSGS
BigMath.gamma(BigDecimal(1).div(3, 100000), 100000)7000s(estimated)803sFull-digit, BSGS
BigMath.gamma(10**17, 10000)71s1.1sFactorial Doubling
BigMath.gamma(10**17, 100000)9000s(estimated)13sFactorial Doubling

Comparison with mpmath(gmpy-backend)

Calculationdigitsmpmath first runmpmath second run (cached)This PR
gamma(1.25)50003.2s0.12s0.08s
gamma(1.25)1000026.9s0.69s0.17s
gamma(1.25)20000226s3.7s0.34s
gamma(1/3)50003.2s0.12s2.2s
gamma(1/3)1000024s0.68s8.2s
gamma(1/3)20000226s3.8s33s

@tompng
tompngforce-pushed the gamma_lagrange branch 2 times, most recently from 81fa97e to b41db9eCompareApril 16, 2026 17:14
@mrknmrkn added this to the v4.2 milestone Apr 22, 2026
@tompng
tompngforce-pushed the gamma_lagrange branch 7 times, most recently from fb147e6 to 5451111CompareMay 10, 2026 16:11
@tompng
tompng marked this pull request as ready for review May 17, 2026 08:46
@tompng
tompngforce-pushed the gamma_lagrange branch 2 times, most recently from 83d619d to beb5187CompareAugust 4, 2026 16:48
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.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@tompng@mrkn