Skip to content

Commit 5ed23e7

Browse files
committed
docs: add docs and benchmark
--- 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: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - 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 073714c commit 5ed23e7

File tree

4 files changed

+322
-0
lines changed

4 files changed

+322
-0
lines changed
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) 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 zeros = require( '@stdlib/array/zeros' );
26+
var pkg = require( './../package.json' ).name;
27+
var decompose = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} N - sequence length to factorize
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( N ) {
40+
var initial = [ 3, 4, 2, 5 ];
41+
return benchmark;
42+
43+
/**
44+
* Benchmark function.
45+
*
46+
* @private
47+
* @param {Benchmark} b - benchmark instance
48+
*/
49+
function benchmark( b ) {
50+
var factors;
51+
var out;
52+
var i;
53+
54+
factors = zeros( 6 );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
out = decompose( N, initial, factors, 1, 0 );
59+
if ( isnan( out ) ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
65+
if ( isnan( out ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
}
71+
}
72+
73+
74+
// MAIN //
75+
76+
/**
77+
* Main execution sequence.
78+
*
79+
* @private
80+
*/
81+
function main() {
82+
var lengths;
83+
var N;
84+
var f;
85+
var i;
86+
87+
lengths = [
88+
12,
89+
24,
90+
36,
91+
48,
92+
60,
93+
120
94+
];
95+
96+
for ( i = 0; i < lengths.length; i++ ) {
97+
N = lengths[i];
98+
f = createBenchmark( N );
99+
bench( pkg+':N='+N, f );
100+
}
101+
}
102+
103+
main();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( N, initial, out, stride, offset )
3+
Factorizes a sequence length into a product of integers.
4+
5+
Parameters
6+
----------
7+
N: number
8+
Length of the sequence.
9+
10+
initial: Array<number>
11+
Array of initial trial divisors.
12+
13+
out: Collection<number>
14+
Output array for storing factorization results.
15+
16+
stride: number
17+
Stride length for `out`.
18+
19+
offset: number
20+
Starting index for `out`.
21+
22+
Returns
23+
-------
24+
numFactors: number
25+
Number of factors into which N was decomposed.
26+
27+
Examples
28+
--------
29+
> var initial = [ 3, 4, 2, 5 ];
30+
> var N = 630;
31+
> var factors = [ 0, 0, 0, 0, 0, 0, 0 ];
32+
> var numFactors = {{alias}}( N, initial, factors, 1, 0 )
33+
5
34+
> factors.slice()
35+
[ 630, 5, 2, 3, 3, 5, 7 ]
36+
37+
See Also
38+
--------
39+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/array';
24+
25+
/**
26+
* Factorizes a sequence length into a product of integers.
27+
*
28+
* ## Notes
29+
*
30+
* - If provided a sequence length which is not a nonnegative integer, the function returns `NaN`.
31+
* - If provided an array of initial trial divisors which is not an array of nonnegative integers, the function returns `NaN`.
32+
* - If provided an output array which is not an array-like object, the function returns `NaN`.
33+
* - If provided a stride which is not a nonnegative integer, the function returns `NaN`.
34+
* - If provided an offset which is not a nonnegative integer, the function returns `NaN`.
35+
*
36+
* @param N - length of the sequence
37+
* @param initial - array of initial trial divisors
38+
* @param out - output array for storing factorization results
39+
* @param stride - stride length for `out`
40+
* @param offset - starting index for `out`
41+
* @returns number of factors into which `N` was decomposed
42+
*
43+
* @example
44+
* // Specify an initial list of potential divisors:
45+
* var initial = [ 3, 4, 2, 5 ]; // as found in FFTPACK
46+
*
47+
* // Define a sequence length:
48+
* var N = 630;
49+
*
50+
* // Initialize an array for storing factorization results:
51+
* var factors = [ 0, 0, 0, 0, 0, 0, 0 ];
52+
*
53+
* // Factorize the sequence length into a product of integers:
54+
* var numFactors = decompose( N, initial, factors, 1, 0 );
55+
* // returns 5
56+
*
57+
* var f = factors.slice();
58+
* // returns [ 630, 5, 2, 3, 3, 5, 7 ]
59+
*/
60+
declare function decompose( N: number, initial: Array<number>, out: Collection<number>, stride: number, offset: number ): number;
61+
62+
63+
// EXPORTS //
64+
65+
export = decompose;
66+
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
import decompose = require( './index' );
20+
21+
// TESTS //
22+
23+
// The function returns a number...
24+
{
25+
const initial = [ 4, 2, 3, 5 ];
26+
const out = [ 0, 0, 0, 0, 0, 0, 0 ];
27+
28+
decompose( 12, initial, out, 1, 0 ); // $ExpectType number
29+
}
30+
31+
// The compiler throws an error if the function is provided a sequence length which is not a number...
32+
{
33+
const initial = [ 4, 2, 3, 5 ];
34+
const out = [ 0, 0, 0, 0, 0, 0, 0 ];
35+
36+
decompose( '12', initial, out, 1, 0 ); // $ExpectError
37+
decompose( true, initial, out, 1, 0 ); // $ExpectError
38+
decompose( false, initial, out, 1, 0 ); // $ExpectError
39+
decompose( null, initial, out, 1, 0 ); // $ExpectError
40+
decompose( undefined, initial, out, 1, 0 ); // $ExpectError
41+
decompose( [], initial, out, 1, 0 ); // $ExpectError
42+
decompose( {}, initial, out, 1, 0 ); // $ExpectError
43+
decompose( ( x: number ): number => x, initial, out, 1, 0 ); // $ExpectError
44+
}
45+
46+
// The compiler throws an error if the function is provided initial trial divisors which is not an array of numbers...
47+
{
48+
const out = [ 0, 0, 0, 0, 0, 0, 0 ];
49+
50+
decompose( 12, '4,2,3,5', out, 1, 0 ); // $ExpectError
51+
decompose( 12, 5, out, 1, 0 ); // $ExpectError
52+
decompose( 12, true, out, 1, 0 ); // $ExpectError
53+
decompose( 12, false, out, 1, 0 ); // $ExpectError
54+
decompose( 12, null, out, 1, 0 ); // $ExpectError
55+
decompose( 12, undefined, out, 1, 0 ); // $ExpectError
56+
decompose( 12, {}, out, 1, 0 ); // $ExpectError
57+
decompose( 12, ( x: number ): number => x, out, 1, 0 ); // $ExpectError
58+
}
59+
60+
// The compiler throws an error if the function is provided an output array which is not an array-like object...
61+
{
62+
const initial = [ 4, 2, 3, 5 ];
63+
64+
decompose( 12, initial, 123, 1, 0 ); // $ExpectError
65+
decompose( 12, initial, true, 1, 0 ); // $ExpectError
66+
decompose( 12, initial, false, 1, 0 ); // $ExpectError
67+
decompose( 12, initial, null, 1, 0 ); // $ExpectError
68+
decompose( 12, initial, undefined, 1, 0 ); // $ExpectError
69+
decompose( 12, initial, {}, 1, 0 ); // $ExpectError
70+
decompose( 12, initial, ( x: number ): number => x, 1, 0 ); // $ExpectError
71+
}
72+
73+
// The compiler throws an error if the function is provided a stride which is not a number...
74+
{
75+
const initial = [ 4, 2, 3, 5 ];
76+
const out = [ 0, 0, 0, 0, 0, 0, 0 ];
77+
78+
decompose( 12, initial, out, '1', 0 ); // $ExpectError
79+
decompose( 12, initial, out, true, 0 ); // $ExpectError
80+
decompose( 12, initial, out, false, 0 ); // $ExpectError
81+
decompose( 12, initial, out, null, 0 ); // $ExpectError
82+
decompose( 12, initial, out, undefined, 0 ); // $ExpectError
83+
decompose( 12, initial, out, [], 0 ); // $ExpectError
84+
decompose( 12, initial, out, {}, 0 ); // $ExpectError
85+
decompose( 12, initial, out, ( x: number ): number => x, 0 ); // $ExpectError
86+
}
87+
88+
// The compiler throws an error if the function is provided an offset which is not a number...
89+
{
90+
const initial = [ 4, 2, 3, 5 ];
91+
const out = [ 0, 0, 0, 0, 0, 0, 0 ];
92+
93+
decompose( 12, initial, out, 1, '0' ); // $ExpectError
94+
decompose( 12, initial, out, 1, true ); // $ExpectError
95+
decompose( 12, initial, out, 1, false ); // $ExpectError
96+
decompose( 12, initial, out, 1, null ); // $ExpectError
97+
decompose( 12, initial, out, 1, undefined ); // $ExpectError
98+
decompose( 12, initial, out, 1, [] ); // $ExpectError
99+
decompose( 12, initial, out, 1, {} ); // $ExpectError
100+
decompose( 12, initial, out, 1, ( x: number ): number => x ); // $ExpectError
101+
}
102+
103+
// The compiler throws an error if the function is provided an unsupported number of arguments...
104+
{
105+
const initial = [ 4, 2, 3, 5 ];
106+
const out = [ 0, 0, 0, 0, 0, 0, 0 ];
107+
108+
decompose(); // $ExpectError
109+
decompose( 12 ); // $ExpectError
110+
decompose( 12, initial ); // $ExpectError
111+
decompose( 12, initial, out ); // $ExpectError
112+
decompose( 12, initial, out, 1 ); // $ExpectError
113+
decompose( 12, initial, out, 1, 0, 123 ); // $ExpectError
114+
}

0 commit comments

Comments
 (0)