time-to-botec

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

loop.js.txt (1189B)


      1 'use strict';
      2 
      3 // MODULES //
      4 
      5 var abs = require( '@stdlib/math/base/special/abs' );
      6 
      7 
      8 // VARIABLES //
      9 
     10 var P = [
     11 {{P}}
     12 ];
     13 var Q = [
     14 {{Q}}
     15 ];
     16 var END = P.length - 1;
     17 
     18 
     19 // MAIN //
     20 
     21 /**
     22 * Evaluates a rational function, i.e., the ratio of two polynomials described by the coefficients stored in \\(P\\) and \\(Q\\).
     23 *
     24 * ## Notes
     25 *
     26 * -   Coefficients should be sorted in ascending degree.
     27 * -   The implementation uses [Horner's rule][horners-method] for efficient computation.
     28 *
     29 * [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
     30 *
     31 *
     32 * @private
     33 * @param {number} x - value at which to evaluate the rational function
     34 * @returns {number} evaluated rational function
     35 */
     36 function evalrational( x ) {
     37 	var s1;
     38 	var s2;
     39 	var i;
     40 
     41 	if ( x === 0.0 ) {
     42 		return {{ratio}};
     43 	}
     44 	if ( abs( x ) <= 1.0 ) {
     45 		s1 = P[ END ];
     46 		s2 = Q[ END ];
     47 		for ( i = END-1; i >= 0; i-- ) {
     48 			s1 *= x;
     49 			s2 *= x;
     50 			s1 += P[ i ];
     51 			s2 += Q[ i ];
     52 		}
     53 	} else {
     54 		x = 1.0 / x; // use inverse to avoid overflow
     55 		s1 = P[ 0 ];
     56 		s2 = Q[ 0 ];
     57 		for ( i = 1; i <= END; i++ ) {
     58 			s1 *= x;
     59 			s2 *= x;
     60 			s1 += P[ i ];
     61 			s2 += Q[ i ];
     62 		}
     63 	}
     64 	return s1 / s2;
     65 }
     66 
     67 
     68 // EXPORTS //
     69 
     70 module.exports = evalrational;