Skip to content

refactor: update gammainc implementation to follow Boost v1.88.0 #7619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,27 @@
* @returns function value
*
* @example
* var y = gammainc( 6.0, 2.0 )
* var y = gammainc( 6.0, 2.0 );
* // returns ~0.9826
*
* @example
* var y = gammainc( 1.0, 2.0, true, true )
* var y = gammainc( 1.0, 2.0, true, true );
* // returns ~0.7358
*
* @example
* var y = gammainc( 7.0, 5.0 )
* var y = gammainc( 7.0, 5.0 );
* // returns ~0.8270
*
* @example
* var y = gammainc( 7.0, 5.0, false )
* var y = gammainc( 7.0, 5.0, false );
* // returns ~19.8482
*
* @example
* var y = gammainc( NaN, 2.0 )
* var y = gammainc( NaN, 2.0 );
* // returns NaN
*
* @example
* var y = gammainc( 6.0, NaN )
* var y = gammainc( 6.0, NaN );
* // returns NaN
*/
declare function gammainc( x: number, a: number, regularized?: boolean, upper?: boolean ): number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* ## Notice
*
* The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_37_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
* The original C++ code and copyright notice are from the [Boost library]{@link https://www.boost.org/doc/libs/1_88_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
*
* ```text
* (C) Copyright John Maddock 2006.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* ## Notice
*
* The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_37_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
* The original C++ code and copyright notice are from the [Boost library]{@link https://www.boost.org/doc/libs/1_88_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
*
* ```text
* (C) Copyright John Maddock 2006.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* ## Notice
*
* The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_37_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
* The original C++ code and copyright notice are from the [Boost library]{@link https://www.boost.org/doc/libs/1_88_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
*
* ```text
* (C) Copyright John Maddock 2006.
Expand Down Expand Up @@ -59,20 +59,16 @@ function fullIGammaPrefix( a, z ) {
if ( z >= 1.0 ) {
if ( ( alz < MAX_LN ) && ( -z > MIN_LN ) ) {
prefix = pow( z, a ) * exp( -z );
}
else if ( a >= 1.0 ) {
} else if ( a >= 1.0 ) {
prefix = pow( z / exp(z/a), a );
}
else {
} else {
prefix = exp( alz - z );
}
}
else {
} else {
/* eslint-disable no-lonely-if */
if ( alz > MIN_LN ) {
prefix = pow( z, a ) * exp( -z );
}
else if ( z/a < MAX_LN ) {
} else if ( z/a < MAX_LN ) {
prefix = pow( z / exp(z/a), a );
} else {
prefix = exp( alz - z );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* ## Notice
*
* The original C++ code and copyright notice are from the [Boost library]{@link https://www.boost.org/doc/libs/1_88_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
*
* ```text
* (C) Copyright John Maddock 2006.
* (C) Copyright Paul A. Bristow 2007.
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
* ```
*/

'use strict';

// MODULES //

var floor = require( '@stdlib/math/base/special/floor' );
var gamma = require( '@stdlib/math/base/special/gamma' );
var abs = require( '@stdlib/math/base/special/abs' );
var pow = require( '@stdlib/math/base/special/pow' );
var ln = require( '@stdlib/math/base/special/ln' );
var SQRT_EPSILON = require( '@stdlib/constants/float64/sqrt-eps' );
var FLOAT64_MAX = require( '@stdlib/constants/float64/max' );
var MAX_LN = require( '@stdlib/constants/float64/max-ln' );
var tgammaILargeX = require( './tgamma_i_large_x.js' );
var finiteGammaQ = require( './finite_gamma_q.js' );
var finiteHalfGammaQ = require( './finite_half_gamma_q.js' );
var fullIGammaPrefix = require( './full_igamma_prefix.js' );
var igammaTemmeLarge = require( './igamma_temme_large.js' );
var lowerGammaSeries = require( './lower_gamma_series.js' );
var regularisedGammaPrefix = require( './regularised_gamma_prefix.js' );
var tgammaSmallUpperPart = require( './tgamma_small_upper_part.js' );
var upperGammaFraction = require( './upper_gamma_fraction.js' );


// MAIN //

/**
* Main entry point for evaluating all the four incomplete gamma functions (lower, upper, regularized lower, and regularized upper).
*
* @private
* @param {number} x - function parameter
* @param {number} a - function parameter
* @param {boolean} regularized - boolean indicating if the function should evaluate the regularized or non-regularized incomplete gamma functions
* @param {boolean} upper - boolean indicating if the function should return the upper tail of the incomplete gamma function
* @returns {number} function value
*/
function igammaFinal( x, a, regularized, upper ) {
var optimisedInvert;
var evalMethod;
var isHalfInt;
var initValue;
var useTemme;
var isSmallA;
var result;
var invert;
var isInt;
var sigma;
var res;
var gam;
var fa;
var g;

result = 0.0;
invert = upper;
isSmallA = ( a < 30 ) && ( a <= x + 1.0 ) && ( x < MAX_LN );
if ( isSmallA ) {
fa = floor( a );
isInt = ( fa === a );
isHalfInt = ( isInt ) ? false : ( abs( fa - a ) === 0.5 );
} else {
isInt = false;
isHalfInt = false;
}
if ( isInt && x > 0.6 ) {
// Calculate Q via finite sum:
invert = !invert;
evalMethod = 0;
} else if ( isHalfInt && x > 0.2 ) {
// Calculate Q via finite sum for half integer a:
invert = !invert;
evalMethod = 1;
} else if ( x < SQRT_EPSILON && a > 1.0 ) {
evalMethod = 6;
} else if ( ( x > 1000.0 ) && ( ( a < x ) || ( abs( a - 50.0 ) / x < 1.0 ) ) ) {

Check warning on line 105 in lib/node_modules/@stdlib/math/base/special/gammainc/lib/igamma_final.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 84. Maximum allowed is 80
// Calculate Q via asymptotic approximation:
invert = !invert;
evalMethod = 7;
} else if ( x < 0.5 ) {
// Changeover criterion chosen to give a changeover at Q ~ 0.33:
if ( -0.4 / ln( x ) < a ) {
evalMethod = 2;
} else {
evalMethod = 3;
}
} else if ( x < 1.1 ) {
// Changeover here occurs when P ~ 0.75 or Q ~ 0.25:
if ( x * 0.75 < a ) {
evalMethod = 2;
} else {
evalMethod = 3;
}
} else {
// Begin by testing whether we're in the "bad" zone where the result will be near 0.5 and the usual series and continued fractions are slow to converge:
useTemme = false;
if ( regularized && a > 20 ) {
sigma = abs( (x-a)/a );
if ( a > 200 ) {
// Limit chosen so that we use Temme's expansion only if the result would be larger than about 10^-6. Below that the regular series and continued fractions converge OK, and if we use Temme's method we get increasing errors from the dominant erfc term as it's (inexact) argument increases in magnitude.

Check warning on line 129 in lib/node_modules/@stdlib/math/base/special/gammainc/lib/igamma_final.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "erfc"
if ( 20 / a > sigma * sigma ) {
useTemme = true;
}
} else if ( sigma < 0.4 ) {
useTemme = true;
}
}
if ( useTemme ) {
evalMethod = 5;
}
// Regular case where the result will not be too close to 0.5: Changeover occurs at P ~ Q ~ 0.5. Note that series computation of P is about x2 faster than continued fraction calculation of Q, so try and use the CF only when really necessary, especially for small x.
else if ( x - ( 1.0 / (3.0 * x) ) < a ) {
evalMethod = 2;
} else {
evalMethod = 4;
invert = !invert;
}
}

/* eslint-disable default-case */
switch ( evalMethod ) {
case 0:
result = finiteGammaQ( a, x );
if ( regularized === false ) {
result *= gamma( a );
}
break;
case 1:
result = finiteHalfGammaQ( a, x );
if ( regularized === false ) {
result *= gamma( a );
}
break;
case 2:
// Compute P:
result = ( regularized ) ?
regularisedGammaPrefix( a, x ) :
fullIGammaPrefix( a, x );
if ( result !== 0.0 ) {
// If the result will be inverted, we can potentially save computation by initializing the series sum closer to the final result. This reduces the number of iterations needed in the series evaluation. However, care must be taken to avoid spurious numeric overflows. In practice, the more expensive overflow checks below are typically bypassed for well-behaved input values.
initValue = 0.0;
optimisedInvert = false;
if ( invert ) {
initValue = ( regularized ) ? 1.0 : gamma( a );
if (
regularized ||
result >= 1.0 ||
FLOAT64_MAX * result > initValue
) {
initValue /= result;
if (
regularized ||
a < 1.0 ||
( FLOAT64_MAX / a > initValue )
) {
initValue *= -a;
optimisedInvert = true;
} else {
initValue = 0.0;
}
} else {
initValue = 0.0;
}
}
result *= lowerGammaSeries( a, x, initValue ) / a;
if ( optimisedInvert ) {
invert = false;
result = -result;
}
}
break;
case 3:
// Compute Q:
invert = !invert;
res = tgammaSmallUpperPart( a, x, invert );
result = res[ 0 ];
g = res[ 1 ];
invert = false;
if ( regularized ) {
result /= g;
}
break;
case 4:
// Compute Q:
result = ( regularized ) ?
regularisedGammaPrefix( a, x ) :
fullIGammaPrefix( a, x );
if ( result !== 0 ) {
result *= upperGammaFraction( a, x );
}
break;
case 5:
result = igammaTemmeLarge( a, x );
if ( x >= a ) {
invert = !invert;
}
break;
case 6:
// Since `x` is so small that P is necessarily very small too, use http://functions.wolfram.com/GammaBetaErf/GammaRegularized/06/01/05/01/01/
result = ( regularized ) ?
pow( x, a ) / gamma( a + 1.0 ) :
pow( x, a ) / a;
result *= 1.0 - ( a * x / ( a + 1.0 ) );
break;
case 7:
// `x` is large, so compute Q:
result = ( regularized ) ?
regularisedGammaPrefix( a, x ) :
fullIGammaPrefix( a, x );
result /= x;
if ( result !== 0.0 ) {
result *= tgammaILargeX( a, x );
}
break;
}
if ( regularized && result > 1.0 ) {
result = 1.0;
}
if ( invert ) {
gam = ( regularized ) ? 1.0 : gamma( a );
result = gam - result;
}
return result;
}


// EXPORTS //

module.exports = igammaFinal;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* ## Notice
*
* The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_37_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
* The original C++ code and copyright notice are from the [Boost library]{@link https://www.boost.org/doc/libs/1_88_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
*
* ```text
* (C) Copyright John Maddock 2006.
Expand Down Expand Up @@ -54,7 +54,7 @@
// VARIABLES //

// Pre-allocate workspace array:
var workspace = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; // WARNING: not thread safe

Check warning on line 57 in lib/node_modules/@stdlib/math/base/special/gammainc/lib/igamma_temme_large.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'warning' comment: 'WARNING: not thread safe'


// MAIN //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* ## Notice
*
* The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_37_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
* The original C++ code and copyright notice are from the [Boost library]{@link https://www.boost.org/doc/libs/1_88_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
*
* ```text
* (C) Copyright John Maddock 2006.
Expand All @@ -35,7 +35,7 @@
// MODULES //

var sumSeries = require( '@stdlib/math/base/tools/sum-series' );
var lowerIncompleteGammaSeries = require( './lower_incomplete_gamma_series.js' );

Check warning on line 38 in lib/node_modules/@stdlib/math/base/special/gammainc/lib/lower_gamma_series.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Identifier name 'lowerIncompleteGammaSeries' is too long (> 25)


// MAIN //
Expand All @@ -46,7 +46,7 @@
* ## Method
*
* - Multiply result by `((z^a) * (e^-z) / a)` to get the full lower incomplete integral.
* - Divide by `tgamma(a)` to get the normalized value.
* - Divide by `gamma(a)` to get the normalized value.
*
* @private
* @param {number} a - function parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* ## Notice
*
* The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_37_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
* The original C++ code and copyright notice are from the [Boost library]{@link https://www.boost.org/doc/libs/1_88_0/boost/math/special_functions/gamma.hpp}. The implementation has been modified for JavaScript.
*
* ```text
* (C) Copyright John Maddock 2006.
Expand All @@ -42,7 +42,7 @@
* @param {number} z1 - function parameter
* @returns {Function} series function
*/
function lowerIncompleteGammaSeries( a1, z1 ) {

Check warning on line 45 in lib/node_modules/@stdlib/math/base/special/gammainc/lib/lower_incomplete_gamma_series.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Identifier name 'lowerIncompleteGammaSeries' is too long (> 25)
var result = 1.0;
var a = a1;
var z = z1;
Expand Down
Loading