|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +* |
| 18 | +* |
| 19 | +* ## Notice |
| 20 | +* |
| 21 | +* 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. |
| 22 | +* |
| 23 | +* ```text |
| 24 | +* (C) Copyright John Maddock 2006. |
| 25 | +* (C) Copyright Paul A. Bristow 2007. |
| 26 | +* |
| 27 | +* Use, modification and distribution are subject to the |
| 28 | +* Boost Software License, Version 1.0. (See accompanying file |
| 29 | +* LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 30 | +* ``` |
| 31 | +*/ |
| 32 | + |
| 33 | +'use strict'; |
| 34 | + |
| 35 | +// MODULES // |
| 36 | + |
| 37 | +var floor = require( '@stdlib/math/base/special/floor' ); |
| 38 | +var gamma = require( '@stdlib/math/base/special/gamma' ); |
| 39 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 40 | +var pow = require( '@stdlib/math/base/special/pow' ); |
| 41 | +var ln = require( '@stdlib/math/base/special/ln' ); |
| 42 | +var SQRT_EPSILON = require( '@stdlib/constants/float64/sqrt-eps' ); |
| 43 | +var FLOAT64_MAX = require( '@stdlib/constants/float64/max' ); |
| 44 | +var MAX_LN = require( '@stdlib/constants/float64/max-ln' ); |
| 45 | +var tgammaILargeX = require( './tgamma_i_large_x.js' ); |
| 46 | +var finiteGammaQ = require( './finite_gamma_q.js' ); |
| 47 | +var finiteHalfGammaQ = require( './finite_half_gamma_q.js' ); |
| 48 | +var fullIGammaPrefix = require( './full_igamma_prefix.js' ); |
| 49 | +var igammaTemmeLarge = require( './igamma_temme_large.js' ); |
| 50 | +var lowerGammaSeries = require( './lower_gamma_series.js' ); |
| 51 | +var regularisedGammaPrefix = require( './regularised_gamma_prefix.js' ); |
| 52 | +var tgammaSmallUpperPart = require( './tgamma_small_upper_part.js' ); |
| 53 | +var upperGammaFraction = require( './upper_gamma_fraction.js' ); |
| 54 | + |
| 55 | + |
| 56 | +// MAIN // |
| 57 | + |
| 58 | +/** |
| 59 | +* Main entry point for evaluating all the four incomplete gamma functions (lower, upper, regularized lower, and regularized upper). |
| 60 | +* |
| 61 | +* @private |
| 62 | +* @param {number} x - function parameter |
| 63 | +* @param {number} a - function parameter |
| 64 | +* @param {boolean} regularized - boolean indicating if the function should evaluate the regularized or non-regularized incomplete gamma functions |
| 65 | +* @param {boolean} upper - boolean indicating if the function should return the upper tail of the incomplete gamma function |
| 66 | +* @returns {number} function value |
| 67 | +*/ |
| 68 | +function igammaFinal( x, a, regularized, upper ) { |
| 69 | + var optimisedInvert; |
| 70 | + var evalMethod; |
| 71 | + var isHalfInt; |
| 72 | + var initValue; |
| 73 | + var useTemme; |
| 74 | + var isSmallA; |
| 75 | + var result; |
| 76 | + var invert; |
| 77 | + var isInt; |
| 78 | + var sigma; |
| 79 | + var res; |
| 80 | + var gam; |
| 81 | + var fa; |
| 82 | + var g; |
| 83 | + |
| 84 | + result = 0.0; |
| 85 | + invert = upper; |
| 86 | + isSmallA = ( a < 30 ) && ( a <= x + 1.0 ) && ( x < MAX_LN ); |
| 87 | + if ( isSmallA ) { |
| 88 | + fa = floor( a ); |
| 89 | + isInt = ( fa === a ); |
| 90 | + isHalfInt = ( isInt ) ? false : ( abs( fa - a ) === 0.5 ); |
| 91 | + } else { |
| 92 | + isInt = false; |
| 93 | + isHalfInt = false; |
| 94 | + } |
| 95 | + if ( isInt && x > 0.6 ) { |
| 96 | + // Calculate Q via finite sum: |
| 97 | + invert = !invert; |
| 98 | + evalMethod = 0; |
| 99 | + } else if ( isHalfInt && x > 0.2 ) { |
| 100 | + // Calculate Q via finite sum for half integer a: |
| 101 | + invert = !invert; |
| 102 | + evalMethod = 1; |
| 103 | + } else if ( x < SQRT_EPSILON && a > 1.0 ) { |
| 104 | + evalMethod = 6; |
| 105 | + } else if ( ( x > 1000.0 ) && ( ( a < x ) || ( abs( a - 50.0 ) / x < 1.0 ) ) ) { |
| 106 | + // Calculate Q via asymptotic approximation: |
| 107 | + invert = !invert; |
| 108 | + evalMethod = 7; |
| 109 | + } else if ( x < 0.5 ) { |
| 110 | + // Changeover criterion chosen to give a changeover at Q ~ 0.33: |
| 111 | + if ( -0.4 / ln( x ) < a ) { |
| 112 | + evalMethod = 2; |
| 113 | + } else { |
| 114 | + evalMethod = 3; |
| 115 | + } |
| 116 | + } else if ( x < 1.1 ) { |
| 117 | + // Changeover here occurs when P ~ 0.75 or Q ~ 0.25: |
| 118 | + if ( x * 0.75 < a ) { |
| 119 | + evalMethod = 2; |
| 120 | + } else { |
| 121 | + evalMethod = 3; |
| 122 | + } |
| 123 | + } else { |
| 124 | + // 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: |
| 125 | + useTemme = false; |
| 126 | + if ( regularized && a > 20 ) { |
| 127 | + sigma = abs( (x-a)/a ); |
| 128 | + if ( a > 200 ) { |
| 129 | + // 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. |
| 130 | + if ( 20 / a > sigma * sigma ) { |
| 131 | + useTemme = true; |
| 132 | + } |
| 133 | + } else if ( sigma < 0.4 ) { |
| 134 | + useTemme = true; |
| 135 | + } |
| 136 | + } |
| 137 | + if ( useTemme ) { |
| 138 | + evalMethod = 5; |
| 139 | + } |
| 140 | + // 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. |
| 141 | + else if ( x - ( 1.0 / (3.0 * x) ) < a ) { |
| 142 | + evalMethod = 2; |
| 143 | + } else { |
| 144 | + evalMethod = 4; |
| 145 | + invert = !invert; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /* eslint-disable default-case */ |
| 150 | + switch ( evalMethod ) { |
| 151 | + case 0: |
| 152 | + result = finiteGammaQ( a, x ); |
| 153 | + if ( regularized === false ) { |
| 154 | + result *= gamma( a ); |
| 155 | + } |
| 156 | + break; |
| 157 | + case 1: |
| 158 | + result = finiteHalfGammaQ( a, x ); |
| 159 | + if ( regularized === false ) { |
| 160 | + result *= gamma( a ); |
| 161 | + } |
| 162 | + break; |
| 163 | + case 2: |
| 164 | + // Compute P: |
| 165 | + result = ( regularized ) ? |
| 166 | + regularisedGammaPrefix( a, x ) : |
| 167 | + fullIGammaPrefix( a, x ); |
| 168 | + if ( result !== 0.0 ) { |
| 169 | + // 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. |
| 170 | + initValue = 0.0; |
| 171 | + optimisedInvert = false; |
| 172 | + if ( invert ) { |
| 173 | + initValue = ( regularized ) ? 1.0 : gamma( a ); |
| 174 | + if ( |
| 175 | + regularized || |
| 176 | + result >= 1.0 || |
| 177 | + FLOAT64_MAX * result > initValue |
| 178 | + ) { |
| 179 | + initValue /= result; |
| 180 | + if ( |
| 181 | + regularized || |
| 182 | + a < 1.0 || |
| 183 | + ( FLOAT64_MAX / a > initValue ) |
| 184 | + ) { |
| 185 | + initValue *= -a; |
| 186 | + optimisedInvert = true; |
| 187 | + } else { |
| 188 | + initValue = 0.0; |
| 189 | + } |
| 190 | + } else { |
| 191 | + initValue = 0.0; |
| 192 | + } |
| 193 | + } |
| 194 | + result *= lowerGammaSeries( a, x, initValue ) / a; |
| 195 | + if ( optimisedInvert ) { |
| 196 | + invert = false; |
| 197 | + result = -result; |
| 198 | + } |
| 199 | + } |
| 200 | + break; |
| 201 | + case 3: |
| 202 | + // Compute Q: |
| 203 | + invert = !invert; |
| 204 | + res = tgammaSmallUpperPart( a, x, invert ); |
| 205 | + result = res[ 0 ]; |
| 206 | + g = res[ 1 ]; |
| 207 | + invert = false; |
| 208 | + if ( regularized ) { |
| 209 | + result /= g; |
| 210 | + } |
| 211 | + break; |
| 212 | + case 4: |
| 213 | + // Compute Q: |
| 214 | + result = ( regularized ) ? |
| 215 | + regularisedGammaPrefix( a, x ) : |
| 216 | + fullIGammaPrefix( a, x ); |
| 217 | + if ( result !== 0 ) { |
| 218 | + result *= upperGammaFraction( a, x ); |
| 219 | + } |
| 220 | + break; |
| 221 | + case 5: |
| 222 | + result = igammaTemmeLarge( a, x ); |
| 223 | + if ( x >= a ) { |
| 224 | + invert = !invert; |
| 225 | + } |
| 226 | + break; |
| 227 | + case 6: |
| 228 | + // Since `x` is so small that P is necessarily very small too, use http://functions.wolfram.com/GammaBetaErf/GammaRegularized/06/01/05/01/01/ |
| 229 | + result = ( regularized ) ? |
| 230 | + pow( x, a ) / gamma( a + 1.0 ) : |
| 231 | + pow( x, a ) / a; |
| 232 | + result *= 1.0 - ( a * x / ( a + 1.0 ) ); |
| 233 | + break; |
| 234 | + case 7: |
| 235 | + // `x` is large, so compute Q: |
| 236 | + result = ( regularized ) ? |
| 237 | + regularisedGammaPrefix( a, x ) : |
| 238 | + fullIGammaPrefix( a, x ); |
| 239 | + result /= x; |
| 240 | + if ( result !== 0.0 ) { |
| 241 | + result *= tgammaILargeX( a, x ); |
| 242 | + } |
| 243 | + break; |
| 244 | + } |
| 245 | + if ( regularized && result > 1.0 ) { |
| 246 | + result = 1.0; |
| 247 | + } |
| 248 | + if ( invert ) { |
| 249 | + gam = ( regularized ) ? 1.0 : gamma( a ); |
| 250 | + result = gam - result; |
| 251 | + } |
| 252 | + return result; |
| 253 | +} |
| 254 | + |
| 255 | + |
| 256 | +// EXPORTS // |
| 257 | + |
| 258 | +module.exports = igammaFinal; |
0 commit comments