time-to-botec

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

coords_array.js (1583B)


      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 sqrt = require( '@stdlib/math/base/special/sqrt' );
     24 var exp = require( '@stdlib/math/base/special/exp' );
     25 var ln = require( '@stdlib/math/base/special/ln' );
     26 
     27 
     28 // VARIABLES //
     29 
     30 // (R*phi(R) + Pr(X>=R))*sqrt(2\pi)
     31 var V = 9.91256303526217e-3;
     32 
     33 
     34 // MAIN //
     35 
     36 /**
     37 * Returns an array containing coordinates such that each rectangle has the same area.
     38 *
     39 * @private
     40 * @param {PositiveInteger} N - number of rectangles
     41 * @param {number} rTail - start of right tail
     42 * @returns {NumberArray} coordinate array
     43 *
     44 * @example
     45 * var X = coordsArray( 128, 3.44 );
     46 * // returns <Array>
     47 */
     48 function coordsArray( N, rTail ) {
     49 	var X;
     50 	var f;
     51 	var i;
     52 
     53 	f = exp( -0.5 * rTail * rTail );
     54 
     55 	X = [];
     56 	X.push( V/f ); // bottom block: V / f(R)
     57 	X.push( rTail );
     58 	for ( i = 2; i < N; i++ ) {
     59 		X[ i ] = sqrt( -2.0 * ln( ( V/X[i-1] ) + f ) );
     60 		f = exp( -0.5 * X[ i ] * X[ i ] );
     61 	}
     62 	X.push( 0.0 );
     63 	return X;
     64 }
     65 
     66 
     67 // EXPORTS //
     68 
     69 module.exports = coordsArray;