time-to-botec

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

attransitionplus.js (3179B)


      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_65_0/boost/math/special_functions/detail/polygamma.hpp}. The implementation follows the original but has been modified for JavaScript.
     22 *
     23 * ```text
     24 * (C) Copyright Nikhar Agrawal 2013.
     25 * (C) Copyright Christopher Kormanyos 2013.
     26 * (C) Copyright John Maddock 2014.
     27 * (C) Copyright Paul Bristow 2013.
     28 *
     29 * Use, modification and distribution are subject to the
     30 * Boost Software License, Version 1.0. (See accompanying file
     31 * LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
     32 * ```
     33 */
     34 
     35 'use strict';
     36 
     37 // MODULES //
     38 
     39 var logger = require( 'debug' );
     40 var factorial = require( './../../../../base/special/factorial' );
     41 var gammaln = require( './../../../../base/special/gammaln' );
     42 var trunc = require( './../../../../base/special/trunc' );
     43 var exp = require( './../../../../base/special/exp' );
     44 var pow = require( './../../../../base/special/pow' );
     45 var ln = require( './../../../../base/special/ln' );
     46 var MAX_LN = require( '@stdlib/constants/float64/max-ln' );
     47 var atinfinityplus = require( './atinfinityplus.js' );
     48 
     49 
     50 // VARIABLES //
     51 
     52 var debug = logger( 'polygamma' );
     53 var MAX_SERIES_ITERATIONS = 1000000;
     54 var DIGITS_BASE10 = 19;
     55 
     56 
     57 // MAIN //
     58 
     59 /**
     60 * Evaluates the polygamma function.
     61 *
     62 * @private
     63 * @param {PositiveInteger} n - derivative to evaluate
     64 * @param {number} x - input
     65 * @returns {number} (n+1)'th derivative
     66 * @see {@link http://functions.wolfram.com/GammaBetaErf/PolyGamma2/16/01/01/0017/}
     67 */
     68 function attransitionplus( n, x ) {
     69 	var minusMminus1;
     70 	var lnterm;
     71 	var zpows;
     72 	var iter;
     73 	var sum0;
     74 	var d4d;
     75 	var N;
     76 	var m;
     77 	var k;
     78 	var z;
     79 
     80 	// Use N = (0.4 * digits) + (4 * n) for target value for x:
     81 	d4d = 0.4 * DIGITS_BASE10;
     82 	N = d4d + ( 4*n );
     83 	m = n;
     84 	iter = N - trunc( x );
     85 
     86 	if ( iter > MAX_SERIES_ITERATIONS ) {
     87 		debug( 'Exceeded maximum series evaluations when evaluated at n = %d and x = %d', n, x );
     88 		return NaN;
     89 	}
     90 	minusMminus1 = -m - 1;
     91 	z = x;
     92 	sum0 = 0.0;
     93 	zpows = 0.0;
     94 
     95 	// Forward recursion to larger `x`, need to check for overflow first though:
     96 	if ( ln( z+iter ) * minusMminus1 > -MAX_LN ) {
     97 		for ( k = 1; k <= iter; k++ ) {
     98 			zpows = pow( z, minusMminus1 );
     99 			sum0 += zpows;
    100 			z += 1;
    101 		}
    102 		sum0 *= factorial( n );
    103 	} else {
    104 		for ( k = 1; k <= iter; k++ ) {
    105 			lnterm = ( ln( z ) * minusMminus1 ) + gammaln( n+1 );
    106 			sum0 += exp( lnterm );
    107 			z += 1;
    108 		}
    109 	}
    110 	if ( ( n-1 ) & 1 ) {
    111 		sum0 = -sum0;
    112 	}
    113 	return sum0 + atinfinityplus( n, z );
    114 }
    115 
    116 
    117 // EXPORTS //
    118 
    119 module.exports = attransitionplus;