diff --git a/lib/node_modules/@stdlib/fft/rfftf/README.md b/lib/node_modules/@stdlib/fft/rfftf/README.md new file mode 100644 index 000000000000..692078f12848 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/README.md @@ -0,0 +1,186 @@ + + +# rfftf + +> Return a new [ndarray][@stdlib/ndarray/ctor] containing the one-dimensional discrete Fourier transform of a real-valued input [ndarray][@stdlib/ndarray/ctor]. + +
+ +## Usage + +```javascript +var rfftf = require( '@stdlib/fft/rfftf' ); +``` + +#### rfftf( x\[, options] ) + +Returns a new [ndarray][@stdlib/ndarray/ctor] containing the one-dimensional discrete Fourier transform of a real-valued input [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); + +var out = rfftf( x ); +// returns +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **n**: number of points over which to compute the transform. If not provided, defaults to the length of the input [ndarray][@stdlib/ndarray/ctor]. If `n` is greater than the input length, the input is zero-padded. If `n` is less than the input length, the input is truncated. +- **normalize**: normalization mode. Must be one of the following: `'backward'` (default), `'ortho'`, or `'forward'`. + +The returned [ndarray][@stdlib/ndarray/ctor] has a complex-valued floating-point [data type][@stdlib/ndarray/dtypes] determined by the input [data type][@stdlib/ndarray/dtypes]. For example, `float64` maps to `complex128`. The returned [ndarray][@stdlib/ndarray/ctor] has a length of `floor(N/2) + 1`, where `N` is the length of the sequence to transform. + +By default, the number of transform points equals the length of the input [ndarray][@stdlib/ndarray/ctor]. To specify a different number of transform points, provide an `n` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); + +var out = rfftf( x, { + 'n': 8 +}); +// returns +``` + +By default, no normalization is applied to the transform result. To specify a normalization mode, provide a `normalize` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); + +// Scale by 1/sqrt(N): +var out = rfftf( x, { + 'normalize': 'ortho' +}); +// returns +``` + +The normalization modes are as follows: + +- **backward**: no normalization (default). +- **ortho**: scale by `1/sqrt(N)`, making the transform orthonormal. +- **forward**: scale by `1/N`. + +#### rfftf.assign( x, out\[, options] ) + +Computes the one-dimensional discrete Fourier transform of a real-valued input [ndarray][@stdlib/ndarray/ctor] and assigns the results to a provided output [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var zeros = require( '@stdlib/ndarray/zeros' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); +var out = zeros( [ 3 ], { + 'dtype': 'complex128' +}); + +var v = rfftf.assign( x, out ); +// returns + +var bool = ( v === out ); +// returns true +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. Must have a complex-valued floating-point [data type][@stdlib/ndarray/dtypes]. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **n**: number of points over which to compute the transform. If not provided, defaults to the length of the input [ndarray][@stdlib/ndarray/ctor]. If `n` is greater than the input length, the input is zero-padded. If `n` is less than the input length, the input is truncated. +- **normalize**: normalization mode. Must be one of the following: `'backward'` (default), `'ortho'`, or `'forward'`. + +
+ + + +
+ +## Notes + +- The function does **not** mutate the input [ndarray][@stdlib/ndarray/ctor]. +- The output [ndarray][@stdlib/ndarray/ctor] contains only the non-negative frequency terms of the full DFT, exploiting the Hermitian symmetry of real-valued input. For an input of length `N`, the output has length `floor(N/2) + 1`. +- The output [ndarray][@stdlib/ndarray/ctor] has a complex-valued floating-point [data type][@stdlib/ndarray/dtypes] determined by the input [data type][@stdlib/ndarray/dtypes]. For example, `float64` input maps to `complex128` output. +- The DC component (index `0`) and, for even-length transforms, the Nyquist component (last index) are always purely real (i.e., have zero imaginary parts). +- This transform is unnormalized by default. A call to this function followed by a call performing a backward transform will multiply the input by `N`. To obtain an orthonormal transform, set the `normalize` option to `'ortho'`. + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var uniform = require( '@stdlib/random/uniform' ); +var rfftf = require( '@stdlib/fft/rfftf' ); + +// Generate an ndarray of random numbers: +var x = uniform( [ 8 ], -10.0, 10.0, { + 'dtype': 'float64' +}); +console.log( ndarray2array( x ) ); + +// Perform operation: +var out = rfftf( x ); + +// Print the results: +console.log( ndarray2array( out ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/fft/rfftf/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/fft/rfftf/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..8506cfe11814 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/benchmark/benchmark.assign.js @@ -0,0 +1,118 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var uniform = require( '@stdlib/random/uniform' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var assign = require( './../lib/assign.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var M; + var x; + + M = floor( len/2 ) + 1; + x = uniform( [ len ], -50.0, 50.0, options ); + out = zeros( [ M ], { + 'dtype': 'complex128' + }); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var view; + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = assign( x, out ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + view = reinterpret128( getData( o ), 0 ); + if ( isnan( view[ 0 ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:assign:dtype=%s,len=%d', pkg, options.dtype, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/fft/rfftf/benchmark/benchmark.js b/lib/node_modules/@stdlib/fft/rfftf/benchmark/benchmark.js new file mode 100644 index 000000000000..45a168ebc790 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/benchmark/benchmark.js @@ -0,0 +1,107 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/uniform' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var rfftf = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( [ len ], -50.0, 50.0, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var view; + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = rfftf( x ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + view = reinterpret128( getData( o ), 0 ); + if ( isnan( view[ 0 ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:dtype=%s,len=%d', pkg, options.dtype, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/fft/rfftf/docs/repl.txt b/lib/node_modules/@stdlib/fft/rfftf/docs/repl.txt new file mode 100644 index 000000000000..cd954fc65176 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/docs/repl.txt @@ -0,0 +1,90 @@ + +{{alias}}( x[, options] ) + Returns a new ndarray containing the one-dimensional discrete Fourier + transform of a real-valued input ndarray. + + The returned ndarray has a complex-valued floating-point data type + determined by the input data type. For example, `float64` maps to + `complex128`. + + The returned ndarray has a length of `floor(N/2) + 1`, where `N` is the + length of the sequence to transform. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + options: Object (optional) + Function options. + + options.n: integer (optional) + Number of points over which to compute the transform. If not provided, + the number of points is equal to the length of the input array. If the + value is greater than the length of the input array, the input is + zero-padded. If the value is less than the length of the input array, + the input is truncated. + + options.normalize: string (optional) + Normalization mode. Must be one of the following: 'backward' (default), + 'ortho', or 'forward'. When set to 'backward', no normalization is + applied. When set to 'ortho', the result is scaled by `1/sqrt(n)`. + When set to 'forward', the result is scaled by `1/n`. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var out = {{alias}}( x ) + + + +{{alias}}.assign( x, out[, options] ) + Computes the one-dimensional discrete Fourier transform of a real-valued + input ndarray and assigns the results to a provided output ndarray. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + out: ndarray + Output array. Must have a complex-valued floating-point data type. + + options: Object (optional) + Function options. + + options.n: integer (optional) + Number of points over which to compute the transform. If not provided, + the number of points is equal to the length of the input array. If the + value is greater than the length of the input array, the input is + zero-padded. If the value is less than the length of the input array, + the input is truncated. + + options.normalize: string (optional) + Normalization mode. Must be one of the following: 'backward' (default), + 'ortho', or 'forward'. When set to 'backward', no normalization is + applied. When set to 'ortho', the result is scaled by `1/sqrt(n)`. + When set to 'forward', the result is scaled by `1/n`. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var opts = { 'dtype': 'complex128' }; + > var out = {{alias:@stdlib/ndarray/zeros}}( [ 3 ], opts ); + > var v = {{alias}}.assign( x, out ) + + > var bool = ( v === out ) + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/fft/rfftf/docs/types/index.d.ts b/lib/node_modules/@stdlib/fft/rfftf/docs/types/index.d.ts new file mode 100644 index 000000000000..8ad97a723a13 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/docs/types/index.d.ts @@ -0,0 +1,187 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { realndarray, complexndarray, genericndarray } from '@stdlib/types/ndarray'; + +/** +* Input array. +*/ +type InputArray = realndarray | genericndarray; + +/** +* Interface defining options. +*/ +interface Options { + /** + * Number of points over which to compute the transform. + */ + n?: number; + + /** + * Normalization mode. + */ + normalize?: 'backward' | 'ortho' | 'forward'; +} + +/** +* Interface for computing the forward real FFT. +*/ +interface Rfftf { + /** + * Returns a new ndarray containing the one-dimensional discrete Fourier transform of a real-valued input ndarray. + * + * @param x - input ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * + * // Create a data buffer: + * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + * + * // Define the shape of the input array: + * var sh = [ 4 ]; + * + * // Define the array strides: + * var sx = [ 1 ]; + * + * // Define the index offset: + * var ox = 0; + * + * // Create an input ndarray: + * var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); + * + * // Perform operation: + * var out = rfftf( x ); + * // returns [ [ 10.0, 0.0 ], [ -2.0, 2.0 ], [ -2.0, 0.0 ] ] + */ + ( x: InputArray, options?: Options ): complexndarray; + + /** + * Computes the one-dimensional discrete Fourier transform of a real-valued input ndarray and assigns the results to a provided output ndarray. + * + * @param x - input ndarray + * @param out - output ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * var zeros = require( '@stdlib/ndarray/zeros' ); + * + * // Create a data buffer: + * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + * + * // Define the shape of the input array: + * var sh = [ 4 ]; + * + * // Define the array strides: + * var sx = [ 1 ]; + * + * // Define the index offset: + * var ox = 0; + * + * // Create an input ndarray: + * var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); + * + * // Create an output ndarray: + * var out = zeros( [ 3 ], { 'dtype': 'complex128' } ); + * + * // Perform operation: + * var v = rfftf.assign( x, out ); + * // returns [ [ 10.0, 0.0 ], [ -2.0, 2.0 ], [ -2.0, 0.0 ] ] + * + * var bool = ( v === out ); + * // returns true + */ + assign( x: InputArray, out: T, options?: Options ): T; +} + +/** +* Returns a new ndarray containing the one-dimensional discrete Fourier transform of a real-valued input ndarray. +* +* @param x - input ndarray +* @param options - function options +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 4 ]; +* +* // Define the array strides: +* var sx = [ 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform operation: +* var out = rfftf( x ); +* // returns [ [ 10.0, 0.0 ], [ -2.0, 2.0 ], [ -2.0, 0.0 ] ] +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 4 ]; +* +* // Define the array strides: +* var sx = [ 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Create an output ndarray: +* var out = zeros( [ 3 ], { 'dtype': 'complex128' } ); +* +* // Perform operation: +* var v = rfftf.assign( x, out ); +* // returns [ [ 10.0, 0.0 ], [ -2.0, 2.0 ], [ -2.0, 0.0 ] ] +* +* var bool = ( v === out ); +* // returns true +*/ +declare const rfftf: Rfftf; + + +// EXPORTS // + +export = rfftf; diff --git a/lib/node_modules/@stdlib/fft/rfftf/docs/types/test.ts b/lib/node_modules/@stdlib/fft/rfftf/docs/types/test.ts new file mode 100644 index 000000000000..7f8ffdfb3da9 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/docs/types/test.ts @@ -0,0 +1,182 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +/* eslint-disable space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import rfftf = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + + rfftf( x ); // $ExpectType complexndarray + rfftf( x, {} ); // $ExpectType complexndarray + rfftf( x, { 'n': 8 } ); // $ExpectType complexndarray + rfftf( x, { 'normalize': 'ortho' } ); // $ExpectType complexndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + rfftf( '5' ); // $ExpectError + rfftf( 5 ); // $ExpectError + rfftf( true ); // $ExpectError + rfftf( false ); // $ExpectError + rfftf( null ); // $ExpectError + rfftf( void 0 ); // $ExpectError + rfftf( {} ); // $ExpectError + rfftf( ( x: number ): number => x ); // $ExpectError + + rfftf( '5', {} ); // $ExpectError + rfftf( 5, {} ); // $ExpectError + rfftf( true, {} ); // $ExpectError + rfftf( false, {} ); // $ExpectError + rfftf( null, {} ); // $ExpectError + rfftf( void 0, {} ); // $ExpectError + rfftf( {}, {} ); // $ExpectError + rfftf( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + const x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + + rfftf( x, '5' ); // $ExpectError + rfftf( x, 5 ); // $ExpectError + rfftf( x, true ); // $ExpectError + rfftf( x, false ); // $ExpectError + rfftf( x, null ); // $ExpectError + rfftf( x, [] ); // $ExpectError + rfftf( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + + rfftf(); // $ExpectError + rfftf( x, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + const out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + rfftf.assign( x, out ); // $ExpectType complex128ndarray + rfftf.assign( x, out, {} ); // $ExpectType complex128ndarray + rfftf.assign( x, out, { 'normalize': 'ortho' } ); // $ExpectType complex128ndarray + rfftf.assign( x, out, { 'n': 8 } ); // $ExpectType complex128ndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + const out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + rfftf.assign( '5', out ); // $ExpectError + rfftf.assign( 5, out ); // $ExpectError + rfftf.assign( true, out ); // $ExpectError + rfftf.assign( false, out ); // $ExpectError + rfftf.assign( null, out ); // $ExpectError + rfftf.assign( void 0, out ); // $ExpectError + rfftf.assign( {}, out ); // $ExpectError + rfftf.assign( ( x: number ): number => x, out ); // $ExpectError + + rfftf.assign( '5', out, {} ); // $ExpectError + rfftf.assign( 5, out, {} ); // $ExpectError + rfftf.assign( true, out, {} ); // $ExpectError + rfftf.assign( false, out, {} ); // $ExpectError + rfftf.assign( null, out, {} ); // $ExpectError + rfftf.assign( void 0, out, {} ); // $ExpectError + rfftf.assign( {}, out, {} ); // $ExpectError + rfftf.assign( ( x: number ): number => x, out, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an output argument which is not an ndarray... +{ + const x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + + rfftf.assign( x, '5' ); // $ExpectError + rfftf.assign( x, 5 ); // $ExpectError + rfftf.assign( x, true ); // $ExpectError + rfftf.assign( x, false ); // $ExpectError + rfftf.assign( x, null ); // $ExpectError + rfftf.assign( x, void 0 ); // $ExpectError + rfftf.assign( x, {} ); // $ExpectError + rfftf.assign( x, ( x: number ): number => x ); // $ExpectError + + rfftf.assign( x, '5', {} ); // $ExpectError + rfftf.assign( x, 5, {} ); // $ExpectError + rfftf.assign( x, true, {} ); // $ExpectError + rfftf.assign( x, false, {} ); // $ExpectError + rfftf.assign( x, null, {} ); // $ExpectError + rfftf.assign( x, void 0, {} ); // $ExpectError + rfftf.assign( x, {}, {} ); // $ExpectError + rfftf.assign( x, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an options argument which is not an object... +{ + const x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + const out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + rfftf.assign( x, out, '5' ); // $ExpectError + rfftf.assign( x, out, 5 ); // $ExpectError + rfftf.assign( x, out, true ); // $ExpectError + rfftf.assign( x, out, false ); // $ExpectError + rfftf.assign( x, out, null ); // $ExpectError + rfftf.assign( x, out, [] ); // $ExpectError + rfftf.assign( x, out, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + const out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + rfftf.assign(); // $ExpectError + rfftf.assign( x ); // $ExpectError + rfftf.assign( x, out, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/fft/rfftf/examples/index.js b/lib/node_modules/@stdlib/fft/rfftf/examples/index.js new file mode 100644 index 000000000000..db3a363d7861 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var uniform = require( '@stdlib/random/uniform' ); +var rfftf = require( './../lib' ); + +// Generate an ndarray of random numbers: +var x = uniform( [ 8 ], -10.0, 10.0, { + 'dtype': 'float64' +}); +console.log( ndarray2array( x ) ); + +// Perform operation: +var out = rfftf( x ); + +// Print the results: +console.log( ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/fft/rfftf/lib/assign.js b/lib/node_modules/@stdlib/fft/rfftf/lib/assign.js new file mode 100644 index 000000000000..705bf5e0ddc6 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/lib/assign.js @@ -0,0 +1,178 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +// MODULES // + +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var sliceTo = require( '@stdlib/ndarray/base/slice-to' ); +var gcopy = require( '@stdlib/blas/base/gcopy' ).ndarray; +var baseZeros = require( '@stdlib/ndarray/base/zeros' ); +var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' ); +var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' ); +var gscal = require( '@stdlib/blas/base/gscal' ).ndarray; +var contains = require( '@stdlib/array/base/assert/contains' ); +var join = require( '@stdlib/array/base/join' ); +var format = require( '@stdlib/string/format' ); +var base = require( './base.js' ); + + +// VARIABLES // + +var NORMS = [ 'backward', 'ortho', 'forward' ]; + + +// MAIN // + +/** +* Computes the one-dimensional discrete Fourier transform of a real-valued input ndarray and assigns the result to a provided output ndarray. +* +* @param {ndarrayLike} x - input ndarray +* @param {ndarrayLike} out - output ndarray +* @param {Options} [options] - function options +* @param {NonNegativeInteger} [options.n] - number of points over which to compute the transform +* @param {string} [options.normalize='backward'] - normalization mode +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} second argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {Error} must provide valid options +* @returns {ndarrayLike} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 4 ]; +* +* // Define the array strides: +* var sx = [ 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Create an output ndarray: +* var out = zeros( [ 3 ], { 'dtype': 'complex128' } ); +* +* // Perform operation: +* var v = rfftf( x, out ); +* // returns [ [ 10.0, 0.0 ], [ -2.0, 2.0 ], [ -2.0, 0.0 ] ] +* +* var bool = ( v === out ); +* // returns true +*/ +function rfftf( x, out ) { + var options; + var norm; + var view; + var xdt; + var odt; + var sh; + var M; + var N; + var n; + var y; + var s; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + if ( !isndarrayLike( out ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', out ) ); + } + norm = 'backward'; + xdt = getDType( x ); + odt = getDType( out ); + sh = getShape( x ); + N = sh[ 0 ]; + n = N; + if ( arguments.length > 2 ) { + options = arguments[ 2 ]; + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'n' ) ) { + n = options.n; + if ( !isNonNegativeInteger( n ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', 'n', n ) ); + } + } + if ( hasOwnProp( options, 'normalize' ) ) { + norm = options.normalize; + if ( !contains( NORMS, norm ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Option: `%s`.', 'normalize', join( NORMS, '", "' ), norm ) ); + } + } + } + M = floor( n/2 ) + 1; + + // When n differs from N, create a new input of length n: + if ( n !== N ) { + if ( n < N ) { + // Create a view of the first n elements: + x = sliceTo( x, [ n ], false, false ); + } else { + // Create a new array of length n and copy the first N elements: + y = baseZeros( xdt, [ n ], getOrder( x ) ); + gcopy( N, getData( x ), getStride( x, 0 ), getOffset( x ), getData( y ), 1, 0 ); // eslint-disable-line max-len + x = y; + } + } + // Compute the forward FFT: + base( out, x ); + + // Apply normalization: + if ( norm !== 'backward' ) { + if ( odt === 'complex128' ) { + view = reinterpret128( getData( out ), 0 ); + } else { + view = reinterpret64( getData( out ), 0 ); + } + if ( norm === 'ortho' ) { + s = 1.0 / sqrt( n ); + } else { + s = 1.0 / n; + } + gscal( 2*M, s, view, 1, 0 ); + } + return out; +} + + +// EXPORTS // + +module.exports = rfftf; diff --git a/lib/node_modules/@stdlib/fft/rfftf/lib/base.js b/lib/node_modules/@stdlib/fft/rfftf/lib/base.js new file mode 100644 index 000000000000..014fa2fcaa4b --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/lib/base.js @@ -0,0 +1,84 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +// MODULES // + +var dtypes = require( '@stdlib/ndarray/dtypes' ); +var drfftf = require( '@stdlib/fft/base/fftpack/ndarray/float64/rfftf' ); +var srfftf = require( '@stdlib/fft/base/fftpack/ndarray/float32/rfftf' ); +var grfftf = require( '@stdlib/fft/base/fftpack/ndarray/generic/rfftf' ); +var factory = require( '@stdlib/ndarray/base/nullary-strided1d-dispatch-factory' ); + + +// VARIABLES // + +var idtypes0 = dtypes( 'real_floating_point_and_generic' ); +var odtypes = dtypes( 'real_floating_point_and_generic' ); +var table = { + 'types': [ + 'float64', + 'float32' + ], + 'fcns': [ + drfftf, + srfftf + ], + 'default': grfftf +}; + + +// MAIN // + +/** +* Computes the forward discrete Fourier transform of a real-valued ndarray in-place using FFTPACK. +* +* @private +* @name rfftf +* @type {Function} +* @param {ndarray} r - working buffer ndarray (modified in-place) +* @param {ndarray} workspace - pre-initialized workspace ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} second argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} working buffer ndarray +* +* @example +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var rffti = require( '@stdlib/fft/fftpack/rffti' ); +* +* var r = zeros( [ 4 ], { +* 'dtype': 'float64' +* }); +* var ws = rffti( 4 ); +* +* var out = rfftf( r, ws ); +* // returns +*/ +var rfftf = factory( table, [ idtypes0 ], odtypes ); + + +// EXPORTS // + +module.exports = rfftf; diff --git a/lib/node_modules/@stdlib/fft/rfftf/lib/defaults.js b/lib/node_modules/@stdlib/fft/rfftf/lib/defaults.js new file mode 100644 index 000000000000..8fb57207ca24 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/lib/defaults.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +// MAIN // + +/** +* Returns default options. +* +* @private +* @returns {Object} default options +*/ +function defaults() { + return { + 'n': -1, + 'dims': [ -1 ], + 'normalize': 'backward' + }; +} + + +// EXPORTS // + +module.exports = defaults; diff --git a/lib/node_modules/@stdlib/fft/rfftf/lib/index.js b/lib/node_modules/@stdlib/fft/rfftf/lib/index.js new file mode 100644 index 000000000000..ab744949529e --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/lib/index.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +/** +* Return a new ndarray containing the one-dimensional discrete Fourier transform of a real-valued input ndarray along a specified dimension. +* +* @module @stdlib/fft/rfftf +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var rfftf = require( '@stdlib/fft/rfftf' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 4 ]; +* +* // Define the array strides: +* var sx = [ 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform operation: +* var out = rfftf( x ); +* // returns [ [ 10.0, 0.0 ], [ -2.0, 2.0 ], [ -2.0, 0.0 ] ] +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var rfftf = require( '@stdlib/fft/rfftf' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 4 ]; +* +* // Define the array strides: +* var sx = [ 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Create an output ndarray: +* var out = zeros( [ 3 ], { 'dtype': 'complex128' } ); +* +* // Perform operation: +* var v = rfftf.assign( x, out ); +* // returns [ [ 10.0, 0.0 ], [ -2.0, 2.0 ], [ -2.0, 0.0 ] ] +* +* var bool = ( v === out ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/fft/rfftf/lib/main.js b/lib/node_modules/@stdlib/fft/rfftf/lib/main.js new file mode 100644 index 000000000000..aa6db2956b94 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/lib/main.js @@ -0,0 +1,247 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +/* eslint-disable max-len */ + +// MODULES // + +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives; +var isEmptyCollection = require( '@stdlib/assert/is-empty-collection' ); +var isEven = require( '@stdlib/math/base/assert/is-even' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var unaryOutputDataType = require( '@stdlib/ndarray/base/unary-output-dtype' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var maybeBroadcast = require( '@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var getStrides = require( '@stdlib/ndarray/strides' ); +var sliceDimTo = require( '@stdlib/ndarray/base/slice-dimension-to' ); +var sliceDim = require( '@stdlib/ndarray/base/slice-dimension' ); +var broadcastArray = require( '@stdlib/ndarray/base/broadcast-array' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarrayAssign = require( '@stdlib/ndarray/base/assign' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var reinterpretComplex = require( '@stdlib/strided/base/reinterpret-complex' ); +var copyWithin = require( '@stdlib/blas/ext/copy-within' ); +var scal = require( '@stdlib/blas/scal' ); +var rffti = require( '@stdlib/fft/fftpack/rffti' ); +var contains = require( '@stdlib/array/base/assert/contains' ); +var join = require( '@stdlib/array/base/join' ); +var arrayWith = require( '@stdlib/array/base/with' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var format = require( '@stdlib/string/format' ); +var defaults = require( './defaults.js' ); +var base = require( './base.js' ); + + +// VARIABLES // + +var NORMS = [ 'backward', 'orthonormal', 'forward' ]; + + +// MAIN // + +/** +* Returns a new ndarray containing the one-dimensional discrete Fourier transform of a real-valued input ndarray along a specified dimension. +* +* @param {ndarrayLike} x - input ndarray +* @param {Options} [options] - function options +* @param {NonNegativeInteger} [options.n] - number of points over which to compute the transform +* @param {string} [options.normalize="backward"] - normalization mode +* @param {IntegerArray} [options.dims=[-1]] - list of dimensions over which to perform operation +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 2, 4 ]; +* +* // Define the array strides: +* var sx = [ 4, 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform operation: +* var out = rfftf( x ); +* // returns +*/ +function rfftf( x ) { + var options; + var rbuf; + var ndim; + var xdt; + var odt; + var osh; + var rsh; + var rst; + var rdt; + var ord; + var sh; + var ws; + var rv; + var M; + var N; + var r; + var n; + var o; + var d; + var i; + + // Resolve input ndarray meta data: + xdt = getDType( x ); + sh = getShape( x ); + ord = getOrder( x ); + ndim = sh.length; + + options = defaults(); + if ( arguments.length > 1 ) { + if ( hasOwnProp( arguments[ 1 ], 'dims' ) ) { + if ( !isIntegerArray( arguments[ 1 ].dims ) && !isEmptyCollection( arguments[ 1 ].dims ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be an array of integers. Option: `%s`.', 'dims', arguments[ 1 ].dims ) ); + } + options.dims = arguments[ 1 ].dims; + } + if ( hasOwnProp( arguments[ 1 ], 'n' ) ) { + if ( !isNonNegativeInteger( arguments[ 1 ].n ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', 'n', arguments[ 1 ].n ) ); + } + options.n = arguments[ 1 ].n; + } + if ( hasOwnProp( arguments[ 1 ], 'normalize' ) ) { + if ( !contains( NORMS, arguments[ 1 ].normalize ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Option: `%s`.', 'normalize', join( NORMS, '", "' ), arguments[ 1 ].normalize ) ); + } + options.normalize = arguments[ 1 ].normalize; + } + } + d = normalizeIndex( options.dims[ 0 ], ndim - 1 ); + if ( d === -1 ) { + throw new RangeError( format( 'invalid option. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', ndim, options.dims[ 0 ] ) ); + } + options.dims = [ d ]; + N = sh[ d ]; + if ( options.n === -1 ) { + options.n = N; + } + n = options.n; + M = floor( n/2 ) + 1; + + // Resolve the output complex data type and corresponding real data type: + odt = unaryOutputDataType( xdt, 'complex_floating_point' ); + rdt = unaryOutputDataType( odt, 'real_floating_point' ); + + // Replace the transform dimension with the output length: + osh = arrayWith( sh, d, M ); + + // Create the zero-filled output ndarray: + o = zeros( osh, { + 'dtype': odt, + 'order': ord + }); + + // Reinterpret the complex output as a real-valued ndarray: + rbuf = reinterpretComplex( getData( o ), 0 ); + rsh = arrayWith( osh, d, 2*M ); + rst = copyIndexed( getStrides( o ) ); + for ( i = 0; i < ndim; i++ ) { + rst[ i ] *= 2; + } + rst[ d ] = 1; + rv = new ndarray( rdt, rbuf, rsh, rst, 0, ord ); + + // Use the first n elements of the real view as the working buffer: + r = sliceDimTo( rv, d, n, false, true ); + + // Copy the input into the working buffer: + if ( n <= N ) { + ndarrayAssign( [ sliceDimTo( x, d, n, false, false ), r ] ); + } else { + ndarrayAssign( [ x, sliceDimTo( r, d, N, false, true ) ] ); + } + + // Allocate and initialize the workspace: + ws = rffti( n, { + 'dtype': rdt + }); + + // Broadcast the workspace with batch dimensions so it is reused across fibers: + ws = maybeBroadcast( ws, sh, [ d - ndim ] ); + + // Compute the forward FFT in-place: + base( r, ws, options ); + + if ( n > 1 ) { + // Move the Nyquist component to its final position (even-length only): + if ( isEven( n ) ) { + copyWithin( rv, ( 2*M ) - 2, n-1, n, { + 'dim': d-ndim + }); + } + // Shift the middle frequency bins right by one position: + if ( n > 2 ) { + copyWithin( rv, 2, 1, n - ( ( isEven( n ) ) ? 1 : 0 ), { + 'dim': d - ndim + }); + } + // Zero-fill Im(DC): + r = sliceDim( rv, d, 1, false, true ); + ndarrayAssign([ + broadcastArray( scalar2ndarray( 0.0, { + 'dtype': rdt + }), getShape( r ) ), + r + ]); + } + + // Normalization: + if ( options.normalize !== 'backward' ) { + if ( options.normalize === 'orthonormal' ) { + scal( o, 1.0/sqrt(n), options ); + } else { + scal( o, 1.0/n, options ); + } + } + return o; +} + + +// EXPORTS // + +module.exports = rfftf; diff --git a/lib/node_modules/@stdlib/fft/rfftf/package.json b/lib/node_modules/@stdlib/fft/rfftf/package.json new file mode 100644 index 000000000000..5dd00368632a --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/fft/rfftf", + "version": "0.0.0", + "description": "Return a new ndarray containing the one-dimensional discrete Fourier transform of a real-valued input ndarray.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "fft", + "fourier", + "transform", + "forward", + "real", + "rfft", + "dft", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/fft/rfftf/test/test.assign.js b/lib/node_modules/@stdlib/fft/rfftf/test/test.assign.js new file mode 100644 index 000000000000..9053adfabc41 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/test/test.assign.js @@ -0,0 +1,598 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' ); +var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' ); +var isAlmostSameValue = require( '@stdlib/number/float64/base/assert/is-almost-same-value' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var assign = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof assign, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + assign( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + assign( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + assign( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + assign( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + values = [ + '5', + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + assign( x, out, value ); + }; + } +}); + +tape( 'the function computes the FFT and assigns results to an output ndarray (float64, row-major)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + var v; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + v = assign( x, out ); + view = reinterpret128( getData( v ), 0 ); + + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( getDType( v ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( v ), [ 3 ], 'returns expected value' ); + + // Expected: (10+0i), (-2+2i), (-2+0i): + expected = new Float64Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the FFT and assigns results to an output ndarray (float64, column-major)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + var v; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'column-major' ); + out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + v = assign( x, out ); + view = reinterpret128( getData( v ), 0 ); + + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( getDType( v ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( v ), [ 3 ], 'returns expected value' ); + + expected = new Float64Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the FFT and assigns results to an output ndarray (float32, row-major)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + var v; + + xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + out = zeros( [ 3 ], { + 'dtype': 'complex64' + }); + + v = assign( x, out ); + view = reinterpret64( getData( v ), 0 ); + + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( getDType( v ), 'complex64', 'returns expected value' ); + t.deepEqual( getShape( v ), [ 3 ], 'returns expected value' ); + + expected = new Float32Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the FFT and assigns results to an output ndarray (float32, column-major)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + var v; + + xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'column-major' ); + out = zeros( [ 3 ], { + 'dtype': 'complex64' + }); + + v = assign( x, out ); + view = reinterpret64( getData( v ), 0 ); + + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( getDType( v ), 'complex64', 'returns expected value' ); + t.deepEqual( getShape( v ), [ 3 ], 'returns expected value' ); + + expected = new Float32Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the FFT of an odd-length input', function test( t ) { + var view; + var xbuf; + var out; + var x; + var v; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + x = new ndarray( 'float64', xbuf, [ 5 ], [ 1 ], 0, 'row-major' ); + out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + v = assign( x, out ); + view = reinterpret128( getData( v ), 0 ); + + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( getDType( v ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( v ), [ 3 ], 'returns expected value' ); + + // DC component: + t.strictEqual( view[ 0 ], 15.0, 'returns expected value' ); + t.strictEqual( view[ 1 ], 0.0, 'returns expected value' ); + + // No Nyquist component for odd N: + t.strictEqual( out.length, 3, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the FFT of a length-1 input (DC only)', function test( t ) { + var view; + var xbuf; + var out; + var x; + var v; + + xbuf = new Float64Array( [ 7.0 ] ); + x = new ndarray( 'float64', xbuf, [ 1 ], [ 1 ], 0, 'row-major' ); + out = zeros( [ 1 ], { + 'dtype': 'complex128' + }); + + v = assign( x, out ); + view = reinterpret128( getData( v ), 0 ); + + t.strictEqual( v, out, 'returns expected value' ); + t.deepEqual( getShape( v ), [ 1 ], 'returns expected value' ); + t.strictEqual( view[ 0 ], 7.0, 'returns expected value' ); + t.strictEqual( view[ 1 ], 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports the `n` option for zero-padding (n > N)', function test( t ) { + var view; + var xbuf; + var out; + var x; + var v; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + out = zeros( [ 5 ], { + 'dtype': 'complex128' + }); + + v = assign( x, out, { + 'n': 8 + }); + view = reinterpret128( getData( v ), 0 ); + + t.strictEqual( v, out, 'returns expected value' ); + t.deepEqual( getShape( v ), [ 5 ], 'returns expected value' ); + + // DC component: sum of [ 1.0, 2.0, 3.0, 4.0, 0.0, 0.0, 0.0, 0.0 ] = 10.0: + t.strictEqual( view[ 0 ], 10.0, 'returns expected value' ); + t.strictEqual( view[ 1 ], 0.0, 'returns expected value' ); + + // Nyquist component (even n=8): + t.strictEqual( view[ 9 ], 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports the `n` option for truncation (n < N)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + var v; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + out = zeros( [ 2 ], { + 'dtype': 'complex128' + }); + + v = assign( x, out, { + 'n': 2 + }); + view = reinterpret128( getData( v ), 0 ); + + t.strictEqual( v, out, 'returns expected value' ); + t.deepEqual( getShape( v ), [ 2 ], 'returns expected value' ); + + expected = new Float64Array( [ 3.0, 0.0, -1.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports the `normalize` option (ortho)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + assign( x, out, { + 'normalize': 'ortho' + }); + view = reinterpret128( getData( out ), 0 ); + + // Ortho = backward / sqrt(N) = [ 10, -2+2i, -2 ] / 2 = [ 5, -1+1i, -1 ]: + expected = new Float64Array( [ 5.0, 0.0, -1.0, 1.0, -1.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports the `normalize` option (forward)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + assign( x, out, { + 'normalize': 'forward' + }); + view = reinterpret128( getData( out ), 0 ); + + // Forward = backward / N = [ 10, -2+2i, -2 ] / 4 = [ 2.5, -0.5+0.5i, -0.5 ]: + expected = new Float64Array( [ 2.5, 0.0, -0.5, 0.5, -0.5, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports `n` and `normalize` options together', function test( t ) { + var xbuf; + var view; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + out = zeros( [ 5 ], { + 'dtype': 'complex128' + }); + + assign( x, out, { + 'n': 8, + 'normalize': 'ortho' + }); + view = reinterpret128( getData( out ), 0 ); + + t.deepEqual( getShape( out ), [ 5 ], 'returns expected value' ); + + // DC component: 10 / sqrt(8): + t.strictEqual( isAlmostSameValue( view[ 0 ], 10.0 / sqrt( 8 ), 1 ), true, 'returns expected value' ); + t.strictEqual( view[ 1 ], 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports strided input', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + var v; + + xbuf = new Float64Array([ + 1.0, // 0 + 99.0, + 2.0, // 1 + 99.0, + 3.0, // 2 + 99.0, + 4.0, // 3 + 99.0 + ]); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 2 ], 0, 'row-major' ); + out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + v = assign( x, out ); + view = reinterpret128( getData( v ), 0 ); + + t.strictEqual( v, out, 'returns expected value' ); + expected = new Float64Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports input with an offset', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + var v; + + xbuf = new Float64Array([ + 99.0, + 99.0, + 1.0, // 0 + 2.0, // 1 + 3.0, // 2 + 4.0 // 3 + ]); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 2, 'row-major' ); + out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + v = assign( x, out ); + view = reinterpret128( getData( v ), 0 ); + + t.strictEqual( v, out, 'returns expected value' ); + expected = new Float64Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the FFT of a generic input', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + var v; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + out = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + + v = assign( x, out ); + view = reinterpret128( getData( v ), 0 ); + + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( getDType( v ), 'complex128', 'returns expected value' ); + expected = new Float64Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/fft/rfftf/test/test.js b/lib/node_modules/@stdlib/fft/rfftf/test/test.js new file mode 100644 index 000000000000..5490119e64e4 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/test/test.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isMethod = require( '@stdlib/assert/is-method' ); +var rfftf = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof rfftf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( rfftf, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/fft/rfftf/test/test.main.js b/lib/node_modules/@stdlib/fft/rfftf/test/test.main.js new file mode 100644 index 000000000000..c8dc996270e4 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/test/test.main.js @@ -0,0 +1,444 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' ); +var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' ); +var isAlmostSameValue = require( '@stdlib/number/float64/base/assert/is-almost-same-value' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var rfftf = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof rfftf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + rfftf( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + rfftf( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + + values = [ + '5', + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + rfftf( x, value ); + }; + } +}); + +tape( 'the function computes the FFT of a real-valued input (float64, row-major)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + out = rfftf( x ); + view = reinterpret128( getData( out ), 0 ); + + t.strictEqual( getDType( out ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 3 ], 'returns expected value' ); + + // Expected: (10+0i), (-2+2i), (-2+0i): + expected = new Float64Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the FFT of a real-valued input (float64, column-major)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'column-major' ); + out = rfftf( x ); + view = reinterpret128( getData( out ), 0 ); + + t.strictEqual( getDType( out ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 3 ], 'returns expected value' ); + + expected = new Float64Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the FFT of a real-valued input (float32, row-major)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + + xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + out = rfftf( x ); + view = reinterpret64( getData( out ), 0 ); + + t.strictEqual( getDType( out ), 'complex64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 3 ], 'returns expected value' ); + + expected = new Float32Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the FFT of a real-valued input (float32, column-major)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + + xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'column-major' ); + out = rfftf( x ); + view = reinterpret64( getData( out ), 0 ); + + t.strictEqual( getDType( out ), 'complex64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 3 ], 'returns expected value' ); + + expected = new Float32Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the FFT of an odd-length input', function test( t ) { + var view; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + x = new ndarray( 'float64', xbuf, [ 5 ], [ 1 ], 0, 'row-major' ); + out = rfftf( x ); + view = reinterpret128( getData( out ), 0 ); + + t.strictEqual( getDType( out ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 3 ], 'returns expected value' ); + + // DC component: + t.strictEqual( view[ 0 ], 15.0, 'returns expected value' ); + t.strictEqual( view[ 1 ], 0.0, 'returns expected value' ); + + // No Nyquist component for odd N: + t.strictEqual( out.length, 3, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the FFT of a length-1 input (DC only)', function test( t ) { + var view; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 7.0 ] ); + x = new ndarray( 'float64', xbuf, [ 1 ], [ 1 ], 0, 'row-major' ); + out = rfftf( x ); + view = reinterpret128( getData( out ), 0 ); + + t.deepEqual( getShape( out ), [ 1 ], 'returns expected value' ); + t.strictEqual( view[ 0 ], 7.0, 'returns expected value' ); + t.strictEqual( view[ 1 ], 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports the `n` option for zero-padding (n > N)', function test( t ) { + var view; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + out = rfftf( x, { + 'n': 8 + }); + view = reinterpret128( getData( out ), 0 ); + + t.deepEqual( getShape( out ), [ 5 ], 'returns expected value' ); + + // DC component: sum of [ 1.0, 2.0, 3.0, 4.0, 0.0, 0.0, 0.0, 0.0 ] = 10.0: + t.strictEqual( view[ 0 ], 10.0, 'returns expected value' ); + t.strictEqual( view[ 1 ], 0.0, 'returns expected value' ); + + // Nyquist component (even n=8): + t.strictEqual( view[ 9 ], 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports the `n` option for truncation (n < N)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + out = rfftf( x, { + 'n': 2 + }); + view = reinterpret128( getData( out ), 0 ); + + t.deepEqual( getShape( out ), [ 2 ], 'returns expected value' ); + expected = new Float64Array( [ 3.0, 0.0, -1.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports the `normalize` option (ortho)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + out = rfftf( x, { + 'normalize': 'ortho' + }); + view = reinterpret128( getData( out ), 0 ); + + // Ortho = backward / sqrt(N) = [ 10, -2+2i, -2 ] / 2 = [ 5, -1+1i, -1 ]: + expected = new Float64Array( [ 5.0, 0.0, -1.0, 1.0, -1.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports the `normalize` option (forward)', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + out = rfftf( x, { + 'normalize': 'forward' + }); + view = reinterpret128( getData( out ), 0 ); + + // Forward = backward / N = [ 10, -2+2i, -2 ] / 4 = [ 2.5, -0.5+0.5i, -0.5 ]: + expected = new Float64Array( [ 2.5, 0.0, -0.5, 0.5, -0.5, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports `n` and `normalize` options together', function test( t ) { + var xbuf; + var view; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + out = rfftf( x, { + 'n': 8, + 'normalize': 'ortho' + }); + view = reinterpret128( getData( out ), 0 ); + + t.deepEqual( getShape( out ), [ 5 ], 'returns expected value' ); + + // DC component: 10 / sqrt(8): + t.strictEqual( isAlmostSameValue( view[ 0 ], 10.0 / sqrt( 8 ), 1 ), true, 'returns expected value' ); + t.strictEqual( view[ 1 ], 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports strided input', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + + xbuf = new Float64Array([ + 1.0, // 0 + 99.0, + 2.0, // 1 + 99.0, + 3.0, // 2 + 99.0, + 4.0, // 3 + 99.0 + ]); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 2 ], 0, 'row-major' ); + out = rfftf( x ); + view = reinterpret128( getData( out ), 0 ); + + expected = new Float64Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports input with an offset', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + + xbuf = new Float64Array([ + 99.0, + 99.0, + 1.0, // 0 + 2.0, // 1 + 3.0, // 2 + 4.0 // 3 + ]); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 2, 'row-major' ); + out = rfftf( x ); + view = reinterpret128( getData( out ), 0 ); + + expected = new Float64Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the FFT of a generic input', function test( t ) { + var expected; + var view; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + out = rfftf( x ); + view = reinterpret128( getData( out ), 0 ); + + t.strictEqual( getDType( out ), 'complex128', 'returns expected value' ); + expected = new Float64Array( [ 10.0, 0.0, -2.0, 2.0, -2.0, 0.0 ] ); + t.deepEqual( view, expected, 'returns expected value' ); + + t.end(); +});