time-to-botec

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

factory.js (1855B)


      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 var SQRT2 = require( '@stdlib/constants/float64/sqrt-two' );
     27 var pow = require( './../../../../base/special/pow' );
     28 var normhermitepoly = require( './../../../../base/tools/normhermitepoly' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Returns a function for evaluating a physicist's Hermite polynomial.
     35 *
     36 * @param {NonNegativeInteger} n - polynomial degree
     37 * @returns {Function} function for evaluating a physicist's Hermite polynomial
     38 *
     39 * @example
     40 * var polyval = factory( 2 );
     41 *
     42 * var v = polyval( 0.5 );
     43 * // returns -1.0
     44 */
     45 function factory( n ) {
     46 	var c;
     47 	if ( n < 0 || isnan( n ) || !isint( n ) ) {
     48 		return constantFunction( NaN );
     49 	}
     50 	if ( n === 0 ) {
     51 		return constantFunction( 1.0 );
     52 	}
     53 	c = pow( 2.0, 0.5*n );
     54 	return polyval;
     55 
     56 	/**
     57 	* Evaluates a physicist's Hermite polynomial.
     58 	*
     59 	* @private
     60 	* @param {number} x - value at which to evaluate a physicist's Hermite polynomial
     61 	* @returns {number} result
     62 	*/
     63 	function polyval( x ) {
     64 		return c * normhermitepoly( n, SQRT2*x );
     65 	}
     66 }
     67 
     68 
     69 // EXPORTS //
     70 
     71 module.exports = factory;