time-to-botec

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

logspace.js (2405B)


      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 pow = require( '@stdlib/math/base/special/pow' );
     24 var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
     25 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
     26 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Generates a logarithmically spaced numeric array.
     33 *
     34 * @param {number} a - exponent of start value
     35 * @param {number} b - exponent of end value
     36 * @param {NonNegativeInteger} [len=10] - length of output array
     37 * @throws {TypeError} first argument must be numeric
     38 * @throws {TypeError} second argument must be numeric
     39 * @throws {TypeError} third argument must be a nonnegative integer
     40 * @returns {Array} logarithmically spaced numeric array
     41 *
     42 * @example
     43 * var arr = logspace( 0, 2, 6 );
     44 * // returns [ 1, ~2.5, ~6.31, ~15.85, ~39.81, 100 ]
     45 */
     46 function logspace( a, b, len ) {
     47 	var arr;
     48 	var end;
     49 	var tmp;
     50 	var d;
     51 	var i;
     52 	if ( !isNumber( a ) || isnan( a ) ) {
     53 		throw new TypeError( 'invalid argument. Exponent of start value must be numeric. Value: `' + a + '`.' );
     54 	}
     55 	if ( !isNumber( b ) || isnan( b ) ) {
     56 		throw new TypeError( 'invalid argument. Exponent of stop value must be numeric. Value: `' + b + '`.' );
     57 	}
     58 	if ( arguments.length < 3 ) {
     59 		len = 10;
     60 	} else {
     61 		if ( !isNonNegativeInteger( len ) ) {
     62 			throw new TypeError( 'invalid argument. Length must be a nonnegative integer. Value: `' + len + '`.' );
     63 		}
     64 		if ( len === 0 ) {
     65 			return [];
     66 		}
     67 	}
     68 	// Calculate the increment:
     69 	end = len - 1;
     70 	d = ( b-a ) / end;
     71 
     72 	// Build the output array...
     73 	arr = [];
     74 	tmp = a;
     75 	arr.push( pow( 10, tmp ) );
     76 	for ( i = 1; i < end; i++ ) {
     77 		tmp += d;
     78 		arr.push( pow( 10, tmp ) );
     79 	}
     80 	arr.push( pow( 10, b ) );
     81 	return arr;
     82 }
     83 
     84 
     85 // EXPORTS //
     86 
     87 module.exports = logspace;