factory.js (7401B)
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 isnan = require( '@stdlib/math/base/assert/is-nan' ); 32 var randn = require( './../../../base/improved-ziggurat' ).factory; 33 var typedarray2json = require( '@stdlib/array/to-json' ); 34 var validate = require( './validate.js' ); 35 var cauchy0 = require( './cauchy.js' ); 36 37 38 // MAIN // 39 40 /** 41 * Returns a pseudorandom number generator for generating random numbers from a Cauchy distribution. 42 * 43 * @param {number} [x0] - location parameter 44 * @param {PositiveNumber} [gamma] - scale 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} `x0` must be a number 51 * @throws {TypeError} `gamma` argument 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 cauchy = factory( 0.0, 1.0 ); 59 * var v = cauchy(); 60 * // returns <number> 61 * 62 * @example 63 * var cauchy = factory( -3.0, 0.5, { 64 * 'seed': 297 65 * }); 66 * var v = cauchy(); 67 * // returns <number> 68 */ 69 function factory() { 70 var gamma; 71 var rnorm; 72 var opts; 73 var rand; 74 var prng; 75 var err; 76 var x0; 77 78 if ( arguments.length === 0 ) { 79 rnorm = randn(); 80 } else if ( arguments.length === 1 ) { 81 opts = arguments[ 0 ]; 82 if ( !isObject( opts ) ) { 83 throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + opts + '`.' ); 84 } 85 if ( hasOwnProp( opts, 'prng' ) ) { 86 if ( !isFunction( opts.prng ) ) { 87 throw new TypeError( 'invalid option. `prng` option must be a pseudorandom number generator function. Option: `' + opts.prng + '`.' ); 88 } 89 rnorm = randn({ 90 'prng': opts.prng 91 }); 92 } else { 93 rnorm = randn( opts ); 94 } 95 } else { 96 x0 = arguments[ 0 ]; 97 gamma = arguments[ 1 ]; 98 err = validate( x0, gamma ); 99 if ( err ) { 100 throw err; 101 } 102 if ( arguments.length > 2 ) { 103 opts = arguments[ 2 ]; 104 if ( !isObject( opts ) ) { 105 throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + opts + '`.' ); 106 } 107 if ( hasOwnProp( opts, 'prng' ) ) { 108 if ( !isFunction( opts.prng ) ) { 109 throw new TypeError( 'invalid option. `prng` option must be a pseudorandom number generator function. Option: `' + opts.prng + '`.' ); 110 } 111 rnorm = randn({ 112 'prng': opts.prng 113 }); 114 } else { 115 rnorm = randn( opts ); 116 } 117 } else { 118 rnorm = randn(); 119 } 120 } 121 if ( x0 === void 0 ) { 122 prng = cauchy2; 123 } else { 124 prng = cauchy1; 125 } 126 rand = rnorm.PRNG; 127 128 setReadOnly( prng, 'NAME', 'cauchy' ); 129 130 // 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. 131 if ( opts && opts.prng ) { 132 setReadOnly( prng, 'seed', null ); 133 setReadOnly( prng, 'seedLength', null ); 134 setReadWriteAccessor( prng, 'state', constantFunction( null ), noop ); 135 setReadOnly( prng, 'stateLength', null ); 136 setReadOnly( prng, 'byteLength', null ); 137 setReadOnly( prng, 'toJSON', constantFunction( null ) ); 138 } else { 139 setReadOnlyAccessor( prng, 'seed', getSeed ); 140 setReadOnlyAccessor( prng, 'seedLength', getSeedLength ); 141 setReadWriteAccessor( prng, 'state', getState, setState ); 142 setReadOnlyAccessor( prng, 'stateLength', getStateLength ); 143 setReadOnlyAccessor( prng, 'byteLength', getStateSize ); 144 setReadOnly( prng, 'toJSON', toJSON ); 145 } 146 setReadOnly( prng, 'PRNG', rand ); 147 return prng; 148 149 /** 150 * Returns the PRNG seed. 151 * 152 * @private 153 * @returns {PRNGSeedMT19937} seed 154 */ 155 function getSeed() { 156 return rand.seed; 157 } 158 159 /** 160 * Returns the PRNG seed length. 161 * 162 * @private 163 * @returns {PositiveInteger} seed length 164 */ 165 function getSeedLength() { 166 return rand.seedLength; 167 } 168 169 /** 170 * Returns the PRNG state length. 171 * 172 * @private 173 * @returns {PositiveInteger} state length 174 */ 175 function getStateLength() { 176 return rand.stateLength; 177 } 178 179 /** 180 * Returns the PRNG state size (in bytes). 181 * 182 * @private 183 * @returns {PositiveInteger} state size (in bytes) 184 */ 185 function getStateSize() { 186 return rand.byteLength; 187 } 188 189 /** 190 * Returns the current pseudorandom number generator state. 191 * 192 * @private 193 * @returns {PRNGStateMT19937} current state 194 */ 195 function getState() { 196 return rand.state; 197 } 198 199 /** 200 * Sets the pseudorandom number generator state. 201 * 202 * @private 203 * @param {PRNGStateMT19937} s - generator state 204 * @throws {Error} must provide a valid state 205 */ 206 function setState( s ) { 207 rand.state = s; 208 } 209 210 /** 211 * Serializes the pseudorandom number generator as a JSON object. 212 * 213 * ## Notes 214 * 215 * - `JSON.stringify()` implicitly calls this method when stringifying a PRNG. 216 * 217 * @private 218 * @returns {Object} JSON representation 219 */ 220 function toJSON() { 221 var out = {}; 222 out.type = 'PRNG'; 223 out.name = prng.NAME; 224 out.state = typedarray2json( rand.state ); 225 if ( x0 === void 0 ) { 226 out.params = []; 227 } else { 228 out.params = [ x0, gamma ]; 229 } 230 return out; 231 } 232 233 /** 234 * Returns pseudorandom number drawn from a Cauchy distribution with bound parameters `x0` and `gamma`. 235 * 236 * @private 237 * @returns {number} pseudorandom number 238 * 239 * @example 240 * var v = cauchy1(); 241 * // returns <number> 242 */ 243 function cauchy1() { 244 return cauchy0( rnorm, x0, gamma ); 245 } 246 247 /** 248 * Returns pseudorandom number drawn from a Cauchy distribution. 249 * 250 * @private 251 * @param {number} x0 - location parameter 252 * @param {PositiveNumber} gamma - scale parameter 253 * @returns {number} pseudorandom number 254 * 255 * @example 256 * var v = cauchy2( 0.0, 2.0 ); 257 * // returns <number> 258 * 259 * @example 260 * var r = cauchy2( 1.0, -1.5 ); 261 * // returns NaN 262 */ 263 function cauchy2( x0, gamma ) { 264 if ( 265 isnan( x0 ) || 266 isnan( gamma ) || 267 gamma <= 0.0 268 ) { 269 return NaN; 270 } 271 return cauchy0( rnorm, x0, gamma ); 272 } 273 } 274 275 276 // EXPORTS // 277 278 module.exports = factory;