All files main.js

62.35% Statements 275/441
95.65% Branches 22/23
33.33% Functions 1/3
62.35% Lines 275/441

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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 4421x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 78x 78x 78x 1x 1x 1x     78x 9x 9x 68x 78x 78x 58x 78x 13x 13x 55x 55x 55x 78x 78x 45x 78x 13x 13x 42x 42x 78x 9x 9x 78x 10x 10x 23x 23x 23x 78x 19x 9x 9x 10x 10x 10x 10x 10x 10x 19x 4x 4x 4x 4x 4x 4x 4x 4x 78x 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  
/**
* @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.
*/
 
/* eslint-disable no-restricted-syntax, no-invalid-this */
 
'use strict';
 
// MODULES //
 
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives;
var isEmptyCollection = require( '@stdlib/assert/is-empty-collection' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isFunction = require( '@stdlib/assert/is-function' );
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
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 isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' );
var isOutputDataTypePolicy = require( '@stdlib/ndarray/base/assert/is-output-data-type-policy' );
var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' );
var isOrder = require( '@stdlib/ndarray/base/assert/is-order' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var contains = require( '@stdlib/array/base/assert/contains' );
var filledBy = require( '@stdlib/array/base/filled-by' );
var everyBy = require( '@stdlib/array/base/every-by' );
var join = require( '@stdlib/array/base/join' );
var nullaryStrided = require( '@stdlib/strided/base/nullary' );
var unary = require( '@stdlib/ndarray/base/unary' );
var broadcast = require( '@stdlib/ndarray/base/maybe-broadcast-array' );
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var dtypes2strings = require( '@stdlib/ndarray/base/dtypes2strings' );
var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' );
var unaryOutputDataType = require( '@stdlib/ndarray/base/unary-output-dtype' );
var numel = require( '@stdlib/ndarray/base/numel' );
var buffer = require( '@stdlib/ndarray/base/buffer' );
var getShape = require( '@stdlib/ndarray/shape' );
var getDType = require( '@stdlib/ndarray/dtype' );
var getOrder = require( '@stdlib/ndarray/order' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var defaults = require( '@stdlib/ndarray/defaults' );
var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' );
var accessorSetter = require( '@stdlib/array/base/accessor-setter' );
var setter = require( '@stdlib/array/base/setter' );
var format = require( '@stdlib/string/format' );
var validate = require( './validate.js' );
 
 
// MAIN //
 
/**
* Constructor for creating ndarrays filled with pseudorandom values drawn from a unary PRNG.
*
* @constructor
* @param {Function} prng - unary pseudorandom value generator
* @param {ArrayLikeObject} idtypes - list of supported input data types
* @param {ArrayLikeObject} odtypes - list of supported output data types
* @param {Object} policies - policies
* @param {string} policies.output - output data type policy
* @param {Object} [options] - function options
* @param {string} [options.order] - default memory layout
* @throws {TypeError} first argument must be a function
* @throws {TypeError} second argument must be an array of supported data types
* @throws {TypeError} third argument must be an array of supported data types
* @throws {TypeError} fourth argument must be an object having supported policies
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @returns {Random} instance
*
* @example
* var dtypes = require( '@stdlib/ndarray/dtypes' );
* var exponential = require( '@stdlib/random/base/exponential' );
*
* 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 rand = new Random( exponential, idt, odt, policies, options );
*
* var v = rand.generate( [ 2, 2 ], 2.0 );
* // returns <ndarray>
*
* @example
* var dtypes = require( '@stdlib/ndarray/dtypes' );
* var ndzeros = require( '@stdlib/ndarray/zeros' );
* var exponential = require( '@stdlib/random/base/exponential' );
*
* 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 rand = new Random( exponential, idt, odt, policies, options );
*
* var out = ndzeros( [ 2, 2 ] );
* var v = rand.assign( 2.0, out );
* // returns <ndarray>
*
* var bool = ( v === out );
* // returns true
*/
function Random( prng, idtypes, odtypes, policies, options ) {
	var opts;
	if ( !( this instanceof Random ) ) {
		if ( arguments.length < 5 ) {
			return new Random( prng, idtypes, odtypes, policies );
		}
		return new Random( prng, idtypes, odtypes, policies, options );
	}
	if ( !isFunction( prng ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', prng ) );
	}
	if (
		!isCollection( idtypes ) ||
		idtypes.length < 1 ||
		!everyBy( idtypes, isDataType )
	) {
		throw new TypeError( format( 'invalid argument. Second argument must be an array of data types. Value: `%s`.', idtypes ) );
	}
	idtypes = dtypes2strings( idtypes );
 
	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 ) );
	}
	opts = {
		'order': defaults.get( 'order' )
	};
	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' ) ) {
			opts.order = options.order;
			if ( !isOrder( opts.order ) ) {
				throw new TypeError( format( 'invalid option. `%s` option must be a valid memory layout. Option: `%s`.', 'order', opts.order ) );
			}
		}
	}
	this._prng = prng;
	this._idtypes = idtypes;
	this._odtypes = odtypes;
	this._policies = {
		'output': policies.output
	};
	this._opts = opts;
	return this;
}
 
/**
* Returns an ndarray filled with pseudorandom values drawn from a unary PRNG.
*
* @name generate
* @memberof Random.prototype
* @type {Function}
* @param {NonNegativeIntegerArray} shape - output shape
* @param {(ndarrayLike|*)} param1 - PRNG parameter
* @param {Options} [options] - function options
* @param {*} [options.dtype] - output ndarray 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 {ArrayLikeObject<string>} [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 ndarray
*
* @example
* var dtypes = require( '@stdlib/ndarray/dtypes' );
* var exponential = require( '@stdlib/random/base/exponential' );
*
* 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 rand = new Random( exponential, idt, odt, policies, options );
*
* var v = rand.generate( [ 2, 2 ], 2.0 );
* // returns <ndarray>
*/
setReadOnly( Random.prototype, 'generate', function generate( shape, param1, options ) {
	var prng;
	var opts;
	var buf;
	var err;
	var len;
	var ord;
	var FLG;
	var out;
	var set;
	var dt;
	var st;
	var sh;
	var p1;

	if ( !isNonNegativeIntegerArray( shape ) && !isEmptyCollection( shape ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be an array-like object containing nonnegative integers. Value: `%s`.', shape ) );
	}
	opts = {};
	if ( arguments.length > 2 ) {
		err = validate( opts, this._odtypes, options );
		if ( err ) {
			throw err;
		}
	}
	// Check whether we've been provided a scalar PRNG parameter...
	if ( isNumber( param1 ) ) {
		p1 = param1;
		dt = 'float64';
		FLG = true;
	} else if ( isComplexLike( param1 ) ) {
		p1 = param1;
		if ( p1.byteLength === 8 ) {
			dt = 'complex64';
		} else {
			dt = 'complex128';
		}
		FLG = true;
	} else if ( isndarrayLike( param1 ) ) {
		dt = getDType( param1 );
		sh = getShape( param1 );

		// Check whether the PRNG parameter is a zero-dimensional array...
		if ( sh.length === 0 ) {
			p1 = param1.get();
			FLG = true;
		} else {
			// Broadcast the PRNG parameter to the desired shape:
			p1 = broadcast( param1, shape ); // delegate to `broadcast` to ensure broadcast compatibility
			FLG = false;
		}
	} else {
		p1 = param1;
		dt = 'generic';
		FLG = true;
	}
	if ( !contains( this._idtypes, resolveStr( dt ) ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must have one of the following data types: "%s". Data type: `%s`.', join( this._idtypes, '", "' ), dt ) );
	}
	prng = this._prng;
	dt = opts.dtype || unaryOutputDataType( dt, this._policies.output );
	if ( opts.order ) {
		ord = opts.order;
	} else if ( FLG ) {
		ord = this._opts.order;
	} else {
		ord = getOrder( p1 );
	}
	// Handles special cases involving (1) zero-dimensional output ndarrays and (2) shapes having at least one dimension of size zero...
	len = numel( shape );
	if ( len === 0 ) {
		// Check for a zero-dimensional shape...
		if ( shape.length === 0 ) {
			// If the output array is a zero-dimensional ndarray, we generate a single random variate...
			buf = buffer( dt, 1 );
			if ( isAccessorArray( buf ) ) {
				set = accessorSetter( dt );
			} else {
				set = setter( dt );
			}
			set( buf, 0, prng( p1 ) );
			st = [ 0 ];
		} else {
			// If the output array has one or more dimensions of size zero, we avoid allocating any memory, and instead return an empty ndarray...
			buf = buffer( dt, len );
			st = shape2strides( shape, ord );
		}
		return new ndarray( dt, buf, shape, st, 0, ord, opts );
	}
	// If provided a scalar PRNG parameter, we can simply fill a linear buffer with pseudorandom values (as all pseudorandom values are drawn from the same PRNG) and then wrap as an ndarray...
	if ( FLG ) {
		if ( isEqualDataType( dt, 'generic' ) ) {
			buf = filledBy( len, wrapper );
		} else {
			buf = buffer( dt, len );
			nullaryStrided( [ buf ], [ len ], [ 1 ], wrapper );
		}
		st = shape2strides( shape, ord );
		return new ndarray( dt, buf, shape, st, 0, ord, opts );
	}
	// We've been provided an ndarray, so we need to perform element-wise iteration...

	// Allocate a data buffer:
	buf = buffer( dt, len );

	// Initialize the output array:
	st = shape2strides( shape, ord );
	out = new ndarray( dt, buf, shape, st, 0, ord, opts );

	// Fill the output array with pseudorandom values:
	unary( [ p1, out ], prng );

	return out;

	/**
	* Applies parameters to a pseudorandom number generator function.
	*
	* @private
	* @returns {(number|ComplexLike)} pseudorandom number
	*/
	function wrapper() {
		return prng( p1 );
	}
});
 
/**
* Fills an ndarray with pseudorandom values drawn from a unary PRNG.
*
* @name assign
* @memberof Random.prototype
* @type {Function}
* @param {(ndarrayLike|*)} param1 - PRNG parameter
* @param {ndarrayLike} out - output ndarray
* @throws {TypeError} second argument must be an ndarray
* @throws {TypeError} must provide valid PRNG parameters
* @throws {TypeError} PRNG parameters and the output ndarray must be broadcast compatible
* @throws {Error} cannot write to a read-only ndarray
* @returns {ndarray} output ndarray
*
* @example
* var dtypes = require( '@stdlib/ndarray/dtypes' );
* var ndzeros = require( '@stdlib/ndarray/zeros' );
* var exponential = require( '@stdlib/random/base/exponential' );
*
* 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 rand = new Random( exponential, idt, odt, policies, options );
*
* var out = ndzeros( [ 2, 2 ] );
* var v = rand.assign( 2.0, out );
* // returns <ndarray>
*
* var bool = ( v === out );
* // returns true
*/
setReadOnly( Random.prototype, 'assign', function assign( param1, out ) {
	var pdt;
	var odt;
	var p1;
	if ( !isndarrayLike( out ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', out ) );
	}
	if ( isReadOnly( out ) ) {
		throw new Error( 'invalid argument. The output ndarray must be writable. Cannot write to a read-only ndarray.' );
	}
	// Check whether we've been provided a scalar PRNG parameter...
	if ( isNumber( param1 ) ) {
		// Wrap the scalar in a broadcasted ndarray:
		pdt = 'float64';
		p1 = broadcastScalar( param1, pdt, getShape( out ), getOrder( out ) );
	} else if ( isComplexLike( param1 ) ) {
		if ( param1.byteLength === 8 ) {
			pdt = 'complex64';
		} else {
			pdt = 'complex128';
		}
		// Wrap the scalar in a broadcasted ndarray:
		p1 = broadcastScalar( param1, pdt, getShape( out ), getOrder( out ) );
	} else if ( isndarrayLike( param1 ) ) {
		// Broadcast the PRNG parameter to the desired shape:
		pdt = getDType( param1 );
		p1 = broadcast( param1, getShape( out ) ); // delegate to `broadcast` to ensure broadcast compatibility
	} else {
		// Wrap the scalar in a broadcasted ndarray:
		pdt = 'generic';
		p1 = broadcastScalar( param1, pdt, getShape( out ), getOrder( out ) );
	}
	if ( !contains( this._idtypes, resolveStr( pdt ) ) ) {
		throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( this._idtypes, '", "' ), pdt ) );
	}
	odt = getDType( out );
	if ( !contains( this._odtypes, resolveStr( odt ) ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must have one of the following data types: "%s". Data type: `%s`.', join( this._odtypes, '", "' ), odt ) );
	}
	// Fill the output array with pseudorandom values:
	unary( [ p1, out ], this._prng );

	return out;
});
 
 
// EXPORTS //
 
module.exports = Random;