time-to-botec

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

factory.js (7400B)


      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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
     24 var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
     25 var setReadWriteAccessor = require( '@stdlib/utils/define-nonenumerable-read-write-accessor' );
     26 var isObject = require( '@stdlib/assert/is-plain-object' );
     27 var isFunction = require( '@stdlib/assert/is-function' );
     28 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     29 var constantFunction = require( '@stdlib/utils/constant-function' );
     30 var noop = require( '@stdlib/utils/noop' );
     31 var randu = require( './../../../base/mt19937' ).factory;
     32 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     33 var typedarray2json = require( '@stdlib/array/to-json' );
     34 var validate = require( './validate.js' );
     35 var kumaraswamy0 = require( './kumaraswamy.js' );
     36 
     37 
     38 // MAIN //
     39 
     40 /**
     41 * Returns a pseudorandom number generator for generating Kumaraswamy's double bounded distributed random numbers.
     42 *
     43 * @param {PositiveNumber} [a] - first shape parameter
     44 * @param {PositiveNumber} [b] - second shape parameter
     45 * @param {Options} [options] - function options
     46 * @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers
     47 * @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed
     48 * @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state
     49 * @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
     50 * @throws {TypeError} `a` must be a positive number
     51 * @throws {TypeError} `b` must be a positive number
     52 * @throws {TypeError} options argument must be an object
     53 * @throws {TypeError} must provide valid options
     54 * @throws {Error} must provide a valid state
     55 * @returns {PRNG} pseudorandom number generator
     56 *
     57 * @example
     58 * var kumaraswamy = factory( 2.0, 1.0 );
     59 * var v = kumaraswamy();
     60 * // returns <number>
     61 *
     62 * @example
     63 * var kumaraswamy = factory( 2.0, 2.0, {
     64 *     'seed': 297
     65 * });
     66 * var v = kumaraswamy();
     67 * // returns <number>
     68 */
     69 function factory() {
     70 	var opts;
     71 	var rand;
     72 	var prng;
     73 	var err;
     74 	var a;
     75 	var b;
     76 
     77 	if ( arguments.length === 0 ) {
     78 		rand = randu();
     79 	} else if ( arguments.length === 1 ) {
     80 		opts = arguments[ 0 ];
     81 		if ( !isObject( opts ) ) {
     82 			throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + opts + '`.' );
     83 		}
     84 		if ( hasOwnProp( opts, 'prng' ) ) {
     85 			if ( !isFunction( opts.prng ) ) {
     86 				throw new TypeError( 'invalid option. `prng` option must be a pseudorandom number generator function. Option: `' + opts.prng + '`.' );
     87 			}
     88 			rand = opts.prng;
     89 		} else {
     90 			rand = randu( opts );
     91 		}
     92 	} else {
     93 		a = arguments[ 0 ];
     94 		b = arguments[ 1 ];
     95 		err = validate( a, b );
     96 		if ( err ) {
     97 			throw err;
     98 		}
     99 		if ( arguments.length > 2 ) {
    100 			opts = arguments[ 2 ];
    101 			if ( !isObject( opts ) ) {
    102 				throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + opts + '`.' );
    103 			}
    104 			if ( hasOwnProp( opts, 'prng' ) ) {
    105 				if ( !isFunction( opts.prng ) ) {
    106 					throw new TypeError( 'invalid option. `prng` option must be a pseudorandom number generator function. Option: `' + opts.prng + '`.' );
    107 				}
    108 				rand = opts.prng;
    109 			} else {
    110 				rand = randu( opts );
    111 			}
    112 		} else {
    113 			rand = randu();
    114 		}
    115 	}
    116 	if ( a === void 0 ) {
    117 		prng = kumaraswamy2;
    118 	} else {
    119 		prng = kumaraswamy1;
    120 	}
    121 	setReadOnly( prng, 'NAME', 'kumaraswamy' );
    122 
    123 	// If we are provided an "external" PRNG, we don't support getting or setting PRNG state, as we'd need to check for compatible state value types, etc, entailing considerable complexity.
    124 	if ( opts && opts.prng ) {
    125 		setReadOnly( prng, 'seed', null );
    126 		setReadOnly( prng, 'seedLength', null );
    127 		setReadWriteAccessor( prng, 'state', constantFunction( null ), noop );
    128 		setReadOnly( prng, 'stateLength', null );
    129 		setReadOnly( prng, 'byteLength', null );
    130 		setReadOnly( prng, 'toJSON', constantFunction( null ) );
    131 		setReadOnly( prng, 'PRNG', rand );
    132 	} else {
    133 		setReadOnlyAccessor( prng, 'seed', getSeed );
    134 		setReadOnlyAccessor( prng, 'seedLength', getSeedLength );
    135 		setReadWriteAccessor( prng, 'state', getState, setState );
    136 		setReadOnlyAccessor( prng, 'stateLength', getStateLength );
    137 		setReadOnlyAccessor( prng, 'byteLength', getStateSize );
    138 		setReadOnly( prng, 'toJSON', toJSON );
    139 		setReadOnly( prng, 'PRNG', rand );
    140 		rand = rand.normalized;
    141 	}
    142 	return prng;
    143 
    144 	/**
    145 	* Returns the PRNG seed.
    146 	*
    147 	* @private
    148 	* @returns {PRNGSeedMT19937} seed
    149 	*/
    150 	function getSeed() {
    151 		return rand.seed;
    152 	}
    153 
    154 	/**
    155 	* Returns the PRNG seed length.
    156 	*
    157 	* @private
    158 	* @returns {PositiveInteger} seed length
    159 	*/
    160 	function getSeedLength() {
    161 		return rand.seedLength;
    162 	}
    163 
    164 	/**
    165 	* Returns the PRNG state length.
    166 	*
    167 	* @private
    168 	* @returns {PositiveInteger} state length
    169 	*/
    170 	function getStateLength() {
    171 		return rand.stateLength;
    172 	}
    173 
    174 	/**
    175 	* Returns the PRNG state size (in bytes).
    176 	*
    177 	* @private
    178 	* @returns {PositiveInteger} state size (in bytes)
    179 	*/
    180 	function getStateSize() {
    181 		return rand.byteLength;
    182 	}
    183 
    184 	/**
    185 	* Returns the current pseudorandom number generator state.
    186 	*
    187 	* @private
    188 	* @returns {PRNGStateMT19937} current state
    189 	*/
    190 	function getState() {
    191 		return rand.state;
    192 	}
    193 
    194 	/**
    195 	* Sets the pseudorandom number generator state.
    196 	*
    197 	* @private
    198 	* @param {PRNGStateMT19937} s - generator state
    199 	* @throws {Error} must provide a valid state
    200 	*/
    201 	function setState( s ) {
    202 		rand.state = s;
    203 	}
    204 
    205 	/**
    206 	* Serializes the pseudorandom number generator as a JSON object.
    207 	*
    208 	* ## Notes
    209 	*
    210 	* -   `JSON.stringify()` implicitly calls this method when stringifying a PRNG.
    211 	*
    212 	* @private
    213 	* @returns {Object} JSON representation
    214 	*/
    215 	function toJSON() {
    216 		var out = {};
    217 		out.type = 'PRNG';
    218 		out.name = prng.NAME;
    219 		out.state = typedarray2json( rand.state );
    220 		if ( a === void 0 ) {
    221 			out.params = [];
    222 		} else {
    223 			out.params = [ a, b ];
    224 		}
    225 		return out;
    226 	}
    227 
    228 	/**
    229 	* Returns a random number drawn from a Kumaraswamy's double bounded distribution with bound parameter values.
    230 	*
    231 	* @private
    232 	* @returns {Probability} pseudorandom number
    233 	*
    234 	* @example
    235 	* var v = beta1();
    236 	* // returns <number>
    237 	*/
    238 	function kumaraswamy1() {
    239 		return kumaraswamy0( rand, a, b );
    240 	}
    241 
    242 	/**
    243 	* Returns a random number drawn from a Kumaraswamy's double bounded distribution.
    244 	*
    245 	* @private
    246 	* @param {PositiveNumber} a - first shape parameter
    247 	* @param {PositiveNumber} b - second shape parameter
    248 	* @returns {Probability} pseudorandom number
    249 	*
    250 	* @example
    251 	* var v = kumaraswamy2( 2.0, 3.0 );
    252 	* // returns <number>
    253 	*/
    254 	function kumaraswamy2( a, b ) {
    255 		if (
    256 			isnan( a ) ||
    257 			isnan( b ) ||
    258 			a <= 0.0 ||
    259 			b <= 0.0
    260 		) {
    261 			return NaN;
    262 		}
    263 		return kumaraswamy0( rand, a, b );
    264 	}
    265 }
    266 
    267 
    268 // EXPORTS //
    269 
    270 module.exports = factory;