Skip to content

Commit 31a0fe5

Browse files
committed
feat: add assign method and refactor implementation
1 parent 460cd4b commit 31a0fe5

20 files changed

+614
-278
lines changed

lib/node_modules/@stdlib/random/array/normal/README.md

+25-15
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ var out = normal( 10, 2.0, 5.0, opts );
6161
// returns [...]
6262
```
6363

64+
#### normal.assign( mu, sigma, out )
65+
66+
Fills an array with pseudorandom numbers drawn from a [normal][@stdlib/random/base/normal] distribution.
67+
68+
```javascript
69+
var zeros = require( '@stdlib/array/zeros' );
70+
71+
var x = zeros( 10, 'float64' );
72+
// returns <Float64Array>
73+
74+
var out = normal.assign( 2.0, 5.0, x );
75+
// returns <Float64Array>
76+
77+
var bool = ( out === x );
78+
// returns true
79+
```
80+
81+
The function has the following parameters:
82+
83+
- **mu**: mean.
84+
- **sigma**: standard deviation.
85+
- **out**: output array.
86+
6487
#### normal.factory( \[mu, sigma, ]\[options] )
6588

6689
Returns a function for creating arrays containing pseudorandom numbers drawn from a [normal][@stdlib/random/base/normal] distribution.
@@ -75,7 +98,7 @@ var len = out.length;
7598
// returns 10
7699
```
77100

78-
If provided `mu` and `sigma`, the returned generator returns random variates from the specified distribution.
101+
If provided distribution parameters, the returned generator returns random variates from the specified distribution.
79102

80103
```javascript
81104
var random = normal.factory( 2.0, 5.0 );
@@ -87,7 +110,7 @@ out = random( 10 );
87110
// returns <Float64Array>
88111
```
89112

90-
If not provided `mu` and `sigma`, the returned generator requires that both parameters be provided at each invocation.
113+
If not provided distribution parameters, the returned generator requires that distribution parameters be provided at each invocation.
91114

92115
```javascript
93116
var random = normal.factory();
@@ -331,13 +354,6 @@ logEach( '%f', x4 );
331354

332355
<section class="related">
333356

334-
* * *
335-
336-
## See Also
337-
338-
- <span class="package-name">[`@stdlib/random/base/normal`][@stdlib/random/base/normal]</span><span class="delimiter">: </span><span class="description">normally distributed pseudorandom numbers.</span>
339-
- <span class="package-name">[`@stdlib/random/strided/normal`][@stdlib/random/strided/normal]</span><span class="delimiter">: </span><span class="description">fill a strided array with pseudorandom numbers drawn from a normal distribution.</span>
340-
341357
</section>
342358

343359
<!-- /.related -->
@@ -354,12 +370,6 @@ logEach( '%f', x4 );
354370

355371
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
356372

357-
<!-- <related-links> -->
358-
359-
[@stdlib/random/strided/normal]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/strided/normal
360-
361-
<!-- </related-links> -->
362-
363373
</section>
364374

365375
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 bench = require( '@stdlib/bench' );
24+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var zeros = require( '@stdlib/array/zeros' );
27+
var pkg = require( './../package.json' ).name;
28+
var random = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
return benchmark;
42+
43+
/**
44+
* Benchmark function.
45+
*
46+
* @private
47+
* @param {Benchmark} b - benchmark instance
48+
*/
49+
function benchmark( b ) {
50+
var out;
51+
var o;
52+
var i;
53+
54+
out = zeros( len, 'float32' );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
o = random.assign( 2.0, 5.0, out );
59+
if ( isnanf( o[ i%len ] ) ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( isnanf( o[ i%len ] ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
f = createBenchmark( len );
93+
bench( pkg+':assign:dtype=float32,len='+len, f );
94+
}
95+
}
96+
97+
main();

lib/node_modules/@stdlib/random/array/normal/benchmark/benchmark.float32.factory.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var bench = require( '@stdlib/bench' );
2424
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2525
var pow = require( '@stdlib/math/base/special/pow' );
2626
var pkg = require( './../package.json' ).name;
27-
var normal = require( './../lib' );
27+
var random = require( './../lib' );
2828

2929

3030
// FUNCTIONS //
@@ -37,7 +37,7 @@ var normal = require( './../lib' );
3737
* @returns {Function} benchmark function
3838
*/
3939
function createBenchmark( len ) {
40-
var fcn = normal.factory( 2.0, 5.0, {
40+
var fcn = random.factory( 2.0, 5.0, {
4141
'dtype': 'float32'
4242
});
4343
return benchmark;

lib/node_modules/@stdlib/random/array/normal/benchmark/benchmark.float32.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var bench = require( '@stdlib/bench' );
2424
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2525
var pow = require( '@stdlib/math/base/special/pow' );
2626
var pkg = require( './../package.json' ).name;
27-
var normal = require( './../lib' );
27+
var random = require( './../lib' );
2828

2929

3030
// FUNCTIONS //
@@ -56,7 +56,7 @@ function createBenchmark( len ) {
5656

5757
b.tic();
5858
for ( i = 0; i < b.iterations; i++ ) {
59-
o = normal( len, 2.0, 5.0, opts );
59+
o = random( len, 2.0, 5.0, opts );
6060
if ( isnanf( o[ i%len ] ) ) {
6161
b.fail( 'should not return NaN' );
6262
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var zeros = require( '@stdlib/array/zeros' );
27+
var pkg = require( './../package.json' ).name;
28+
var random = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
return benchmark;
42+
43+
/**
44+
* Benchmark function.
45+
*
46+
* @private
47+
* @param {Benchmark} b - benchmark instance
48+
*/
49+
function benchmark( b ) {
50+
var out;
51+
var o;
52+
var i;
53+
54+
out = zeros( len, 'float64' );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
o = random.assign( 2.0, 5.0, out );
59+
if ( isnan( o[ i%len ] ) ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( isnan( o[ i%len ] ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
f = createBenchmark( len );
93+
bench( pkg+':assign:dtype=float64,len='+len, f );
94+
}
95+
}
96+
97+
main();

lib/node_modules/@stdlib/random/array/normal/benchmark/benchmark.float64.factory.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var bench = require( '@stdlib/bench' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2525
var pow = require( '@stdlib/math/base/special/pow' );
2626
var pkg = require( './../package.json' ).name;
27-
var normal = require( './../lib' );
27+
var random = require( './../lib' );
2828

2929

3030
// FUNCTIONS //
@@ -37,7 +37,7 @@ var normal = require( './../lib' );
3737
* @returns {Function} benchmark function
3838
*/
3939
function createBenchmark( len ) {
40-
var fcn = normal.factory( 2.0, 5.0, {
40+
var fcn = random.factory( 2.0, 5.0, {
4141
'dtype': 'float64'
4242
});
4343
return benchmark;

lib/node_modules/@stdlib/random/array/normal/benchmark/benchmark.float64.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var bench = require( '@stdlib/bench' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2525
var pow = require( '@stdlib/math/base/special/pow' );
2626
var pkg = require( './../package.json' ).name;
27-
var normal = require( './../lib' );
27+
var random = require( './../lib' );
2828

2929

3030
// FUNCTIONS //
@@ -56,7 +56,7 @@ function createBenchmark( len ) {
5656

5757
b.tic();
5858
for ( i = 0; i < b.iterations; i++ ) {
59-
o = normal( len, 2.0, 5.0, opts );
59+
o = random( len, 2.0, 5.0, opts );
6060
if ( isnan( o[ i%len ] ) ) {
6161
b.fail( 'should not return NaN' );
6262
}

0 commit comments

Comments
 (0)