MathCore’sDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Nonanti/mathcore/llms.txt
Use this file to discover all available pages before exploring further.
transforms module brings signal processing directly into your symbolic math pipeline. At its core is the FFT struct, which provides a fast Fourier transform, a reference-quality DFT, an inverse FFT, a 2D FFT for image processing, and FFT-accelerated convolution. When the fft feature is enabled, the forward and inverse transforms delegate to rustfft — one of the fastest FFT libraries available in any language. Without the feature, a manual Cooley-Tukey implementation handles power-of-two lengths and falls back to the O(n²) DFT for other sizes.
Import
Feature Flag
Thefft feature gates the rustfft backend. Add it to your Cargo.toml to get the best performance:
Without the
fft feature, FFT::fft uses a built-in Cooley-Tukey recursive implementation for power-of-two lengths, and falls back to FFT::dft (O(n²)) for other lengths. Both paths produce identical numeric results — the feature only affects speed.FFT::fft
FFT::fft(input: &[Complex64]) -> Vec<Complex64>
Computes the forward Discrete Fourier Transform of a complex-valued signal. With the fft feature enabled, this uses rustfft’s highly optimised mixed-radix Cooley-Tukey algorithm. Without the feature, it falls back to the manual recursive implementation.
FFT::dft
FFT::dft(input: &[Complex64]) -> Vec<Complex64>
The reference O(n²) Discrete Fourier Transform. Always available regardless of feature flags. Computes the same transform as fft but without any algorithmic speedup — suitable for small inputs, verification, or inputs whose length is not a power of two and rustfft is not enabled.
FFT::ifft
FFT::ifft(input: &[Complex64]) -> Vec<Complex64>
Computes the inverse FFT using the conjugate trick: conjugate the input, apply the forward FFT, conjugate and scale the output by 1/N. The result satisfies ifft(fft(x)) ≈ x up to floating-point rounding.
FFT::power_spectrum
FFT::power_spectrum(signal: &[f64]) -> Vec<f64>
Convenience wrapper: converts a real-valued &[f64] to complex, runs the FFT, and returns |X[k]|² for each frequency bin. Useful for spectral analysis of real signals without manual complex conversion.
FFT::fft2d
FFT::fft2d(input: &[Vec<Complex64>]) -> Vec<Vec<Complex64>>
2D FFT for image and matrix analysis: applies the 1D FFT across all rows first, then across all columns. The output has the same dimensions as the input.
FFT::convolve
FFT::convolve(a: &[f64], b: &[f64]) -> Vec<f64>
Computes the linear convolution of two real-valued signals using the FFT overlap-add approach: both signals are zero-padded to the next power of two, transformed, pointwise-multiplied in the frequency domain, then inverse-transformed. This is O(n log n) compared to O(n²) for direct convolution.
Input Length and Performance
Power-of-two lengths (2, 4, 8, 16, 32, 64, …) are the most efficient for the Cooley-Tukey algorithm. What happens for other lengths depends on which backend is active:| Condition | Behaviour |
|---|---|
fft feature enabled, any length | rustfft’s mixed-radix algorithm handles all lengths efficiently |
fft feature disabled, length is a power of two | Built-in Cooley-Tukey recursive FFT — O(n log n) |
fft feature disabled, length is not a power of two | Falls back to FFT::dft — O(n²) |
Full Example
Enabling the fft Feature
Add the following to your Cargo.toml:
FFT::fft and FFT::ifft use rustfft’s planner internally. The planner caches twiddle factors and FFT plans, so repeated transforms of the same length are especially fast.