Skip to content

Commit c1d2b7a

Browse files
committed
feat: add random/tools/binary-factory
--- 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: passed - 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 81f4209 commit c1d2b7a

File tree

13 files changed

+2249
-0
lines changed

13 files changed

+2249
-0
lines changed

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

Lines changed: 625 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 dtypes = require( '@stdlib/ndarray/dtypes' );
27+
var uniform = require( '@stdlib/random/base/uniform' );
28+
var zeros = require( '@stdlib/ndarray/zeros' );
29+
var pkg = require( './../package.json' ).name;
30+
var createFactory = 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 policies;
44+
var factory;
45+
var random;
46+
var out;
47+
var dt;
48+
49+
dt = dtypes( 'real_floating_point' );
50+
policies = {
51+
'output': 'real_floating_point',
52+
'casting': 'none'
53+
};
54+
factory = createFactory( uniform, [ dt, dt ], dt, policies );
55+
random = factory();
56+
57+
out = zeros( [ len ] );
58+
59+
return benchmark;
60+
61+
/**
62+
* Benchmark function.
63+
*
64+
* @private
65+
* @param {Benchmark} b - benchmark instance
66+
*/
67+
function benchmark( b ) {
68+
var o;
69+
var i;
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
o = random.assign( 0.0, 1.0, out );
74+
if ( typeof o !== 'object' ) {
75+
b.fail( 'should return an ndarray' );
76+
}
77+
}
78+
b.toc();
79+
if ( isnan( o.get( i%len ) ) ) {
80+
b.fail( 'should not return NaN' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
*/
95+
function main() {
96+
var len;
97+
var min;
98+
var max;
99+
var f;
100+
var i;
101+
102+
min = 1; // 10^min
103+
max = 6; // 10^max
104+
105+
for ( i = min; i <= max; i++ ) {
106+
len = pow( 10, i );
107+
f = createBenchmark( len );
108+
bench( pkg+':assign:len='+len, f );
109+
}
110+
}
111+
112+
main();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 uniform = require( '@stdlib/random/base/uniform' );
25+
var pkg = require( './../package.json' ).name;
26+
var createFactory = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+'::factory', function benchmark( b ) {
32+
var policies;
33+
var factory;
34+
var dtypes;
35+
var v;
36+
var i;
37+
38+
dtypes = [
39+
'float64',
40+
'float32'
41+
];
42+
policies = {
43+
'output': 'real_floating_point'
44+
};
45+
factory = createFactory( uniform, [ dtypes, dtypes ], dtypes, policies );
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
v = factory();
50+
if ( typeof v !== 'function' ) {
51+
b.fail( 'should return a function' );
52+
}
53+
}
54+
b.toc();
55+
if ( typeof v !== 'function' ) {
56+
b.fail( 'should return a function' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 uniform = require( '@stdlib/random/base/uniform' );
25+
var pkg = require( './../package.json' ).name;
26+
var createFactory = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+'::create_factory', function benchmark( b ) {
32+
var policies;
33+
var dtypes;
34+
var v;
35+
var i;
36+
37+
dtypes = [
38+
'float64',
39+
'float32'
40+
];
41+
policies = {
42+
'output': 'real_floating_point'
43+
};
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
v = createFactory( uniform, [ dtypes, dtypes ], dtypes, policies );
48+
if ( typeof v !== 'function' ) {
49+
b.fail( 'should return a function' );
50+
}
51+
}
52+
b.toc();
53+
if ( typeof v !== 'function' ) {
54+
b.fail( 'should return a function' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 dtypes = require( '@stdlib/ndarray/dtypes' );
27+
var uniform = require( '@stdlib/random/base/uniform' );
28+
var pkg = require( './../package.json' ).name;
29+
var createFactory = 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 policies;
43+
var factory;
44+
var random;
45+
var dt;
46+
47+
dt = dtypes( 'real_floating_point' );
48+
policies = {
49+
'output': 'real_floating_point',
50+
'casting': 'none'
51+
};
52+
factory = createFactory( uniform, [ dt, dt ], dt, policies );
53+
random = factory();
54+
55+
return benchmark;
56+
57+
/**
58+
* Benchmark function.
59+
*
60+
* @private
61+
* @param {Benchmark} b - benchmark instance
62+
*/
63+
function benchmark( b ) {
64+
var o;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
o = random( [ len ], 0.0, 1.0 );
70+
if ( typeof o !== 'object' ) {
71+
b.fail( 'should return an ndarray' );
72+
}
73+
}
74+
b.toc();
75+
if ( isnan( o.get( i%len ) ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
/**
87+
* Main execution sequence.
88+
*
89+
* @private
90+
*/
91+
function main() {
92+
var len;
93+
var min;
94+
var max;
95+
var f;
96+
var i;
97+
98+
min = 1; // 10^min
99+
max = 6; // 10^max
100+
101+
for ( i = min; i <= max; i++ ) {
102+
len = pow( 10, i );
103+
f = createBenchmark( len );
104+
bench( pkg+'::generate:len='+len, f );
105+
}
106+
}
107+
108+
main();

0 commit comments

Comments
 (0)