time-to-botec

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

main.js (1689B)


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