time-to-botec

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

factory.js (1793B)


      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 constantFunction = require( '@stdlib/utils/constant-function' );
     24 var isnan = require( './../../../../base/assert/is-nan' );
     25 var isint = require( './../../../../base/assert/is-integer' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns a function for evaluating a normalized Hermite polynomial.
     32 *
     33 * @param {NonNegativeInteger} n - polynomial degree
     34 * @returns {Function} function for evaluating a normalized Hermite polynomial
     35 *
     36 * @example
     37 * var polyval = factory( 2 );
     38 *
     39 * var v = polyval( 0.5 );
     40 * // returns -0.75
     41 */
     42 function factory( n ) {
     43 	if ( n < 0 || isnan( n ) || !isint( n ) ) {
     44 		return constantFunction( NaN );
     45 	}
     46 	if ( n === 0 ) {
     47 		return constantFunction( 1.0 );
     48 	}
     49 	return polyval;
     50 
     51 	/**
     52 	* Evaluates a normalized Hermite polynomial.
     53 	*
     54 	* @private
     55 	* @param {number} x - value at which to evaluate a normalized Hermite polynomial
     56 	* @returns {number} result
     57 	*/
     58 	function polyval( x ) {
     59 		var y1;
     60 		var y2;
     61 		var y3;
     62 		var i;
     63 
     64 		if ( isnan( x ) ) {
     65 			return NaN;
     66 		}
     67 		y2 = 1.0;
     68 		y3 = 0.0;
     69 		for ( i = n; i > 1; i-- ) {
     70 			y1 = (x*y2) - (i*y3);
     71 			y3 = y2;
     72 			y2 = y1;
     73 		}
     74 		return (x*y2) - y3;
     75 	}
     76 }
     77 
     78 
     79 // EXPORTS //
     80 
     81 module.exports = factory;