Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 82x 82x 82x 82x 82x 82x 9x 9x 82x 1x 1x 82x 9x 9x 63x 82x 120x 120x 120x 120x 117x 120x 6x 6x 114x 114x 57x 82x 82x 46x 82x 14x 14x 43x 43x 82x 9x 9x 82x 11x 11x 23x 82x 22x 9x 9x 13x 13x 13x 11x 11x 13x 22x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 82x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
var setReadWriteAccessor = require( '@stdlib/utils/define-nonenumerable-read-write-accessor' );
var isMethodIn = require( '@stdlib/assert/is-method-in' );
var isFunction = require( '@stdlib/assert/is-function' );
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var isObject = require( '@stdlib/assert/is-object' );
var isCollection = require( '@stdlib/assert/is-collection' );
var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' );
var isOutputDataTypePolicy = require( '@stdlib/ndarray/base/assert/is-output-data-type-policy' );
var isOrder = require( '@stdlib/ndarray/base/assert/is-order' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var everyBy = require( '@stdlib/array/base/every-by' );
var copy = require( '@stdlib/array/base/copy' );
var dtypes2strings = require( '@stdlib/ndarray/base/dtypes2strings' );
var constantFunction = require( '@stdlib/utils/constant-function' );
var noop = require( '@stdlib/utils/noop' );
var Random = require( '@stdlib/random/tools/binary' );
var format = require( '@stdlib/string/format' );
// MAIN //
/**
* Returns a function for generating pseudorandom values drawn from a binary PRNG.
*
* @param {Function} prng - binary pseudorandom value generator
* @param {Function} prng.factory - method which returns a new binary pseudorandom value generator
* @param {ArrayLikeObject<ArrayLikeObject>} idtypes - list containing a list of supported input data types for each PRNG parameter
* @param {ArrayLikeObject} odtypes - list of supported output data types
* @param {Object} policies - policies
* @param {string} policies.output - output data type policy
* @param {Object} options - options
* @param {string} options.order - default memory layout (either row-major or column-major)
* @throws {TypeError} first argument must be a function
* @throws {TypeError} first argument must have a `factory` method
* @throws {TypeError} second argument must be an array containing arrays of supported data types
* @throws {TypeError} third argument must be an array of supported data types
* @throws {TypeError} fourth argument must be an object
* @throws {TypeError} must provide valid options
* @returns {Function} function which returns a function for creating arrays
*
* @example
* var dtypes = require( '@stdlib/ndarray/dtypes' );
* var uniform = require( '@stdlib/random/base/uniform' );
*
* var idt = dtypes( 'real_and_generic' );
* var odt = dtypes( 'real_floating_point_and_generic' );
*
* var policies = {
* 'output': 'real_floating_point_and_generic'
* };
* var options = {
* 'order': 'row-major'
* };
*
* var factory = createFactory( uniform, [ idt, idt ], odt, policies, options );
*
* var rand = factory();
* // returns <Function>
*
* var v = rand( [ 2, 2 ], 0.0, 1.0 );
* // returns <ndarray>
*
* @example
* var dtypes = require( '@stdlib/ndarray/dtypes' );
* var ndzeros = require( '@stdlib/ndarray/zeros' );
* var uniform = require( '@stdlib/random/base/uniform' );
*
* var idt = dtypes( 'real_and_generic' );
* var odt = dtypes( 'real_floating_point_and_generic' );
*
* var policies = {
* 'output': 'real_floating_point_and_generic'
* };
* var options = {
* 'order': 'row-major'
* };
*
* var factory = createFactory( uniform, [ idt, idt ], odt, policies, options );
*
* var rand = factory();
* // returns <Function>
*
* var out = ndzeros( [ 2, 2 ] );
* var v = rand.assign( 0.0, 1.0, out );
* // returns <ndarray>
*
* var bool = ( v === out );
* // returns true
*/
function createFactory( prng, idtypes, odtypes, policies, options ) {
var OPTIONS;
var dt;
var i;
if ( !isFunction( prng ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', prng ) );
}
if ( !isMethodIn( prng, 'factory' ) ) {
throw new TypeError( format( 'invalid argument. First argument must have a `%s` method.', 'factory' ) );
}
if ( !isCollection( idtypes ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', idtypes ) );
}
idtypes = copy( idtypes );
for ( i = 0; i < idtypes.length; i++ ) {
dt = idtypes[ i ];
if (
!isCollection( dt ) ||
dt.length < 1 ||
!everyBy( dt, isDataType )
) {
throw new TypeError( format( 'invalid argument. Second argument must contain arrays of data types. Value: `%s`.', idtypes ) );
}
idtypes[ i ] = dtypes2strings( dt );
}
if (
!isCollection( odtypes ) ||
odtypes.length < 1 ||
!everyBy( odtypes, isDataType )
) {
throw new TypeError( format( 'invalid argument. Third argument must be an array of data types. Value: `%s`.', odtypes ) );
}
odtypes = dtypes2strings( odtypes );
if ( !isObject( policies ) ) {
throw new TypeError( format( 'invalid argument. Fourth argument must be an object. Value: `%s`.', policies ) );
}
if ( !isOutputDataTypePolicy( policies.output ) ) {
throw new TypeError( format( 'invalid argument. Fourth argument must be an object having a supported output data type policy. Value: `%s`.', policies.output ) );
}
OPTIONS = {};
if ( arguments.length > 4 ) {
if ( !isPlainObject( options ) ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
}
if ( hasOwnProp( options, 'order' ) ) {
OPTIONS.order = options.order;
if ( !isOrder( OPTIONS.order ) ) {
throw new TypeError( format( 'invalid option. `%s` option must be a valid memory layout. Option: `%s`.', 'order', OPTIONS.order ) );
}
}
}
return factory;
/**
* Returns a function for generating pseudorandom values drawn from a binary PRNG.
*
* @private
* @param {Options} [options] - function options
* @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers
* @param {*} [options.seed] - pseudorandom value generator seed
* @param {*} [options.state] - pseudorandom value generator state
* @param {boolean} [options.copy] - boolean indicating whether to copy a provided pseudorandom value generator state
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @throws {Error} must provide a valid state
* @returns {Function} function for creating ndarrays
*/
function factory() {
var random;
var base;
var opts;
if ( arguments.length > 0 ) {
opts = arguments[ 0 ];
if ( !isPlainObject( opts ) ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) );
}
base = prng.factory( opts );
} else {
opts = {};
base = prng;
}
random = new Random( base, idtypes, odtypes, policies, OPTIONS );
if ( opts && opts.prng ) {
setReadOnly( rand, 'seed', null );
setReadOnly( rand, 'seedLength', null );
setReadWriteAccessor( rand, 'state', constantFunction( null ), noop );
setReadOnly( rand, 'stateLength', null );
setReadOnly( rand, 'byteLength', null );
} else {
setReadOnlyAccessor( rand, 'seed', getSeed );
setReadOnlyAccessor( rand, 'seedLength', getSeedLength );
setReadWriteAccessor( rand, 'state', getState, setState );
setReadOnlyAccessor( rand, 'stateLength', getStateLength );
setReadOnlyAccessor( rand, 'byteLength', getStateSize );
}
setReadOnly( rand, 'PRNG', base.PRNG );
setReadOnly( rand, 'assign', assign );
return rand;
/**
* Returns an ndarray filled with pseudorandom values drawn from a binary PRNG.
*
* @private
* @param {NonNegativeIntegerArray} shape - output ndarray shape
* @param {(ndarrayLike|*)} param1 - first PRNG parameter
* @param {(ndarrayLike|*)} param2 - second PRNG parameter
* @param {Options} [options] - function options
* @param {*} [options.dtype] - output data type
* @param {string} [options.order] - memory layout (either row-major or column-major)
* @param {string} [options.mode] - specifies how to handle indices which exceed ndarray dimensions
* @param {StringArray} [options.submode] - specifies how to handle subscripts which exceed ndarray dimensions on a per dimension basis
* @param {boolean} [options.readonly] - boolean indicating whether an ndarray should be read-only
* @throws {TypeError} first argument must be a valid shape
* @throws {TypeError} must provide valid PRNG parameters
* @throws {TypeError} PRNG parameters and the desired shape must be broadcast compatible
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @returns {ndarray} output array
*/
function rand( shape, param1, param2, options ) {
if ( arguments.length < 4 ) {
return random.generate( shape, param1, param2 );
}
return random.generate( shape, param1, param2, options );
}
/**
* Fills an ndarray with pseudorandom values drawn from a binary PRNG.
*
* @private
* @param {(ndarrayLike|*)} param1 - first PRNG parameter
* @param {(ndarrayLike|*)} param2 - second PRNG parameter
* @param {ndarrayLike} out - output ndarray
* @throws {TypeError} third argument must be an ndarray
* @throws {TypeError} must provide valid PRNG parameters
* @throws {TypeError} PRNG parameters and the output ndarray must be broadcast compatible
* @returns {ndarray} output ndarray
*/
function assign( param1, param2, out ) {
return random.assign( param1, param2, out );
}
/**
* Returns the PRNG seed.
*
* @private
* @returns {*} seed
*/
function getSeed() {
return rand.PRNG.seed;
}
/**
* Returns the PRNG seed length.
*
* @private
* @returns {PositiveInteger} seed length
*/
function getSeedLength() {
return rand.PRNG.seedLength;
}
/**
* Returns the PRNG state length.
*
* @private
* @returns {PositiveInteger} state length
*/
function getStateLength() {
return rand.PRNG.stateLength;
}
/**
* Returns the PRNG state size (in bytes).
*
* @private
* @returns {PositiveInteger} state size (in bytes)
*/
function getStateSize() {
return rand.PRNG.byteLength;
}
/**
* Returns the current pseudorandom value generator state.
*
* @private
* @returns {*} current state
*/
function getState() {
return rand.PRNG.state;
}
/**
* Sets the pseudorandom value generator state.
*
* @private
* @param {*} s - generator state
* @throws {Error} must provide a valid state
*/
function setState( s ) {
rand.PRNG.state = s;
}
}
}
// EXPORTS //
module.exports = createFactory;
|