Skip to content

Commit b28e890

Browse files
gunjjoshikgryte
andauthored
feat: add fft/base/fftpack/decompose
PR-URL: #10354 Ref: #4121 Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Athan Reines <kgryte@gmail.com>
1 parent 364e294 commit b28e890

File tree

10 files changed

+993
-0
lines changed

10 files changed

+993
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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, M, initial, si, oi, out, so, oo )
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, 4, initial, 1, 0, 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+
- **M**: number of trial divisors.
63+
- **initial**: array of initial trial divisors.
64+
- **si**: stride length for `initial`.
65+
- **oi**: starting index for `initial`.
66+
- **out**: output array for storing factorization results.
67+
- **so**: stride length for `out`.
68+
- **oo**: starting index for `out`.
69+
70+
The function returns the number of factors into which `N` was decomposed.
71+
72+
</section>
73+
74+
<!-- /.usage -->
75+
76+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
77+
78+
<section class="notes">
79+
80+
## Notes
81+
82+
- Factorization results are stored in the output array as follows:
83+
84+
```text
85+
[ sequence_length | number_of_factors | integer_factors | unused_storage ]
86+
```
87+
88+
</section>
89+
90+
<!-- /.notes -->
91+
92+
<section class="examples">
93+
94+
## Examples
95+
96+
<!-- eslint no-undef: "error" -->
97+
98+
```javascript
99+
var decompose = require( '@stdlib/fft/base/fftpack/decompose' );
100+
101+
var initial = [ 3, 4, 2, 5 ]; // as found in FFTPACK
102+
var factors = [ 0, 0, 0, 0 ];
103+
104+
var nf = decompose( 12, 4, initial, 1, 0, factors, 1, 0 );
105+
106+
console.log( 'Sequence length: %d', 12 );
107+
console.log( 'Number of factors: %d', nf );
108+
109+
console.log( 'Factors:' );
110+
var j;
111+
for ( j = 0; j < nf; j++ ) {
112+
console.log( ' %d', factors[ j+2 ] );
113+
}
114+
```
115+
116+
</section>
117+
118+
<!-- /.examples -->
119+
120+
<!-- 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. -->
121+
122+
<section class="references">
123+
124+
</section>
125+
126+
<!-- /.references -->
127+
128+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
129+
130+
<section class="related">
131+
132+
</section>
133+
134+
<!-- /.related -->
135+
136+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
137+
138+
<section class="links">
139+
140+
</section>
141+
142+
<!-- /.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, 4, initial, 1, 0, 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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
{{alias}}( N, M, initial, si, oi, out, so, oo )
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: integer
15+
Length of the sequence.
16+
17+
M: integer
18+
Number of trial divisors.
19+
20+
initial: ArrayLikeObject<number>
21+
Array of initial trial divisors.
22+
23+
si: integer
24+
Stride length for `initial`.
25+
26+
oi: integer
27+
Starting index for `initial`.
28+
29+
out: ArrayLikeObject<number>
30+
Output array for storing factorization results.
31+
32+
so: integer
33+
Stride length for `out`.
34+
35+
oo: integer
36+
Starting index for `out`.
37+
38+
Returns
39+
-------
40+
numFactors: integer
41+
Number of factors into which N was decomposed.
42+
43+
Examples
44+
--------
45+
> var N = 630;
46+
> var initial = [ 3, 4, 2, 5 ];
47+
> var factors = [ 0, 0, 0, 0, 0, 0, 0 ];
48+
> var numFactors = {{alias}}( N, 4, initial, 1, 0, factors, 1, 0 )
49+
5
50+
> factors
51+
[ 630, 5, 2, 3, 3, 5, 7 ]
52+
53+
See Also
54+
--------
55+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
import { Collection } from '@stdlib/types/array';
24+
25+
/**
26+
* Factorizes a sequence length into a product of integers.
27+
*
28+
* @param N - length of the sequence
29+
* @param M - number of trial divisors
30+
* @param initial - array of initial trial divisors
31+
* @param si - stride length for `initial`
32+
* @param oi - starting index for `initial`
33+
* @param out - output array for storing factorization results
34+
* @param so - stride length for `out`
35+
* @param oo - starting index for `out`
36+
* @returns number of factors into which `N` was decomposed
37+
*
38+
* @example
39+
* var initial = new Float64Array( [ 3, 4, 2, 5 ] );
40+
* var factors = new Float64Array( 4 );
41+
*
42+
* var numFactors = decompose( 12, 4, initial, 1, 0, factors, 1, 0 );
43+
* // returns 2
44+
*/
45+
declare function decompose( N: number, M: number, initial: Collection<number>, si: number, oi: number, out: Collection<number>, so: number, oo: number ): number;
46+
47+
48+
// EXPORTS //
49+
50+
export = decompose;

0 commit comments

Comments
 (0)