From 4e98a1bc91be1b1a1b715ea7ac87284f80697a35 Mon Sep 17 00:00:00 2001 From: Gunj Joshi Date: Mon, 12 Oct 2026 23:47:12 +0530 Subject: [PATCH] feat: add fft/rfftf --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/fft/rfftf/README.md | 186 ++++++ .../fft/rfftf/benchmark/benchmark.assign.js | 118 ++++ .../@stdlib/fft/rfftf/benchmark/benchmark.js | 107 ++++ .../@stdlib/fft/rfftf/docs/repl.txt | 90 +++ .../@stdlib/fft/rfftf/docs/types/index.d.ts | 187 ++++++ .../@stdlib/fft/rfftf/docs/types/test.ts | 182 ++++++ .../@stdlib/fft/rfftf/examples/index.js | 35 + .../@stdlib/fft/rfftf/lib/assign.js | 178 ++++++ .../@stdlib/fft/rfftf/lib/base.js | 192 ++++++ .../@stdlib/fft/rfftf/lib/index.js | 98 +++ .../@stdlib/fft/rfftf/lib/main.js | 177 ++++++ .../@stdlib/fft/rfftf/package.json | 66 ++ .../@stdlib/fft/rfftf/test/test.assign.js | 598 ++++++++++++++++++ .../@stdlib/fft/rfftf/test/test.js | 39 ++ .../@stdlib/fft/rfftf/test/test.main.js | 444 +++++++++++++ 15 files changed, 2697 insertions(+) create mode 100644 lib/node_modules/@stdlib/fft/rfftf/README.md create mode 100644 lib/node_modules/@stdlib/fft/rfftf/benchmark/benchmark.assign.js create mode 100644 lib/node_modules/@stdlib/fft/rfftf/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/fft/rfftf/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/fft/rfftf/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/fft/rfftf/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/fft/rfftf/examples/index.js create mode 100644 lib/node_modules/@stdlib/fft/rfftf/lib/assign.js create mode 100644 lib/node_modules/@stdlib/fft/rfftf/lib/base.js create mode 100644 lib/node_modules/@stdlib/fft/rfftf/lib/index.js create mode 100644 lib/node_modules/@stdlib/fft/rfftf/lib/main.js create mode 100644 lib/node_modules/@stdlib/fft/rfftf/package.json create mode 100644 lib/node_modules/@stdlib/fft/rfftf/test/test.assign.js create mode 100644 lib/node_modules/@stdlib/fft/rfftf/test/test.js create mode 100644 lib/node_modules/@stdlib/fft/rfftf/test/test.main.js 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..3223f737b66e --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/lib/base.js @@ -0,0 +1,192 @@ +/** +* @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 vars-on-top */ + +// MODULES // + +var dtypes = require( '@stdlib/ndarray/dtypes' ); +var factory = require( '@stdlib/ndarray/base/nullary-strided1d-dispatch-factory' ); +var isEven = require( '@stdlib/math/base/assert/is-even' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var gcopy = require( '@stdlib/blas/base/gcopy' ).ndarray; +var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); +var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' ); +var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var drffti = require( '@stdlib/fft/base/fftpack/float64/rffti' ); +var drfftf = require( '@stdlib/fft/base/fftpack/float64/rfftf' ); +var srffti = require( '@stdlib/fft/base/fftpack/float32/rffti' ); +var srfftf = require( '@stdlib/fft/base/fftpack/float32/rfftf' ); +var grffti = require( '@stdlib/fft/base/fftpack/generic/rffti' ); +var grfftf = require( '@stdlib/fft/base/fftpack/generic/rfftf' ); + + +// FUNCTIONS // + +/** +* Computes the forward FFT and writes results to the output complex ndarray. +* +* @private +* @param {Function} reinterpret - complex-to-real reinterpret function +* @param {Function} rffti - workspace initialization function +* @param {Function} rfftf - forward FFT function +* @param {Function} Workspace - typed array constructor for workspace +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {void} +*/ +function kernel( reinterpret, rffti, rfftf, Workspace, arrays ) { + var view; + var out; + var M; + var N; + var w; + var x; + + out = arrays[ 0 ]; + x = arrays[ 1 ]; + N = numelDimension( x, 0 ); + M = floor( N/2 ) + 1; + view = reinterpret( getData( out ), 0 ); + + if ( N === 0 ) { + return; + } + // Copy the input into the output buffer at offset 1: + gcopy( N, getData( x ), getStride( x, 0 ), getOffset( x ), view, 1, 1 ); + + // Initialize the workspace and compute the forward FFT in-place: + w = new Workspace( ( 2*N ) + 34 ); + rffti( N, w, 1, 0 ); + rfftf( N, view, 1, 1, w, 1, 0 ); + + // Move the DC component from the imaginary slot to the real slot of the first complex element and zero out the imaginary part: + view[ 0 ] = view[ 1 ]; + view[ 1 ] = 0.0; + + // For even-length sequences, zero out the imaginary part of the Nyquist component: + if ( isEven( N ) ) { + view[ ( 2*M ) - 1 ] = 0.0; + } +} + +/** +* Computes the forward FFT of a real-valued input. +* +* @private +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {void} +*/ +function gkernel( arrays ) { + kernel( reinterpret, grffti, grfftf, Float64Array, arrays ); +} + +/** +* Computes the forward FFT of a real-valued double-precision input. +* +* @private +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {void} +*/ +function dkernel( arrays ) { + kernel( reinterpret128, drffti, drfftf, Float64Array, arrays ); +} + +/** +* Computes the forward FFT of a real-valued single-precision input. +* +* @private +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {void} +*/ +function skernel( arrays ) { + kernel( reinterpret64, srffti, srfftf, Float32Array, arrays ); +} + + +// VARIABLES // + +var idtypes = dtypes( 'real_and_generic' ); +var odtypes = dtypes( 'complex_floating_point' ); +var table = { + 'types': [ + 'complex128', + 'complex64' + ], + 'fcns': [ + dkernel, + skernel + ], + 'default': gkernel +}; + + +// MAIN // + +/** +* Computes the one-dimensional discrete Fourier transform of a real-valued input ndarray and assigns the results to a provided output ndarray. +* +* @private +* @name rfftf +* @type {Function} +* @param {ndarray} out - output ndarray +* @param {ndarray} x - input ndarray +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} second argument must be an ndarray-like object +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var Complex128Array = require( '@stdlib/array/complex128' ); +* 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' ); +* +* // Create an output ndarray: +* var out = new ndarray( 'complex128', new Complex128Array( 3 ), [ 3 ], [ 1 ], 0, 'row-major' ); +* +* // Perform operation: +* var v = rfftf( out, x ); +* // returns [ [ 10.0, 0.0 ], [ -2.0, 2.0 ], [ -2.0, 0.0 ] ] +*/ +var rfftf = factory( table, [ idtypes ], odtypes ); + + +// EXPORTS // + +module.exports = rfftf; 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..56d823be6d8f --- /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. +* +* @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..8bdc39e1de11 --- /dev/null +++ b/lib/node_modules/@stdlib/fft/rfftf/lib/main.js @@ -0,0 +1,177 @@ +/** +* @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 unaryOutputDataType = require( '@stdlib/ndarray/base/unary-output-dtype' ); +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 empty = require( '@stdlib/ndarray/empty' ); +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 // + +/** +* Returns a new ndarray containing the one-dimensional discrete Fourier transform of a real-valued input ndarray. +* +* @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 +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @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 ] ); +* +* // 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 ] ] +*/ +function rfftf( x ) { + var options; + var norm; + var view; + var xdt; + var odt; + var sh; + var M; + var N; + var n; + var o; + var y; + var s; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + norm = 'backward'; + xdt = getDType( x ); + sh = getShape( x ); + N = sh[ 0 ]; + n = N; + if ( arguments.length > 1 ) { + options = arguments[ 1 ]; + 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; + + // Resolve the output complex data type: + odt = unaryOutputDataType( xdt, 'complex_floating_point' ); + + // Create the output ndarray: + o = empty( [ M ], { + 'dtype': odt, + 'order': getOrder( x ) + }); + + // 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( getDType( x ), [ 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( o, x ); + + // Apply normalization: + if ( norm !== 'backward' ) { + if ( odt === 'complex128' ) { + view = reinterpret128( getData( o ), 0 ); + } else { + view = reinterpret64( getData( o ), 0 ); + } + if ( norm === 'ortho' ) { + s = 1.0 / sqrt( n ); + } else { + s = 1.0 / n; + } + gscal( 2*M, s, view, 1, 0 ); + } + 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(); +});