Skip to content

Commit a30b64a

Browse files
committed
feat: add random/array/tools/unary-factory
1 parent bfbf7d0 commit a30b64a

File tree

9 files changed

+778
-0
lines changed

9 files changed

+778
-0
lines changed
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) 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 exponential = require( '@stdlib/random/base/exponential' );
27+
var dtypes = require( '@stdlib/array/dtypes' );
28+
var zeros = require( '@stdlib/array/zeros' );
29+
var pkg = require( './../package.json' ).name;
30+
var unaryFactory = 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 = unaryFactory( exponential, dt );
49+
random = factory( dt[ 0 ] );
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, 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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 exponential = require( '@stdlib/random/base/exponential' );
25+
var geometric = require( '@stdlib/random/base/geometric' );
26+
var isFunction = require( '@stdlib/assert/is-function' );
27+
var pkg = require( './../package.json' ).name;
28+
var unaryFactory = 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+
exponential,
41+
geometric
42+
];
43+
dtypes = [
44+
'float64',
45+
'float32'
46+
];
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
f = unaryFactory( values[ i%values.length ], dtypes );
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 = unaryFactory( exponential, dtypes );
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
rand = f( dtypes[ i%dtypes.length ] );
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+
});
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) 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 exponential = require( '@stdlib/random/base/exponential' );
27+
var dtypes = require( '@stdlib/array/dtypes' );
28+
var pkg = require( './../package.json' ).name;
29+
var unaryFactory = 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 = unaryFactory( exponential, dt );
48+
random = factory( dt[ 0 ] );
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 );
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( prng, dtypes )
3+
Returns a function for generating pseudorandom values drawn from a unary
4+
PRNG.
5+
6+
Parameters
7+
----------
8+
prng: Function
9+
Unary pseudorandom value generator.
10+
11+
dtypes: Array<string>
12+
List of supported output array data types.
13+
14+
Returns
15+
-------
16+
fcn: Function
17+
Function which returns a factory function for creating arrays. The
18+
returned factory function is a unary function accepting a single
19+
argument for specifying the default output array data type.
20+
21+
Examples
22+
--------
23+
> var dt = [ 'float64', 'float32', 'generic' ];
24+
> var fcn = {{alias}}( {{alias:@stdlib/random/base/exponential}}, dt );
25+
> var f = fcn( dt[ 0 ] );
26+
> var x = f( 5, 2.0 )
27+
<Float64Array>
28+
> x = f( 5, 2.0, { 'dtype': 'float32' } )
29+
<Float32Array>
30+
31+
See Also
32+
--------
33+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
var exponential = require( '@stdlib/random/base/exponential' );
22+
var dtypes = require( '@stdlib/array/dtypes' );
23+
var unaryFactory = require( './../lib' );
24+
25+
var dt = dtypes( 'real_floating_point' );
26+
dt.push( 'generic' );
27+
28+
var factory = unaryFactory( exponential, dt );
29+
// returns <Function>
30+
31+
var random = factory( 'float64' );
32+
// returns <Function>
33+
34+
var x = random( 10, 2.0 );
35+
console.log( x );
36+
// => <Float64Array>
37+
38+
x = random( 10, 2.0, {
39+
'dtype': 'float32'
40+
});
41+
console.log( x );
42+
// => <Float32Array>
43+
44+
x = random( 10, 2.0, {
45+
'dtype': 'generic'
46+
});
47+
console.log( x );
48+
// => [...]

0 commit comments

Comments
 (0)