time-to-botec

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

root_finder.js (2012B)


      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_62_0/boost/math/special_functions/detail/ibeta_inverse.hpp}. The implementation has been modified for JavaScript.
     22 *
     23 * ```text
     24 * Copyright John Maddock 2006.
     25 * 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 ln = require( './../../../../base/special/ln' );
     38 var MAX_VALUE = require( '@stdlib/constants/float64/max' );
     39 
     40 
     41 // VARIABLES //
     42 
     43 var BIG = MAX_VALUE / 4.0;
     44 
     45 
     46 // MAIN //
     47 
     48 /**
     49 * Helper function used by root finding code to convert `eta` to `x`.
     50 *
     51 * @private
     52 * @param {number} t - first parameter
     53 * @param {number} a - second parameter
     54 * @returns {Function} root function
     55 */
     56 function temmeRootFinder( t, a ) {
     57 	return roots;
     58 
     59 	/**
     60 	* Calculates roots.
     61 	*
     62 	* @private
     63 	* @param {number} x - function value
     64 	* @returns {Array} function roots
     65 	*/
     66 	function roots( x ) {
     67 		var f1;
     68 		var f;
     69 		var y;
     70 
     71 		y = 1.0 - x;
     72 		if ( y === 0.0 ) {
     73 			return [ -BIG, -BIG ];
     74 		}
     75 		if ( x === 0.0 ) {
     76 			return [ -BIG, -BIG ];
     77 		}
     78 		f = ln( x ) + ( a * ln( y ) ) + t;
     79 		f1 = ( 1.0 / x ) - ( a / y );
     80 		return [ f, f1 ];
     81 	}
     82 }
     83 
     84 
     85 // EXPORTS //
     86 
     87 module.exports = temmeRootFinder;