All files / fft/base/fftpack/lib decompose.js

96.03% Statements 194/202
71.42% Branches 5/7
100% Functions 1/1
96.03% Lines 194/202

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 2031x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x             1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* ## Notice
*
* The original C code and copyright notice are from the [PFFFT library]{@link https://github.com/marton78/pffft/blob/master/fftpack.c}. The implementation follows the original, but has been modified for JavaScript.
*
* ```text
* Copyright (c) 2004 the University Corporation for Atmospheric
* Research ("UCAR"). All rights reserved. Developed by NCAR's
* Computational and Information Systems Laboratory, UCAR,
* www.cisl.ucar.edu.
*
* Redistribution and use of the Software in source and binary forms,
* with or without modification, is permitted provided that the
* following conditions are met:
*
*     - Neither the names of NCAR's Computational and Information Systems
*       Laboratory, the University Corporation for Atmospheric Research,
*       nor the names of its sponsors or contributors may be used to
*       endorse or promote products derived from this Software without
*       specific prior written permission.
*
*     - Redistributions of source code must retain the above copyright
*       notices, this list of conditions, and the disclaimer below.
*
*     - Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions, and the disclaimer below in the
*       documentation and/or other materials provided with the
*       distribution.
*
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
* SOFTWARE.
* ```
*/
 
'use strict';
 
// MODULES //
 
var floor = require( '@stdlib/math/base/special/floor' );
 
 
// MAIN //
 
/**
* Factorizes a sequence length into a product of integers.
*
* ## Notes
*
* -   Factorization results are stored in the input array as follows:
*
*     ```text
*     [ sequence_length | number_of_factors | integer_factors | unused_storage ]
*     ```
*
* -   The function mutates the input array.
*
* @private
* @param {NonNegativeInteger} N - length of the sequence
* @param {NonNegativeIntegerArray} initial - array of initial trial divisors
* @param {Collection} out - output array for storing factorization results
* @param {integer} stride - stride length for `out`
* @param {NonNegativeInteger} offset - starting index for `out`
* @returns {NonNegativeInteger} number of factors into which `N` was decomposed
*
* @example
* // Specify an initial list of potential divisors:
* var initial = [ 3, 4, 2, 5 ]; // as found in FFTPACK
*
* // Define a sequence length:
* var N = 630;
*
* // Initialize an array for storing factorization results:
* var factors = [ 0, 0, 0, 0, 0, 0, 0 ];
*
* // Factorize the sequence length into a product of integers:
* var numFactors = decompose( N, initial, factors, 1, 0 );
* // returns 5
*
* var f = factors.slice();
* // returns [ 630, 5, 2, 3, 3, 5, 7 ]
*
* @example
* // Specify an initial list of potential divisors:
* var initial = [ 3, 4, 2, 5 ]; // as found in FFTPACK
*
* // Define a sequence length:
* var N = 8;
*
* // Initialize an array for storing factorization results:
* var factors = [ 0, 0, 0, 0 ];
*
* // Factorize the sequence length into its a product of integers:
* var numFactors = decompose( N, initial, factors, 1, 0 );
* // returns 2
*
* var f = factors.slice();
* // returns [ 8, 2, 2, 4 ]
*/
function decompose( N, initial, out, stride, offset ) { // TODO: consider moving to a separate utility package
	var divisor;
	var ntrials;
	var nl;
	var nf;
	var nq;
	var nr;
	var ib;
	var i;
	var j;
 
	// Resolve the number of trial divisors:
	ntrials = initial.length;
 
	// Initialize a variable for storing a trial divisor:
	divisor = 0;
 
	// Initialize a variable for storing a sub-sequence length:
	nl = N;
 
	// Initialize a variable for keeping track of the number of factors into which `N` decomposes:
	nf = 0;
 
	j = 0;
	do {
		if ( j < ntrials ) {
			divisor = initial[ j ];
		} else {
			divisor += 2;
		}
		j += 1;
		while ( true ) {
			// Compute the integer quotient:
			nq = floor( nl / divisor );
 
			// Compute the remainder:
			nr = nl - ( divisor * nq );
 
			// If the divisor did not evenly divide the current sub-sequence length, try a new divisor...
			if ( nr !== 0 ) {
				break;
			}
			// We found a new factor:
			nf += 1;
 
			// Update the sub-sequence length:
			nl = nq;
 
			// Store the factor in the output array:
			out[ offset+((nf+1)*stride) ] = divisor;
 
			// When the divisor is `2` and we've already found other factors, shift the other factors right to make room for the most recent `2` factor...
			if ( divisor === 2 && nf !== 1 ) {
				for ( i = 2; i <= nf; i++ ) {
					ib = nf - i + 2;
					out[ offset+((ib+1)*stride) ] = out[ offset+(ib*stride) ];
				}
				out[ offset+(2*stride) ] = 2;
			}
			// If we cannot further divide the sequence length into smaller sub-sequences, we're done...
			if ( nl === 1 ) {
				break;
			}
		}
	} while ( nl !== 1 );
 
	// Store the sequence length:
	out[ offset ] = N;
 
	// Store the number of factors:
	out[ offset+stride ] = nf;
 
	// Return the number of factors:
	return nf;
}
 
 
// EXPORTS //
 
module.exports = decompose;