time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

full_igamma_prefix.js (2216B)


      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 exp = require( './../../../../base/special/exp' );
     38 var pow = require( './../../../../base/special/pow' );
     39 var ln = require( './../../../../base/special/ln' );
     40 var MAX_LN = require( '@stdlib/constants/float64/max-ln' );
     41 var MIN_LN = require( '@stdlib/constants/float64/min-ln' );
     42 
     43 
     44 // MAIN //
     45 
     46 /**
     47 * Calculates the power term prefix `(z^a)(e^-z)` used in the non-normalized incomplete gammas.
     48 *
     49 * @private
     50 * @param {number} a - function parameter
     51 * @param {number} z - function parameter
     52 * @returns {number} power term prefix
     53 */
     54 function fullIGammaPrefix( a, z ) {
     55 	var prefix;
     56 	var alz;
     57 
     58 	alz = a * ln( z );
     59 	if ( z >= 1.0 ) {
     60 		if ( ( alz < MAX_LN ) && ( -z > MIN_LN ) ) {
     61 			prefix = pow( z, a ) * exp( -z );
     62 		}
     63 		else if ( a >= 1.0 ) {
     64 			prefix = pow( z / exp(z/a), a );
     65 		}
     66 		else {
     67 			prefix = exp( alz - z );
     68 		}
     69 	}
     70 	else if ( alz > MIN_LN ) {
     71 		prefix = pow( z, a ) * exp( -z );
     72 	}
     73 	else if ( z/a < MAX_LN ) {
     74 		prefix = pow( z / exp(z/a), a );
     75 	}
     76 	else {
     77 		prefix = exp( alz - z );
     78 	}
     79 	return prefix;
     80 }
     81 
     82 
     83 // EXPORTS //
     84 
     85 module.exports = fullIGammaPrefix;