Skip to content

Commit 7a98dfa

Browse files
committed
feat: add random/array/tools/binary-factory
1 parent 6b16ced commit 7a98dfa

File tree

10 files changed

+1451
-0
lines changed

10 files changed

+1451
-0
lines changed

lib/node_modules/@stdlib/random/array/tools/binary-factory/README.md

Lines changed: 544 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 arcsine = require( '@stdlib/random/base/arcsine' );
27+
var dtypes = require( '@stdlib/array/dtypes' );
28+
var zeros = require( '@stdlib/array/zeros' );
29+
var pkg = require( './../package.json' ).name;
30+
var binaryFactory = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var factory;
44+
var random;
45+
var dt;
46+
47+
dt = dtypes( 'real_floating_point' );
48+
factory = binaryFactory( arcsine, dt, dt[ 0 ] );
49+
random = factory();
50+
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var out;
61+
var o;
62+
var i;
63+
64+
out = zeros( len, 'float64' );
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
o = random.assign( 2.0, 5.0, out );
69+
if ( isnan( o[ i%len ] ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnan( o[ i%len ] ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10^min
98+
max = 6; // 10^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 10, i );
102+
f = createBenchmark( len );
103+
bench( pkg+'::generate:assign:len='+len, f );
104+
}
105+
}
106+
107+
main();
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 arcsine = require( '@stdlib/random/base/arcsine' );
25+
var normal = require( '@stdlib/random/base/normal' );
26+
var isFunction = require( '@stdlib/assert/is-function' );
27+
var pkg = require( './../package.json' ).name;
28+
var binaryFactory = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var values;
35+
var dtypes;
36+
var f;
37+
var i;
38+
39+
values = [
40+
arcsine,
41+
normal
42+
];
43+
dtypes = [
44+
'float64',
45+
'float32'
46+
];
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
f = binaryFactory( values[ i%values.length ], dtypes, dtypes[ 0 ] );
51+
if ( typeof f !== 'function' ) {
52+
b.fail( 'should return a function' );
53+
}
54+
}
55+
b.toc();
56+
if ( !isFunction( f ) ) {
57+
b.fail( 'should return a function' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});
62+
63+
bench( pkg+'::factory', function benchmark( b ) {
64+
var dtypes;
65+
var rand;
66+
var f;
67+
var i;
68+
69+
dtypes = [
70+
'float64',
71+
'float32',
72+
'generic'
73+
];
74+
f = binaryFactory( arcsine, dtypes, dtypes[ 0 ] );
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
rand = f();
79+
if ( typeof rand !== 'function' ) {
80+
b.fail( 'should return a function' );
81+
}
82+
}
83+
b.toc();
84+
if ( !isFunction( rand ) ) {
85+
b.fail( 'should return a function' );
86+
}
87+
b.pass( 'benchmark finished' );
88+
b.end();
89+
});
90+
91+
bench( pkg+'::factory,partial_application', function benchmark( b ) {
92+
var values;
93+
var dtypes;
94+
var rand;
95+
var f;
96+
var i;
97+
98+
dtypes = [
99+
'float64',
100+
'float32',
101+
'generic'
102+
];
103+
f = binaryFactory( arcsine, dtypes, dtypes[ 0 ] );
104+
105+
values = [
106+
1.0,
107+
2.0,
108+
3.0
109+
];
110+
111+
b.tic();
112+
for ( i = 0; i < b.iterations; i++ ) {
113+
rand = f( values[ i%values.length ], 5.0 );
114+
if ( typeof rand !== 'function' ) {
115+
b.fail( 'should return a function' );
116+
}
117+
}
118+
b.toc();
119+
if ( !isFunction( rand ) ) {
120+
b.fail( 'should return a function' );
121+
}
122+
b.pass( 'benchmark finished' );
123+
b.end();
124+
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 arcsine = require( '@stdlib/random/base/arcsine' );
27+
var dtypes = require( '@stdlib/array/dtypes' );
28+
var pkg = require( './../package.json' ).name;
29+
var binaryFactory = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var factory;
43+
var random;
44+
var dt;
45+
46+
dt = dtypes( 'real_floating_point' );
47+
factory = binaryFactory( arcsine, dt, dt[ 0 ] );
48+
random = factory();
49+
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var o;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
o = random( len, 2.0, 5.0 );
65+
if ( isnan( o[ i%len ] ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( o[ i%len ] ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( pkg+'::generate:len='+len, f );
100+
}
101+
}
102+
103+
main();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
{{alias}}( prng, dtypes, dtype )
3+
Returns a factory function for generating pseudorandom values drawn from a
4+
binary PRNG.
5+
6+
Parameters
7+
----------
8+
prng: Function
9+
Binary pseudorandom value generator.
10+
11+
dtypes: Array<string>
12+
List of supported output array data types.
13+
14+
dtype: string
15+
Default output array data type.
16+
17+
Returns
18+
-------
19+
fcn: Function
20+
Factory function which returns a function for creating arrays. The
21+
returned factory function accepts three optional arguments:
22+
23+
- param1: first PRNG parameter.
24+
- param2: second PRNG parameter.
25+
- options: function options.
26+
27+
If provided PRNG parameters, the factory function returns a partially
28+
applied function for creating arrays.
29+
30+
The function supports the following options:
31+
32+
- prng: pseudorandom number generator which generates uniformly
33+
distributed pseudorandom numbers.
34+
- seed: pseudorandom value generator seed.
35+
- state: pseudorandom value generator state.
36+
- copy: boolean indicating whether to copy a provided pseudorandom value
37+
generator state.
38+
- dtype: default output array data type. Setting this option overrides
39+
the default array data type specified when invoking the parent
40+
function.
41+
42+
Examples
43+
--------
44+
> var dt = [ 'float64', 'float32', 'generic' ];
45+
> var f = {{alias}}( {{alias:@stdlib/random/base/arcsine}}, dt, dt[0] );
46+
> var fcn = f();
47+
> var x = fcn( 5, 2.0, 5.0 )
48+
<Float64Array>
49+
> x = fcn( 5, 2.0, 5.0, { 'dtype': 'float32' } )
50+
<Float32Array>
51+
52+
See Also
53+
--------
54+

0 commit comments

Comments
 (0)