Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 20x 20x 21x 21x 21x 21x 21x 41x 12x 12x 7x 7x 12x 3x 3x 3x 12x 3x 3x 3x 12x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 41x 3x 1x 1x 3x 2x 2x 2x 2x 2x 3x 14x 14x 14x 14x 41x 3x 3x 3x 3x 2x 3x 1x 1x 3x 3x 14x 41x 2x 2x 2x 2x 2x | /**
* @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 <ndarray>[ <Complex128>[ 10.0, 0.0 ], <Complex128>[ -2.0, 2.0 ], <Complex128>[ -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;
|