Skip to content

Commit 62ed403

Browse files
committed
refactor!: migrate to random/tools/unary-factory and drop support for default options
BREAKING CHANGE: drop support for default options Previously, the `factory` method supported providing defaults for various ndarray options (e.g., readonly, mode, order, etc). These options have been removed. Instead, users should pass them along to the main API for generating pseudorandom numbers. To replicate previous functionality, create your own wrapper which uses partial application to pass a set of "default" options for each invocation of the PRNG function. --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 9990c87 commit 62ed403

27 files changed

+491
-1846
lines changed

lib/node_modules/@stdlib/random/exponential/README.md

Lines changed: 38 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,11 @@ var arr = exponential( [ 3, 3 ], 2.0 );
4141

4242
The function has the following parameters:
4343

44-
- **shape**: output array shape.
45-
- **lambda**: rate parameter.
44+
- **shape**: output shape.
45+
- **lambda**: rate parameter. May be either a scalar or an [ndarray][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the specified output shape.
4646
- **options**: function options.
4747

48-
If provided an empty `shape`, the function returns a zero-dimensional [ndarray][@stdlib/ndarray/ctor].
49-
50-
```javascript
51-
var arr = exponential( [], 2.0 );
52-
// returns <ndarray>
53-
54-
var shape = arr.shape;
55-
// returns []
56-
57-
var v = arr.get();
58-
// returns <number>
59-
```
60-
61-
Distribution parameters may be either scalars or [ndarrays][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the specified output array `shape`.
48+
When provided a scalar distribution parameter, every element in the output [ndarray][@stdlib/ndarray/ctor] is drawn from the same distribution. To generate pseudorandom numbers drawn from different distributions, provide a distribution parameter argument as an [ndarray][@stdlib/ndarray/ctor]. The following example demonstrates broadcasting an [ndarray][@stdlib/ndarray/ctor] containing distribution parameters to generate sub-matrices drawn from different distributions.
6249

6350
```javascript
6451
var array = require( '@stdlib/ndarray/array' );
@@ -73,15 +60,28 @@ var arr = exponential( [ 2, 3, 3 ], lambda );
7360
// returns <ndarray>
7461
```
7562

76-
The function accepts the following `options`:
63+
If provided an empty shape, the function returns a zero-dimensional [ndarray][@stdlib/ndarray/ctor].
7764

78-
- **dtype**: output array data type. Must be a [real-valued floating-point data type][@stdlib/array/typed-real-float-dtypes] or "generic". Default: `'float64'`.
79-
- **order**: array order (i.e., memory layout), which is either `row-major` (C-style) or `column-major` (Fortran-style). Default: `'row-major'`.
80-
- **mode**: specifies how to handle indices which exceed array dimensions. For a list of supported modes, see [`ndarray`][@stdlib/ndarray/ctor]. Default: `'throw'`.
81-
- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions. If provided fewer modes than dimensions, an [ndarray][@stdlib/ndarray/ctor] instance recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
82-
- **readonly**: `boolean` indicating whether an array should be **read-only**. Default: `false`.
65+
```javascript
66+
var arr = exponential( [], 2.0 );
67+
// returns <ndarray>
8368

84-
By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a `float64` data type. To return an [ndarray][@stdlib/ndarray/ctor] having a different data type, set the `dtype` option.
69+
var shape = arr.shape;
70+
// returns []
71+
72+
var v = arr.get();
73+
// returns <number>
74+
```
75+
76+
The function accepts the following options:
77+
78+
- **dtype**: output ndarray data type. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes].
79+
- **order**: ndarray order (i.e., memory layout), which is either `row-major` (C-style) or `column-major` (Fortran-style). Default: `'row-major'`.
80+
- **mode**: specifies how to handle indices which exceed ndarray dimensions. For a list of supported modes, see [`ndarray`][@stdlib/ndarray/ctor]. Default: `'throw'`.
81+
- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed ndarray dimensions. If provided fewer modes than dimensions, an [ndarray][@stdlib/ndarray/ctor] instance recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
82+
- **readonly**: boolean indicating whether an ndarray should be **read-only**. Default: `false`.
83+
84+
By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option.
8585

8686
```javascript
8787
var opts = {
@@ -95,41 +95,27 @@ var dt = arr.dtype;
9595
// returns 'generic'
9696
```
9797

98-
#### exponential.assign( out, lambda )
98+
#### exponential.assign( lambda, out )
9999

100100
Fills an [ndarray][@stdlib/ndarray/ctor] with pseudorandom numbers drawn from an [exponential][@stdlib/random/base/exponential] distribution.
101101

102102
```javascript
103103
var zeros = require( '@stdlib/ndarray/zeros' );
104104

105-
var arr = zeros( [ 3, 3 ] );
105+
var out = zeros( [ 3, 3 ] );
106106
// returns <ndarray>
107107

108-
var out = exponential.assign( arr, 2.0 );
108+
var v = exponential.assign( 2.0, out );
109109
// returns <ndarray>
110110

111-
var bool = ( out === arr );
111+
var bool = ( v === out );
112112
// returns true
113113
```
114114

115-
Distribution parameters may be either scalars or [ndarrays][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output [ndarray][@stdlib/ndarray/ctor].
116-
117-
```javascript
118-
var array = require( '@stdlib/ndarray/array' );
119-
var zeros = require( '@stdlib/ndarray/zeros' );
120-
121-
var lambda = array( [ [ [ 2.0 ] ], [ [ 5.0 ] ] ] );
122-
// returns <ndarray>
123-
124-
var shape = lambda.shape;
125-
// returns [ 2, 1, 1 ]
115+
The method has the following parameters:
126116

127-
var arr = zeros( [ 2, 3, 3 ] );
128-
// returns <ndarray>
129-
130-
var out = exponential.assign( arr, lambda );
131-
// returns <ndarray>
132-
```
117+
- **lambda**: rate parameter. May be either a scalar or an [ndarray][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output [ndarray][@stdlib/ndarray/ctor].
118+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
133119

134120
#### exponential.factory( \[options] )
135121

@@ -145,17 +131,12 @@ var sh = out.shape;
145131
// returns [ 3, 3 ]
146132
```
147133

148-
The function accepts the following `options`:
134+
The method accepts the following options:
149135

150136
- **prng**: pseudorandom number generator for generating uniformly distributed pseudorandom numbers on the interval `[0,1)`. If provided, the function **ignores** both the `state` and `seed` options. In order to seed the underlying pseudorandom number generator, one must seed the provided `prng` (assuming the provided `prng` is seedable).
151137
- **seed**: pseudorandom number generator seed.
152138
- **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option.
153-
- **copy**: `boolean` indicating whether to copy a provided pseudorandom number generator state. Setting this option to `false` allows sharing state between two or more pseudorandom number generators. Setting this option to `true` ensures that an underlying generator has exclusive control over its internal state. Default: `true`.
154-
- **dtype**: default output array data type. Must be a [real-valued floating-point data type][@stdlib/array/typed-real-float-dtypes] or "generic". Default: `'float64'`.
155-
- **order**: default array order (i.e., memory layout), which is either `row-major` (C-style) or `column-major` (Fortran-style). Default: `'row-major'`.
156-
- **mode**: default specifying how to handle indices which exceed array dimensions. For a list of supported modes, see [`ndarray`][@stdlib/ndarray/ctor]. Default: `'throw'`.
157-
- **submode**: default specifying for each dimension how to handle subscripts which exceed array dimensions. If the number of modes is less than the number of dimensions, an [ndarray][@stdlib/ndarray/ctor] instance recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
158-
- **readonly**: default indicating whether an array should be **read-only**. Default: `false`.
139+
- **copy**: boolean indicating whether to copy a provided pseudorandom number generator state. Setting this option to `false` allows sharing state between two or more pseudorandom number generators. Setting this option to `true` ensures that an underlying generator has exclusive control over its internal state. Default: `true`.
159140

160141
To use a custom PRNG as the underlying source of uniformly distributed pseudorandom numbers, set the `prng` option.
161142

@@ -183,34 +164,7 @@ var out = random( [ 3, 3 ], 2.0, opts );
183164
// returns <ndarray>
184165
```
185166

186-
The returned function accepts the following `options`, each of which overrides the respective default:
187-
188-
- **dtype**: output array data type. Must be a [real-valued floating-point data type][@stdlib/array/typed-real-float-dtypes] or "generic".
189-
- **order**: array order (i.e., memory layout), which is either `row-major` (C-style) or `column-major` (Fortran-style).
190-
- **mode**: specifies how to handle indices which exceed array dimensions. For a list of supported modes, see [`ndarray`][@stdlib/ndarray/ctor].
191-
- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions. If the number of modes is less than the number of dimensions, an [ndarray][@stdlib/ndarray/ctor] instance recycles modes using modulo arithmetic.
192-
- **readonly**: `boolean` indicating whether an array should be **read-only**.
193-
194-
To override the default output array data type, set the `dtype` option.
195-
196-
```javascript
197-
var random = exponential.factory();
198-
199-
var out = random( [ 3, 3 ], 2.0 );
200-
// returns <ndarray>
201-
202-
var dt = out.dtype;
203-
// returns 'float64'
204-
205-
var opts = {
206-
'dtype': 'generic'
207-
};
208-
out = random( [ 3, 3 ], 2.0, opts );
209-
// returns <ndarray>
210-
211-
dt = out.dtype;
212-
// returns 'generic'
213-
```
167+
The returned function has the same interface and accepts the same options as the `exponential` function above.
214168

215169
#### exponential.PRNG
216170

@@ -341,6 +295,7 @@ var sz = random.byteLength;
341295

342296
- If PRNG state is "shared" (meaning a state array was provided during function creation and **not** copied) and one sets the underlying generator state to a state array having a different length, the function returned by the `factory` method does **not** update the existing shared state and, instead, points to the newly provided state array. In order to synchronize the output of the underlying generator according to the new shared state array, the state array for **each** relevant creation function and/or PRNG must be **explicitly** set.
343297
- If PRNG state is "shared" and one sets the underlying generator state to a state array of the same length, the PRNG state is updated (along with the state of all other creation functions and/or PRNGs sharing the PRNG's state array).
298+
- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes].
344299

345300
</section>
346301

@@ -406,10 +361,12 @@ logEach( '%f, %f, %f', arr[ 0 ], arr[ 1 ], arr[ 2 ] );
406361

407362
[@stdlib/random/base/exponential]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/exponential
408363

409-
[@stdlib/array/typed-real-float-dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/typed-real-float-dtypes
410-
411364
[@stdlib/array/uint32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint32
412365

366+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
367+
368+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
369+
413370
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
414371

415372
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes

lib/node_modules/@stdlib/random/exponential/benchmark/benchmark.1d_columnmajor.js renamed to lib/node_modules/@stdlib/random/exponential/benchmark/benchmark.1d.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2525
var pow = require( '@stdlib/math/base/special/pow' );
26+
var dtypes = require( '@stdlib/ndarray/dtypes' );
27+
var orders = require( '@stdlib/ndarray/orders' );
2628
var pkg = require( './../package.json' ).name;
27-
var dtypes = require( './../lib/dtypes.js' );
2829
var random = require( './../lib' );
2930

3031

3132
// VARIABLES //
3233

33-
var order = 'column-major';
34+
var DTYPES = dtypes( 'real_floating_point_and_generic' );
35+
var ORDERS = orders();
36+
var PARAM1 = 2.0;
3437

3538

3639
// FUNCTIONS //
@@ -42,9 +45,10 @@ var order = 'column-major';
4245
* @param {PositiveInteger} len - ndarray length
4346
* @param {NonNegativeIntegerArray} shape - ndarray shape
4447
* @param {string} dtype - output ndarray data type
48+
* @param {string} order - output ndarray memory layout
4549
* @returns {Function} benchmark function
4650
*/
47-
function createBenchmark( len, shape, dtype ) {
51+
function createBenchmark( len, shape, dtype, order ) {
4852
var opts = {
4953
'dtype': dtype,
5054
'order': order
@@ -63,7 +67,7 @@ function createBenchmark( len, shape, dtype ) {
6367

6468
b.tic();
6569
for ( i = 0; i < b.iterations; i++ ) {
66-
x = random( shape, 2.0, opts );
70+
x = random( shape, PARAM1, opts );
6771
if ( isnan( x.data[ i%len ] ) ) {
6872
b.fail( 'should not return NaN' );
6973
}
@@ -89,23 +93,28 @@ function main() {
8993
var len;
9094
var min;
9195
var max;
96+
var ord;
9297
var sh;
9398
var t1;
9499
var f;
95100
var i;
96101
var j;
102+
var k;
97103

98104
min = 1; // 10^min
99105
max = 6; // 10^max
100106

101-
for ( j = 0; j < dtypes.length; j++ ) {
102-
t1 = dtypes[ j ];
103-
for ( i = min; i <= max; i++ ) {
104-
len = pow( 10, i );
107+
for ( k = 0; k < ORDERS.length; k++ ) {
108+
ord = ORDERS[ k ];
109+
for ( j = 0; j < DTYPES.length; j++ ) {
110+
t1 = DTYPES[ j ];
111+
for ( i = min; i <= max; i++ ) {
112+
len = pow( 10, i );
105113

106-
sh = [ len ];
107-
f = createBenchmark( len, sh, t1 );
108-
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
114+
sh = [ len ];
115+
f = createBenchmark( len, sh, t1, ord );
116+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1, f );
117+
}
109118
}
110119
}
111120
}

lib/node_modules/@stdlib/random/exponential/benchmark/benchmark.1d_rowmajor.js

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)