Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Avhz RustQuant FFT

From Leeroopedia


Knowledge Sources
Domains Mathematics, Statistics
Last Updated 2026-02-07 19:00 GMT

Overview

Concrete tool for computing Fast Fourier Transforms (FFT) on real and complex data provided by the RustQuant library.

Description

The fft module implements the Cooley-Tukey radix-2 FFT algorithm for both real-valued and complex-valued input data. The module provides four public FFT functions in two variants (in-place and returning a new vector) for each data type (real and complex).

Public functions:

  • fft_real_inplace(x) -- Computes the real FFT in-place, modifying the input slice directly.
  • fft_real(x) -- Computes the real FFT and returns a new vector, leaving the original unchanged.
  • fft_complex_inplace(x) -- Computes the complex FFT in-place.
  • fft_complex(x) -- Computes the complex FFT and returns a new vector.
  • is_valid_length(x) -- Helper function that checks whether a slice length is a power of 2.

All FFT functions require the input length to be a power of 2 and will panic otherwise. The algorithm works recursively by splitting the input into even and odd indexed elements, computing the FFT of each half, and combining the results using the twiddle factors e^(-2*pi*i*k/n).

The real FFT uses the norm (magnitude) of the complex twiddle factor, while the complex FFT performs full complex multiplication. The internal split_array function handles the even/odd decomposition.

Usage

Use this module when you need to perform spectral analysis of time series data. In quantitative finance, FFT is used for option pricing via characteristic functions (Carr-Madan method), spectral density estimation, signal processing of financial time series, and efficient computation of convolutions for portfolio risk aggregation.

Code Reference

Source Location

Signature

/// Real FFT in-place. Input length must be a power of 2.
pub fn fft_real_inplace(x: &mut [f64]);

/// Real FFT returning a new vector. Input length must be a power of 2.
pub fn fft_real(x: &[f64]) -> Vec<f64>;

/// Complex FFT in-place. Input length must be a power of 2.
pub fn fft_complex_inplace(x: &mut [Complex<f64>]);

/// Complex FFT returning a new vector. Input length must be a power of 2.
pub fn fft_complex(x: &[Complex<f64>]) -> Vec<Complex<f64>>;

/// Check if a vector length is a power of 2.
pub fn is_valid_length<T>(x: &[T]) -> bool;

Import

use RustQuant::math::{fft_real, fft_real_inplace, fft_complex, fft_complex_inplace, is_valid_length};

I/O Contract

Inputs

Name Type Required Description
x (real) &[f64] or &mut [f64] Yes A slice of real-valued data. Length must be a power of 2.
x (complex) &[Complex<f64>] or &mut [Complex<f64>] Yes A slice of complex-valued data. Length must be a power of 2.

Outputs

Name Type Description
fft_real() Vec<f64> The FFT magnitudes of the real input data.
fft_complex() Vec<Complex<f64>> The complex FFT coefficients of the input data.
(in-place variants) (mutated input) The input slice is modified to contain the FFT result.
is_valid_length() bool True if the slice length is a power of 2.

Usage Examples

use RustQuant::math::{fft_real, fft_complex, fft_real_inplace, is_valid_length};
use num::Complex;

// Real FFT (returns new vector)
let data = vec![-1.0, 2.0, 3.0, 0.0];
let result = fft_real(&data);
// result: [4.0, ~4.472, 0.0, ~4.472]

// Real FFT in-place
let mut data_mut = vec![-1.0, 2.0, 3.0, 0.0];
fft_real_inplace(&mut data_mut);
// data_mut is now modified with FFT results

// Complex FFT
let complex_data = vec![
    Complex::new(-1.0, 0.0),
    Complex::new(2.0, 0.0),
    Complex::new(3.0, 0.0),
    Complex::new(0.0, 0.0),
];
let complex_result = fft_complex(&complex_data);
// complex_result: [(4,0), (-4,-2), (0,0), (-4,2)]

// Validate length
assert!(is_valid_length(&vec![0; 16]));  // true: 16 = 2^4
assert!(!is_valid_length(&vec![0; 15])); // false: not a power of 2

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment