Skip to content

Commit a467beb

Browse files
committed
feat: add fft/base/fftpack/decompose
--- 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 9d9b268 commit a467beb

10 files changed

Lines changed: 1091 additions & 0 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# decompose
22+
23+
> Factorize a sequence length into a product of integers.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var decompose = require( '@stdlib/fft/base/fftpack/decompose' );
41+
```
42+
43+
#### decompose( N, initial, out, stride, offset )
44+
45+
Factorizes a sequence length into a product of integers.
46+
47+
```javascript
48+
var initial = [ 3, 4, 2, 5 ]; // as found in FFTPACK
49+
var N = 630;
50+
var factors = [ 0, 0, 0, 0, 0, 0, 0 ];
51+
52+
var numFactors = decompose( N, initial, factors, 1, 0 );
53+
// returns 5
54+
55+
console.log( factors );
56+
// => [ 630, 5, 2, 3, 3, 5, 7 ]
57+
```
58+
59+
The function accepts the following arguments:
60+
61+
- **N**: length of the sequence.
62+
- **initial**: array of initial trial divisors.
63+
- **out**: output array for storing factorization results.
64+
- **stride**: stride length for `out`.
65+
- **offset**: starting index for `out`.
66+
67+
The function returns the number of factors into which `N` was decomposed.
68+
69+
</section>
70+
71+
<!-- /.usage -->
72+
73+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
74+
75+
<section class="notes">
76+
77+
## Notes
78+
79+
- Factorization results are stored in the output array as follows:
80+
81+
```text
82+
[ sequence_length | number_of_factors | integer_factors | unused_storage ]
83+
```
84+
85+
- The function mutates the input array.
86+
87+
</section>
88+
89+
<!-- /.notes -->
90+
91+
<section class="examples">
92+
93+
## Examples
94+
95+
<!-- eslint no-undef: "error" -->
96+
97+
```javascript
98+
var decompose = require( '@stdlib/fft/base/fftpack/decompose' );
99+
100+
var initial = [ 3, 4, 2, 5 ]; // as found in FFTPACK
101+
var factors = [ 0, 0, 0, 0 ];
102+
var nf;
103+
var j;
104+
105+
nf = decompose( 12, initial, factors, 1, 0 );
106+
107+
console.log( 'Sequence length: %d', 12 );
108+
console.log( 'Number of factors: %d', nf );
109+
console.log( 'Factors:' );
110+
for ( j = 0; j < nf; j++ ) {
111+
console.log( ' %d', factors[ j+2 ] );
112+
}
113+
```
114+
115+
</section>
116+
117+
<!-- /.examples -->
118+
119+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="references">
122+
123+
</section>
124+
125+
<!-- /.references -->
126+
127+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
128+
129+
<section class="related">
130+
131+
</section>
132+
133+
<!-- /.related -->
134+
135+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="links">
138+
139+
</section>
140+
141+
<!-- /.links -->
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) 2026 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 format = require( '@stdlib/string/format' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var floor = require( '@stdlib/math/base/special/floor' );
27+
var log2 = require( '@stdlib/math/base/special/log2' );
28+
var zeros = require( '@stdlib/array/zeros' );
29+
var pkg = require( './../package.json' ).name;
30+
var decompose = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var initial = [ 3, 4, 2, 5 ]; // as found in FFTPACK
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Creates a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} N - sequence length
45+
* @returns {Function} benchmark function
46+
*/
47+
function createBenchmark( N ) {
48+
var factors = zeros( 2 + floor( log2( N ) ) );
49+
return benchmark;
50+
51+
/**
52+
* Benchmark function.
53+
*
54+
* @private
55+
* @param {Benchmark} b - benchmark instance
56+
*/
57+
function benchmark( b ) {
58+
var d;
59+
var i;
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
d = decompose( N, initial, factors, 1, 0 );
64+
if ( isnan( d ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
}
68+
b.toc();
69+
if ( isnan( d ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var lengths;
87+
var N;
88+
var f;
89+
var i;
90+
91+
lengths = [
92+
12,
93+
24,
94+
36,
95+
48,
96+
60,
97+
120
98+
];
99+
100+
for ( i = 0; i < lengths.length; i++ ) {
101+
N = lengths[ i ];
102+
f = createBenchmark( N );
103+
bench( format( '%s:N=%d', pkg, N ), f );
104+
}
105+
}
106+
107+
main();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
{{alias}}( N, initial, out, stride, offset )
3+
Factorizes a sequence length into a product of integers.
4+
5+
Factorization results are stored in the output array sequentially, where
6+
the first element contains the sequence length N, the second element
7+
contains the number of factors into which N was decomposed and subsequent
8+
elements contain the individual integer factors.
9+
10+
Any remaining array space remains as unused storage.
11+
12+
Parameters
13+
----------
14+
N: number
15+
Length of the sequence.
16+
17+
initial: Array<number>
18+
Array of initial trial divisors.
19+
20+
out: Collection<number>
21+
Output array for storing factorization results.
22+
23+
stride: number
24+
Stride length for `out`.
25+
26+
offset: number
27+
Starting index for `out`.
28+
29+
Returns
30+
-------
31+
numFactors: number
32+
Number of factors into which N was decomposed.
33+
34+
Examples
35+
--------
36+
> var N = 630;
37+
> var initial = [ 3, 4, 2, 5 ];
38+
> var factors = [ 0, 0, 0, 0, 0, 0, 0 ];
39+
> var numFactors = {{alias}}( N, initial, factors, 1, 0 )
40+
5
41+
> factors.slice()
42+
[ 630, 5, 2, 3, 3, 5, 7 ]
43+
44+
See Also
45+
--------
46+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
/**
24+
* Factorizes a sequence length into a product of integers.
25+
*
26+
* @param N - length of the sequence
27+
* @param initial - array of initial trial divisors
28+
* @param out - output array for storing factorization results
29+
* @param stride - stride length for `out`
30+
* @param offset - starting index for `out`
31+
* @returns number of factors into which `N` was decomposed
32+
*
33+
* @example
34+
* var N = 630;
35+
* var initial = [ 3, 4, 2, 5 ]; // as found in FFTPACK
36+
* var factors = [ 0, 0, 0, 0, 0, 0, 0 ];
37+
*
38+
* var numFactors = decompose( N, initial, factors, 1, 0 );
39+
* // returns 5
40+
*
41+
* var f = factors.slice();
42+
* // returns [ 630, 5, 2, 3, 3, 5, 7 ]
43+
*/
44+
declare function decompose( N: number, initial: Array<number>, out: Array<number>, stride: number, offset: number ): number;
45+
46+
47+
// EXPORTS //
48+
49+
export = decompose;

0 commit comments

Comments
 (0)