@stdlib/random-array-invgamma 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CITATION.cff +30 -0
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +440 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +246 -0
- package/lib/defaults.json +3 -0
- package/lib/factory.js +281 -0
- package/lib/index.js +77 -0
- package/lib/main.js +58 -0
- package/lib/validate.js +72 -0
- package/package.json +115 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../lib/defaults.json", "../lib/validate.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"],
|
|
4
|
+
"sourcesContent": ["{\n\t\"dtype\": \"float64\"\n}\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2023 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar dtypes = require( '@stdlib/array-typed-real-float-dtypes' );\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar format = require( '@stdlib/string-format' );\n\n\n// VARIABLES //\n\nvar DTYPES = dtypes();\nDTYPES.push( 'generic' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {string} [options.dtype] - output array data type\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'dtype': 'float64'\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'dtype' ) ) {\n\t\topts.dtype = options.dtype;\n\t\tif ( DTYPES.indexOf( opts.dtype ) < 0 ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be one of the following: \"%s\". Option: `%s`.', 'dtype', DTYPES.join( '\", \"' ), opts.dtype ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2023 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isNonNegativeInteger = require( '@stdlib/assert-is-nonnegative-integer' ).isPrimitive;\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar setReadOnlyAccessor = require( '@stdlib/utils-define-nonenumerable-read-only-accessor' );\nvar setReadWriteAccessor = require( '@stdlib/utils-define-nonenumerable-read-write-accessor' );\nvar constantFunction = require( '@stdlib/utils-constant-function' );\nvar noop = require( '@stdlib/utils-noop' );\nvar base = require( '@stdlib/random-base-invgamma' );\nvar ctors = require( '@stdlib/array-typed-real-float-ctors' );\nvar filledBy = require( '@stdlib/array-base-filled-by' );\nvar nullary = require( '@stdlib/strided-base-nullary' );\nvar binary = require( '@stdlib/strided-base-binary' );\nvar format = require( '@stdlib/string-format' );\nvar defaults = require( './defaults.json' );\nvar validate = require( './validate.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function for creating arrays containing pseudorandom numbers drawn from an inverse gamma distribution.\n*\n* @param {PositiveNumber} [alpha] - shape parameter\n* @param {PositiveNumber} [beta] - scale parameter\n* @param {Options} [options] - function options\n* @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers\n* @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed\n* @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state\n* @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state\n* @param {string} [options.dtype=\"float64\"] - default data type\n* @throws {TypeError} `alpha` must be a positive number\n* @throws {TypeError} `beta` must be a positive number\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {Error} must provide a valid state\n* @returns {Function} function for creating arrays\n*\n* @example\n* var invgamma = factory( 2.0, 5.0 );\n* // returns <Function>\n*\n* var arr = invgamma( 10 );\n* // returns <Float64Array>\n*\n* @example\n* var invgamma = factory( 2.0, 5.0 );\n* // returns <Function>\n*\n* var arr = invgamma( 10, {\n* 'dtype': 'generic'\n* });\n* // returns [...]\n*/\nfunction factory() {\n\tvar options;\n\tvar nargs;\n\tvar opts;\n\tvar rand;\n\tvar prng;\n\tvar err;\n\n\topts = {\n\t\t'dtype': defaults.dtype\n\t};\n\n\tnargs = arguments.length;\n\tif ( nargs === 0 ) {\n\t\tprng = base;\n\t\trand = invgamma2;\n\t} else if ( nargs === 1 ) {\n\t\toptions = arguments[ 0 ];\n\t\tprng = base.factory( options );\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\trand = invgamma2;\n\t} else if ( nargs === 2 ) {\n\t\tprng = base.factory( arguments[ 0 ], arguments[ 1 ] );\n\t\trand = invgamma1;\n\t} else if ( nargs === 3 ) {\n\t\toptions = arguments[ 2 ];\n\t\tprng = base.factory( arguments[ 0 ], arguments[ 1 ], options );\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\trand = invgamma1;\n\t}\n\tif ( options && options.prng ) {\n\t\tsetReadOnly( rand, 'seed', null );\n\t\tsetReadOnly( rand, 'seedLength', null );\n\t\tsetReadWriteAccessor( rand, 'state', constantFunction( null ), noop );\n\t\tsetReadOnly( rand, 'stateLength', null );\n\t\tsetReadOnly( rand, 'byteLength', null );\n\t} else {\n\t\tsetReadOnlyAccessor( rand, 'seed', getSeed );\n\t\tsetReadOnlyAccessor( rand, 'seedLength', getSeedLength );\n\t\tsetReadWriteAccessor( rand, 'state', getState, setState );\n\t\tsetReadOnlyAccessor( rand, 'stateLength', getStateLength );\n\t\tsetReadOnlyAccessor( rand, 'byteLength', getStateSize );\n\t}\n\tsetReadOnly( rand, 'PRNG', prng.PRNG );\n\treturn rand;\n\n\t/**\n\t* Returns an array containing pseudorandom numbers drawn from an inverse gamma distribution with parameters `alpha` (shape parameter) and `beta` (scale parameter).\n\t*\n\t* @private\n\t* @param {NonNegativeInteger} len - array length\n\t* @param {Options} [options] - function options\n\t* @param {string} [options.dtype] - output array data type\n\t* @throws {TypeError} first argument must be a nonnegative integer\n\t* @throws {TypeError} options argument must be an object\n\t* @throws {TypeError} must provide valid options\n\t* @returns {(Array|TypedArray)} output array\n\t*/\n\tfunction invgamma1( len, options ) {\n\t\tvar ctor;\n\t\tvar out;\n\t\tvar err;\n\t\tvar dt;\n\t\tvar o;\n\t\tif ( !isNonNegativeInteger( len ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a nonnegative integer. Value: `%s`.', len ) );\n\t\t}\n\t\to = {};\n\t\tif ( arguments.length > 1 ) {\n\t\t\terr = validate( o, options );\n\t\t\tif ( err ) {\n\t\t\t\tthrow err;\n\t\t\t}\n\t\t}\n\t\tdt = o.dtype || opts.dtype;\n\t\tif ( dt === 'generic' ) {\n\t\t\treturn filledBy( len, prng );\n\t\t}\n\t\tctor = ctors( dt );\n\t\tout = new ctor( len );\n\t\tnullary( [ out ], [ len ], [ 1 ], prng );\n\t\treturn out;\n\t}\n\n\t/**\n\t* Returns an array containing pseudorandom numbers drawn from an inverse gamma distribution with parameters `alpha` (shape parameter) and `beta` (scale parameter).\n\t*\n\t* @private\n\t* @param {NonNegativeInteger} len - array length\n\t* @param {PositiveNumber} alpha - shape parameter\n\t* @param {PositiveNumber} beta - scale parameter\n\t* @param {Options} [options] - function options\n\t* @param {string} [options.dtype] - output array data type\n\t* @throws {TypeError} first argument must be a nonnegative integer\n\t* @throws {TypeError} options argument must be an object\n\t* @throws {TypeError} must provide valid options\n\t* @returns {(Array|TypedArray)} output array\n\t*/\n\tfunction invgamma2( len, alpha, beta, options ) {\n\t\tvar ctor;\n\t\tvar out;\n\t\tvar err;\n\t\tvar dt;\n\t\tvar o;\n\t\tif ( !isNonNegativeInteger( len ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a nonnegative integer. Value: `%s`.', len ) );\n\t\t}\n\t\to = {};\n\t\tif ( arguments.length > 3 ) {\n\t\t\terr = validate( o, options );\n\t\t\tif ( err ) {\n\t\t\t\tthrow err;\n\t\t\t}\n\t\t}\n\t\t// NOTE: we could alternatively use the PRNG factory function to create a PRNG function which applies parameters for each invocation; however, this would impose a one-time cost which is likely to be rather expensive when generating small arrays. Decision made here to avoid this cost, despite a small cost due to repeatedly validating parameters for each generated pseudorandom number. Additionally, the current implementation has the added benefit that it mirrors the underlying PRNG where invalid parameters result in a return value of `NaN`.\n\t\tdt = o.dtype || opts.dtype;\n\t\tif ( dt === 'generic' ) {\n\t\t\treturn filledBy( len, wrapper );\n\t\t}\n\t\tctor = ctors( dt );\n\t\tout = new ctor( len );\n\t\tbinary( [ [ alpha ], [ beta ], out ], [ len ], [ 0, 0, 1 ], prng );\n\t\treturn out;\n\n\t\t/**\n\t\t* Applies parameters to a pseudorandom number generator function.\n\t\t*\n\t\t* @private\n\t\t* @returns {number} pseudorandom number\n\t\t*/\n\t\tfunction wrapper() {\n\t\t\treturn prng( alpha, beta );\n\t\t}\n\t}\n\n\t/**\n\t* Returns the PRNG seed.\n\t*\n\t* @private\n\t* @returns {PRNGSeedMT19937} seed\n\t*/\n\tfunction getSeed() {\n\t\treturn rand.PRNG.seed;\n\t}\n\n\t/**\n\t* Returns the PRNG seed length.\n\t*\n\t* @private\n\t* @returns {PositiveInteger} seed length\n\t*/\n\tfunction getSeedLength() {\n\t\treturn rand.PRNG.seedLength;\n\t}\n\n\t/**\n\t* Returns the PRNG state length.\n\t*\n\t* @private\n\t* @returns {PositiveInteger} state length\n\t*/\n\tfunction getStateLength() {\n\t\treturn rand.PRNG.stateLength;\n\t}\n\n\t/**\n\t* Returns the PRNG state size (in bytes).\n\t*\n\t* @private\n\t* @returns {PositiveInteger} state size (in bytes)\n\t*/\n\tfunction getStateSize() {\n\t\treturn rand.PRNG.byteLength;\n\t}\n\n\t/**\n\t* Returns the current pseudorandom number generator state.\n\t*\n\t* @private\n\t* @returns {PRNGStateMT19937} current state\n\t*/\n\tfunction getState() {\n\t\treturn rand.PRNG.state;\n\t}\n\n\t/**\n\t* Sets the pseudorandom number generator state.\n\t*\n\t* @private\n\t* @param {PRNGStateMT19937} s - generator state\n\t* @throws {Error} must provide a valid state\n\t*/\n\tfunction setState( s ) {\n\t\trand.PRNG.state = s;\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2023 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Returns an array containing pseudorandom numbers drawn from an inverse gamma distribution with parameters `alpha` (shape parameter) and `beta` (scale parameter).\n*\n* @name invgamma\n* @type {Function}\n* @param {NonNegativeInteger} len - array length\n* @param {PositiveNumber} alpha - shape parameter\n* @param {PositiveNumber} beta - scale parameter\n* @param {Options} [options] - options\n* @param {string} [options.dtype=\"float64\"] - output array data type\n* @throws {TypeError} first argument must be a nonnegative integer\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @returns {(Array|TypedArray)} output array\n*\n* @example\n* var arr = invgamma( 10, 2.0, 5.0 );\n* // returns <Float64Array>\n*\n* @example\n* var arr = invgamma( 10, 2.0, 5.0, {\n* 'dtype': 'generic'\n* });\n* // returns [...]\n*/\nvar invgamma = factory();\n\n\n// EXPORTS //\n\nmodule.exports = invgamma;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2023 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Create an array containing pseudorandom numbers drawn from an inverse gamma distribution with parameters `alpha` (shape parameter) and `beta` (scale parameter).\n*\n* @module @stdlib/random-array-invgamma\n*\n* @example\n* var invgamma = require( '@stdlib/random-array-invgamma' );\n*\n* var arr = invgamma( 10, 2.0, 5.0 );\n* // returns <Float64Array>\n*\n* @example\n* var invgamma = require( '@stdlib/random-array-invgamma' );\n*\n* var arr = invgamma( 10, 2.0, 5.0, {\n* 'dtype': 'generic'\n* });\n* // returns [...]\n*\n* @example\n* var invgamma = require( '@stdlib/random-array-invgamma' );\n*\n* var rand = invgamma.factory( 2.0, 5.0 );\n* // returns <Function>\n*\n* var arr = rand( 10 );\n* // returns <Float64Array>\n*\n* @example\n* var invgamma = require( '@stdlib/random-array-invgamma' );\n*\n* var rand = invgamma.factory( 2.0, 5.0 );\n* // returns <Function>\n*\n* var arr = rand( 10, {\n* 'dtype': 'generic'\n* });\n* // returns [...]\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n\n// exports: { \"factory\": \"main.factory\" }\n"],
|
|
5
|
+
"mappings": "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",
|
|
6
|
+
"names": ["require_defaults", "__commonJSMin", "exports", "module", "require_validate", "__commonJSMin", "exports", "module", "dtypes", "isObject", "hasOwnProp", "format", "DTYPES", "validate", "opts", "options", "require_factory", "__commonJSMin", "exports", "module", "isNonNegativeInteger", "setReadOnly", "setReadOnlyAccessor", "setReadWriteAccessor", "constantFunction", "noop", "base", "ctors", "filledBy", "nullary", "binary", "format", "defaults", "validate", "factory", "options", "nargs", "opts", "rand", "prng", "err", "invgamma2", "invgamma1", "getSeed", "getSeedLength", "getState", "setState", "getStateLength", "getStateSize", "len", "ctor", "out", "dt", "o", "alpha", "beta", "wrapper", "s", "require_main", "__commonJSMin", "exports", "module", "factory", "invgamma", "setReadOnly", "main", "factory"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2023 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
|
+
// TypeScript Version: 4.1
|
|
20
|
+
|
|
21
|
+
/// <reference types="@stdlib/types"/>
|
|
22
|
+
|
|
23
|
+
import { FloatDataType, FloatTypedArray } from '@stdlib/types/array';
|
|
24
|
+
import * as random from '@stdlib/types/random';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Supported data types.
|
|
28
|
+
*/
|
|
29
|
+
type DataType = FloatDataType | 'generic';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Output array.
|
|
33
|
+
*/
|
|
34
|
+
type RandomArray = FloatTypedArray | Array<number>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Interface defining PRNG options.
|
|
38
|
+
*/
|
|
39
|
+
interface PRNGOptions {
|
|
40
|
+
/**
|
|
41
|
+
* Pseudorandom number generator which generates uniformly distributed pseudorandom numbers.
|
|
42
|
+
*/
|
|
43
|
+
prng?: random.PRNG;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Pseudorandom number generator seed.
|
|
47
|
+
*/
|
|
48
|
+
seed?: random.PRNGSeedMT19937;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Pseudorandom number generator state.
|
|
52
|
+
*/
|
|
53
|
+
state?: random.PRNGStateMT19937;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Specifies whether to copy a provided pseudorandom number generator state. Default: true.
|
|
57
|
+
*/
|
|
58
|
+
copy?: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Interface defining factory options.
|
|
63
|
+
*/
|
|
64
|
+
interface FactoryOptions extends PRNGOptions {
|
|
65
|
+
/**
|
|
66
|
+
* Default output array data type. Default: 'float64'.
|
|
67
|
+
*/
|
|
68
|
+
dtype?: DataType;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Interface defining options.
|
|
73
|
+
*/
|
|
74
|
+
interface Options {
|
|
75
|
+
/**
|
|
76
|
+
* Output array data type.
|
|
77
|
+
*/
|
|
78
|
+
dtype?: DataType;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Interface for PRNG properties and methods.
|
|
83
|
+
*/
|
|
84
|
+
interface PRNG {
|
|
85
|
+
/**
|
|
86
|
+
* Underlying pseudorandom number generator.
|
|
87
|
+
*/
|
|
88
|
+
readonly PRNG: random.PRNG;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* PRNG seed.
|
|
92
|
+
*/
|
|
93
|
+
readonly seed: random.PRNGSeedMT19937 | null;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* PRNG seed length.
|
|
97
|
+
*/
|
|
98
|
+
readonly seedLength: number | null;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* PRNG state.
|
|
102
|
+
*/
|
|
103
|
+
state: random.PRNGStateMT19937 | null;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* PRNG state length.
|
|
107
|
+
*/
|
|
108
|
+
readonly stateLength: number | null;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* PRNG state size (in bytes).
|
|
112
|
+
*/
|
|
113
|
+
readonly byteLength: number | null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Interface for generating pseudorandom numbers drawn from an inverse gamma distribution with pre-specified parameter values.
|
|
118
|
+
*/
|
|
119
|
+
interface UnaryFunction extends PRNG {
|
|
120
|
+
/**
|
|
121
|
+
* Returns an array containing pseudorandom numbers drawn from an inverse gamma distribution with parameters `alpha` (shape parameter) and `beta` (scale parameter).
|
|
122
|
+
*
|
|
123
|
+
* @param len - array length
|
|
124
|
+
* @param options - function options
|
|
125
|
+
* @returns output array
|
|
126
|
+
*/
|
|
127
|
+
( len: number, options?: Options ): RandomArray;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Interface for generating pseudorandom numbers drawn from an inverse gamma distribution without pre-specified parameter values.
|
|
132
|
+
*/
|
|
133
|
+
interface TernaryFunction extends PRNG {
|
|
134
|
+
/**
|
|
135
|
+
* Returns an array containing pseudorandom numbers drawn from an inverse gamma distribution with parameters `alpha` (shape parameter) and `beta` (scale parameter).
|
|
136
|
+
*
|
|
137
|
+
* @param len - array length
|
|
138
|
+
* @param alpha - shape parameter
|
|
139
|
+
* @param beta - scale parameter
|
|
140
|
+
* @param options - function options
|
|
141
|
+
* @returns output array
|
|
142
|
+
*/
|
|
143
|
+
( len: number, alpha: number, beta: number, options?: Options ): RandomArray; // tslint:disable-line: max-line-length
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Interface for generating pseudorandom numbers drawn from an inverse gamma distribution.
|
|
148
|
+
*/
|
|
149
|
+
interface Random extends PRNG {
|
|
150
|
+
/**
|
|
151
|
+
* Returns an array containing pseudorandom numbers drawn from an inverse gamma distribution with parameters `alpha` (shape parameter) and `beta` (scale parameter).
|
|
152
|
+
*
|
|
153
|
+
* @param len - array length
|
|
154
|
+
* @param alpha - shape parameter
|
|
155
|
+
* @param beta - scale parameter
|
|
156
|
+
* @param options - function options
|
|
157
|
+
* @returns output array
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* var out = invgamma( 10, 2.0, 5.0 );
|
|
161
|
+
* // returns <Float64Array>
|
|
162
|
+
*/
|
|
163
|
+
( len: number, alpha: number, beta: number, options?: Options ): RandomArray; // tslint:disable-line: max-line-length
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Returns a function for creating arrays containing pseudorandom numbers drawn from an inverse gamma distribution.
|
|
167
|
+
*
|
|
168
|
+
* ## Notes
|
|
169
|
+
*
|
|
170
|
+
* - When provided `alpha` and `beta`, the returned function returns random variates drawn from the specified distribution.
|
|
171
|
+
*
|
|
172
|
+
* @param alpha - shape parameter
|
|
173
|
+
* @param beta - scale parameter
|
|
174
|
+
* @param options - function options
|
|
175
|
+
* @throws `alpha` must be a positive number
|
|
176
|
+
* @throws `beta` must be a positive number
|
|
177
|
+
* @throws must provide a valid state
|
|
178
|
+
* @returns function for creating arrays
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* var random = invgamma.factory( 2.0, 5.0 );
|
|
182
|
+
*
|
|
183
|
+
* var out = random( 10 );
|
|
184
|
+
* // returns <Float64Array>
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* var random = invgamma.factory( 2.0, 5.0, {
|
|
188
|
+
* 'seed': 297
|
|
189
|
+
* });
|
|
190
|
+
* var out = random( 10 );
|
|
191
|
+
* // returns <Float64Array>
|
|
192
|
+
*/
|
|
193
|
+
factory( alpha: number, beta: number, options?: FactoryOptions ): UnaryFunction; // tslint:disable-line: max-line-length
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Returns a function for creating arrays containing pseudorandom numbers drawn from an inverse gamma distribution.
|
|
197
|
+
*
|
|
198
|
+
* ## Notes
|
|
199
|
+
*
|
|
200
|
+
* - When not provided `alpha` and `beta`, the returned function requires that both `alpha` and `beta` be provided at each invocation.
|
|
201
|
+
*
|
|
202
|
+
* @param options - function options
|
|
203
|
+
* @throws must provide a valid state
|
|
204
|
+
* @returns function for creating arrays
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* var random = beta.factory();
|
|
208
|
+
*
|
|
209
|
+
* var out = random( 10, 2.0, 5.0 );
|
|
210
|
+
* // returns <Float64Array>
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* var random = beta.factory({
|
|
214
|
+
* 'seed': 297
|
|
215
|
+
* });
|
|
216
|
+
* var out = random( 10, 2.0, 5.0 );
|
|
217
|
+
* // returns <Float64Array>
|
|
218
|
+
*/
|
|
219
|
+
factory( options?: FactoryOptions ): TernaryFunction;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Returns an array containing pseudorandom numbers drawn from an inverse gamma distribution with parameters `alpha` (shape parameter) and `beta` (scale parameter).
|
|
224
|
+
*
|
|
225
|
+
* @param len - array length
|
|
226
|
+
* @param alpha - shape parameter
|
|
227
|
+
* @param beta - scale parameter
|
|
228
|
+
* @param options - function options
|
|
229
|
+
* @returns output array
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* var out = invgamma( 10, 2.0, 5.0 );
|
|
233
|
+
* // returns <Float64Array>
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* var random = invgamma.factory( 2.0, 5.0 );
|
|
237
|
+
*
|
|
238
|
+
* var out = random( 10 );
|
|
239
|
+
* // returns <Float64Array>
|
|
240
|
+
*/
|
|
241
|
+
declare var invgamma: Random;
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
// EXPORTS //
|
|
245
|
+
|
|
246
|
+
export = invgamma;
|
package/lib/factory.js
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2023 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 isNonNegativeInteger = require( '@stdlib/assert-is-nonnegative-integer' ).isPrimitive;
|
|
24
|
+
var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
|
|
25
|
+
var setReadOnlyAccessor = require( '@stdlib/utils-define-nonenumerable-read-only-accessor' );
|
|
26
|
+
var setReadWriteAccessor = require( '@stdlib/utils-define-nonenumerable-read-write-accessor' );
|
|
27
|
+
var constantFunction = require( '@stdlib/utils-constant-function' );
|
|
28
|
+
var noop = require( '@stdlib/utils-noop' );
|
|
29
|
+
var base = require( '@stdlib/random-base-invgamma' );
|
|
30
|
+
var ctors = require( '@stdlib/array-typed-real-float-ctors' );
|
|
31
|
+
var filledBy = require( '@stdlib/array-base-filled-by' );
|
|
32
|
+
var nullary = require( '@stdlib/strided-base-nullary' );
|
|
33
|
+
var binary = require( '@stdlib/strided-base-binary' );
|
|
34
|
+
var format = require( '@stdlib/string-format' );
|
|
35
|
+
var defaults = require( './defaults.json' );
|
|
36
|
+
var validate = require( './validate.js' );
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
// MAIN //
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns a function for creating arrays containing pseudorandom numbers drawn from an inverse gamma distribution.
|
|
43
|
+
*
|
|
44
|
+
* @param {PositiveNumber} [alpha] - shape parameter
|
|
45
|
+
* @param {PositiveNumber} [beta] - scale parameter
|
|
46
|
+
* @param {Options} [options] - function options
|
|
47
|
+
* @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers
|
|
48
|
+
* @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed
|
|
49
|
+
* @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state
|
|
50
|
+
* @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
|
|
51
|
+
* @param {string} [options.dtype="float64"] - default data type
|
|
52
|
+
* @throws {TypeError} `alpha` must be a positive number
|
|
53
|
+
* @throws {TypeError} `beta` must be a positive number
|
|
54
|
+
* @throws {TypeError} options argument must be an object
|
|
55
|
+
* @throws {TypeError} must provide valid options
|
|
56
|
+
* @throws {Error} must provide a valid state
|
|
57
|
+
* @returns {Function} function for creating arrays
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* var invgamma = factory( 2.0, 5.0 );
|
|
61
|
+
* // returns <Function>
|
|
62
|
+
*
|
|
63
|
+
* var arr = invgamma( 10 );
|
|
64
|
+
* // returns <Float64Array>
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* var invgamma = factory( 2.0, 5.0 );
|
|
68
|
+
* // returns <Function>
|
|
69
|
+
*
|
|
70
|
+
* var arr = invgamma( 10, {
|
|
71
|
+
* 'dtype': 'generic'
|
|
72
|
+
* });
|
|
73
|
+
* // returns [...]
|
|
74
|
+
*/
|
|
75
|
+
function factory() {
|
|
76
|
+
var options;
|
|
77
|
+
var nargs;
|
|
78
|
+
var opts;
|
|
79
|
+
var rand;
|
|
80
|
+
var prng;
|
|
81
|
+
var err;
|
|
82
|
+
|
|
83
|
+
opts = {
|
|
84
|
+
'dtype': defaults.dtype
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
nargs = arguments.length;
|
|
88
|
+
if ( nargs === 0 ) {
|
|
89
|
+
prng = base;
|
|
90
|
+
rand = invgamma2;
|
|
91
|
+
} else if ( nargs === 1 ) {
|
|
92
|
+
options = arguments[ 0 ];
|
|
93
|
+
prng = base.factory( options );
|
|
94
|
+
err = validate( opts, options );
|
|
95
|
+
if ( err ) {
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
rand = invgamma2;
|
|
99
|
+
} else if ( nargs === 2 ) {
|
|
100
|
+
prng = base.factory( arguments[ 0 ], arguments[ 1 ] );
|
|
101
|
+
rand = invgamma1;
|
|
102
|
+
} else if ( nargs === 3 ) {
|
|
103
|
+
options = arguments[ 2 ];
|
|
104
|
+
prng = base.factory( arguments[ 0 ], arguments[ 1 ], options );
|
|
105
|
+
err = validate( opts, options );
|
|
106
|
+
if ( err ) {
|
|
107
|
+
throw err;
|
|
108
|
+
}
|
|
109
|
+
rand = invgamma1;
|
|
110
|
+
}
|
|
111
|
+
if ( options && options.prng ) {
|
|
112
|
+
setReadOnly( rand, 'seed', null );
|
|
113
|
+
setReadOnly( rand, 'seedLength', null );
|
|
114
|
+
setReadWriteAccessor( rand, 'state', constantFunction( null ), noop );
|
|
115
|
+
setReadOnly( rand, 'stateLength', null );
|
|
116
|
+
setReadOnly( rand, 'byteLength', null );
|
|
117
|
+
} else {
|
|
118
|
+
setReadOnlyAccessor( rand, 'seed', getSeed );
|
|
119
|
+
setReadOnlyAccessor( rand, 'seedLength', getSeedLength );
|
|
120
|
+
setReadWriteAccessor( rand, 'state', getState, setState );
|
|
121
|
+
setReadOnlyAccessor( rand, 'stateLength', getStateLength );
|
|
122
|
+
setReadOnlyAccessor( rand, 'byteLength', getStateSize );
|
|
123
|
+
}
|
|
124
|
+
setReadOnly( rand, 'PRNG', prng.PRNG );
|
|
125
|
+
return rand;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Returns an array containing pseudorandom numbers drawn from an inverse gamma distribution with parameters `alpha` (shape parameter) and `beta` (scale parameter).
|
|
129
|
+
*
|
|
130
|
+
* @private
|
|
131
|
+
* @param {NonNegativeInteger} len - array length
|
|
132
|
+
* @param {Options} [options] - function options
|
|
133
|
+
* @param {string} [options.dtype] - output array data type
|
|
134
|
+
* @throws {TypeError} first argument must be a nonnegative integer
|
|
135
|
+
* @throws {TypeError} options argument must be an object
|
|
136
|
+
* @throws {TypeError} must provide valid options
|
|
137
|
+
* @returns {(Array|TypedArray)} output array
|
|
138
|
+
*/
|
|
139
|
+
function invgamma1( len, options ) {
|
|
140
|
+
var ctor;
|
|
141
|
+
var out;
|
|
142
|
+
var err;
|
|
143
|
+
var dt;
|
|
144
|
+
var o;
|
|
145
|
+
if ( !isNonNegativeInteger( len ) ) {
|
|
146
|
+
throw new TypeError( format( 'invalid argument. First argument must be a nonnegative integer. Value: `%s`.', len ) );
|
|
147
|
+
}
|
|
148
|
+
o = {};
|
|
149
|
+
if ( arguments.length > 1 ) {
|
|
150
|
+
err = validate( o, options );
|
|
151
|
+
if ( err ) {
|
|
152
|
+
throw err;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
dt = o.dtype || opts.dtype;
|
|
156
|
+
if ( dt === 'generic' ) {
|
|
157
|
+
return filledBy( len, prng );
|
|
158
|
+
}
|
|
159
|
+
ctor = ctors( dt );
|
|
160
|
+
out = new ctor( len );
|
|
161
|
+
nullary( [ out ], [ len ], [ 1 ], prng );
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Returns an array containing pseudorandom numbers drawn from an inverse gamma distribution with parameters `alpha` (shape parameter) and `beta` (scale parameter).
|
|
167
|
+
*
|
|
168
|
+
* @private
|
|
169
|
+
* @param {NonNegativeInteger} len - array length
|
|
170
|
+
* @param {PositiveNumber} alpha - shape parameter
|
|
171
|
+
* @param {PositiveNumber} beta - scale parameter
|
|
172
|
+
* @param {Options} [options] - function options
|
|
173
|
+
* @param {string} [options.dtype] - output array data type
|
|
174
|
+
* @throws {TypeError} first argument must be a nonnegative integer
|
|
175
|
+
* @throws {TypeError} options argument must be an object
|
|
176
|
+
* @throws {TypeError} must provide valid options
|
|
177
|
+
* @returns {(Array|TypedArray)} output array
|
|
178
|
+
*/
|
|
179
|
+
function invgamma2( len, alpha, beta, options ) {
|
|
180
|
+
var ctor;
|
|
181
|
+
var out;
|
|
182
|
+
var err;
|
|
183
|
+
var dt;
|
|
184
|
+
var o;
|
|
185
|
+
if ( !isNonNegativeInteger( len ) ) {
|
|
186
|
+
throw new TypeError( format( 'invalid argument. First argument must be a nonnegative integer. Value: `%s`.', len ) );
|
|
187
|
+
}
|
|
188
|
+
o = {};
|
|
189
|
+
if ( arguments.length > 3 ) {
|
|
190
|
+
err = validate( o, options );
|
|
191
|
+
if ( err ) {
|
|
192
|
+
throw err;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// NOTE: we could alternatively use the PRNG factory function to create a PRNG function which applies parameters for each invocation; however, this would impose a one-time cost which is likely to be rather expensive when generating small arrays. Decision made here to avoid this cost, despite a small cost due to repeatedly validating parameters for each generated pseudorandom number. Additionally, the current implementation has the added benefit that it mirrors the underlying PRNG where invalid parameters result in a return value of `NaN`.
|
|
196
|
+
dt = o.dtype || opts.dtype;
|
|
197
|
+
if ( dt === 'generic' ) {
|
|
198
|
+
return filledBy( len, wrapper );
|
|
199
|
+
}
|
|
200
|
+
ctor = ctors( dt );
|
|
201
|
+
out = new ctor( len );
|
|
202
|
+
binary( [ [ alpha ], [ beta ], out ], [ len ], [ 0, 0, 1 ], prng );
|
|
203
|
+
return out;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Applies parameters to a pseudorandom number generator function.
|
|
207
|
+
*
|
|
208
|
+
* @private
|
|
209
|
+
* @returns {number} pseudorandom number
|
|
210
|
+
*/
|
|
211
|
+
function wrapper() {
|
|
212
|
+
return prng( alpha, beta );
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Returns the PRNG seed.
|
|
218
|
+
*
|
|
219
|
+
* @private
|
|
220
|
+
* @returns {PRNGSeedMT19937} seed
|
|
221
|
+
*/
|
|
222
|
+
function getSeed() {
|
|
223
|
+
return rand.PRNG.seed;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Returns the PRNG seed length.
|
|
228
|
+
*
|
|
229
|
+
* @private
|
|
230
|
+
* @returns {PositiveInteger} seed length
|
|
231
|
+
*/
|
|
232
|
+
function getSeedLength() {
|
|
233
|
+
return rand.PRNG.seedLength;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Returns the PRNG state length.
|
|
238
|
+
*
|
|
239
|
+
* @private
|
|
240
|
+
* @returns {PositiveInteger} state length
|
|
241
|
+
*/
|
|
242
|
+
function getStateLength() {
|
|
243
|
+
return rand.PRNG.stateLength;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Returns the PRNG state size (in bytes).
|
|
248
|
+
*
|
|
249
|
+
* @private
|
|
250
|
+
* @returns {PositiveInteger} state size (in bytes)
|
|
251
|
+
*/
|
|
252
|
+
function getStateSize() {
|
|
253
|
+
return rand.PRNG.byteLength;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Returns the current pseudorandom number generator state.
|
|
258
|
+
*
|
|
259
|
+
* @private
|
|
260
|
+
* @returns {PRNGStateMT19937} current state
|
|
261
|
+
*/
|
|
262
|
+
function getState() {
|
|
263
|
+
return rand.PRNG.state;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Sets the pseudorandom number generator state.
|
|
268
|
+
*
|
|
269
|
+
* @private
|
|
270
|
+
* @param {PRNGStateMT19937} s - generator state
|
|
271
|
+
* @throws {Error} must provide a valid state
|
|
272
|
+
*/
|
|
273
|
+
function setState( s ) {
|
|
274
|
+
rand.PRNG.state = s;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
// EXPORTS //
|
|
280
|
+
|
|
281
|
+
module.exports = factory;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2023 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
|
+
/**
|
|
22
|
+
* Create an array containing pseudorandom numbers drawn from an inverse gamma distribution with parameters `alpha` (shape parameter) and `beta` (scale parameter).
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/random-array-invgamma
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var invgamma = require( '@stdlib/random-array-invgamma' );
|
|
28
|
+
*
|
|
29
|
+
* var arr = invgamma( 10, 2.0, 5.0 );
|
|
30
|
+
* // returns <Float64Array>
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* var invgamma = require( '@stdlib/random-array-invgamma' );
|
|
34
|
+
*
|
|
35
|
+
* var arr = invgamma( 10, 2.0, 5.0, {
|
|
36
|
+
* 'dtype': 'generic'
|
|
37
|
+
* });
|
|
38
|
+
* // returns [...]
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* var invgamma = require( '@stdlib/random-array-invgamma' );
|
|
42
|
+
*
|
|
43
|
+
* var rand = invgamma.factory( 2.0, 5.0 );
|
|
44
|
+
* // returns <Function>
|
|
45
|
+
*
|
|
46
|
+
* var arr = rand( 10 );
|
|
47
|
+
* // returns <Float64Array>
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* var invgamma = require( '@stdlib/random-array-invgamma' );
|
|
51
|
+
*
|
|
52
|
+
* var rand = invgamma.factory( 2.0, 5.0 );
|
|
53
|
+
* // returns <Function>
|
|
54
|
+
*
|
|
55
|
+
* var arr = rand( 10, {
|
|
56
|
+
* 'dtype': 'generic'
|
|
57
|
+
* });
|
|
58
|
+
* // returns [...]
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
// MODULES //
|
|
62
|
+
|
|
63
|
+
var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
|
|
64
|
+
var main = require( './main.js' );
|
|
65
|
+
var factory = require( './factory.js' );
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
// MAIN //
|
|
69
|
+
|
|
70
|
+
setReadOnly( main, 'factory', factory );
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
// EXPORTS //
|
|
74
|
+
|
|
75
|
+
module.exports = main;
|
|
76
|
+
|
|
77
|
+
// exports: { "factory": "main.factory" }
|