time-to-botec

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

binomcoefln.js (2193B)


      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 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isInteger = require( './../../../../base/assert/is-integer' );
     24 var isnan = require( './../../../../base/assert/is-nan' );
     25 var betaln = require( './../../../../base/special/betaln' );
     26 var abs = require( './../../../../base/special/abs' );
     27 var ln = require( './../../../../base/special/ln' );
     28 var NINF = require( '@stdlib/constants/float64/ninf' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Computes the natural logarithm of the binomial coefficient of two integers.
     35 *
     36 * @param {integer} n - input value
     37 * @param {integer} k - second input value
     38 * @returns {number} function value
     39 *
     40 * @example
     41 * var v = binomcoefln( 8, 2 );
     42 * // returns ~3.332
     43 *
     44 * @example
     45 * var v = binomcoefln( 0, 0 );
     46 * // returns 0.0
     47 *
     48 * @example
     49 * var v = binomcoefln( -4, 2 );
     50 * // returns ~2.303
     51 *
     52 * @example
     53 * var v = binomcoefln( 88, 3 );
     54 * // returns ~11.606
     55 *
     56 * @example
     57 * var v = binomcoefln( NaN, 3 );
     58 * // returns NaN
     59 *
     60 * @example
     61 * var v = binomcoefln( 5, NaN );
     62 * // returns NaN
     63 *
     64 * @example
     65 * var v = binomcoefln( NaN, NaN );
     66 * // returns NaN
     67 */
     68 function binomcoefln( n, k ) {
     69 	if ( isnan( n ) || isnan( k ) ) {
     70 		return NaN;
     71 	}
     72 	if ( !isInteger( n ) || !isInteger( k ) ) {
     73 		return NaN;
     74 	}
     75 	if ( n < 0.0 ) {
     76 		return binomcoefln( -n + k - 1.0, k );
     77 	}
     78 	if ( k < 0 ) {
     79 		return NINF;
     80 	}
     81 	if ( k === 0 ) {
     82 		return 0.0;
     83 	}
     84 	if ( k === 1 ) {
     85 		return ln( abs( n ) );
     86 	}
     87 	if ( n < k ) {
     88 		return NINF;
     89 	}
     90 	if ( n - k < 2 ) {
     91 		return binomcoefln( n, n - k );
     92 	}
     93 	// Case: n - k >= 2
     94 	return -ln( n + 1 ) - betaln( n - k + 1, k + 1 );
     95 }
     96 
     97 
     98 // EXPORTS //
     99 
    100 module.exports = binomcoefln;