igamma_temme_large.js (3132B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 http://www.boost.org/doc/libs/1_37_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 evalpoly = require( './../../../../base/tools/evalpoly' ); 38 var erfc = require( './../../../../base/special/erfc' ); 39 var sqrt = require( './../../../../base/special/sqrt' ); 40 var exp = require( './../../../../base/special/exp' ); 41 var ln = require( './../../../../base/special/ln' ); 42 var TWO_PI = require( '@stdlib/constants/float64/two-pi' ); 43 var polyvalC0 = require( './polyval_c0.js' ); 44 var polyvalC1 = require( './polyval_c1.js' ); 45 var polyvalC2 = require( './polyval_c2.js' ); 46 var polyvalC3 = require( './polyval_c3.js' ); 47 var polyvalC4 = require( './polyval_c4.js' ); 48 var polyvalC5 = require( './polyval_c5.js' ); 49 var polyvalC6 = require( './polyval_c6.js' ); 50 var polyvalC7 = require( './polyval_c7.js' ); 51 var polyvalC8 = require( './polyval_c8.js' ); 52 53 54 // VARIABLES // 55 56 // Pre-allocate workspace array: 57 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 58 59 60 // MAIN // 61 62 /** 63 * Asymptotic expansions of the incomplete gamma functions when `a` is large and `x ~ a` (IEEE double precision or 10^-17). 64 * 65 * @private 66 * @param {number} a - function parameter 67 * @param {number} x - function parameter 68 * @returns {number} value of asymptotic expansion 69 */ 70 function igammaTemmeLarge( a, x ) { 71 var result; 72 var sigma; 73 var phi; 74 var y; 75 var z; 76 77 sigma = ( x-a ) / a; 78 phi = -ln( 1.0 + sigma ) + sigma; 79 y = a * phi; 80 z = sqrt( 2.0 * phi ); 81 if ( x < a ) { 82 z = -z; 83 } 84 workspace[ 0 ] = polyvalC0( z ); 85 workspace[ 1 ] = polyvalC1( z ); 86 workspace[ 2 ] = polyvalC2( z ); 87 workspace[ 3 ] = polyvalC3( z ); 88 workspace[ 4 ] = polyvalC4( z ); 89 workspace[ 5 ] = polyvalC5( z ); 90 workspace[ 6 ] = polyvalC6( z ); 91 workspace[ 7 ] = polyvalC7( z ); 92 workspace[ 8 ] = polyvalC8( z ); 93 workspace[ 9 ] = -0.00059676129019274625; 94 result = evalpoly( workspace, 1.0/a ); 95 result *= exp( -y ) / sqrt( TWO_PI * a ); 96 if ( x < a ) { 97 result = -result; 98 } 99 result += erfc( sqrt(y) ) / 2.0; 100 return result; 101 } 102 103 104 // EXPORTS // 105 106 module.exports = igammaTemmeLarge;