Core API
Public transform functions, plan management, and spectral utilities.
Arbitrary-precision FFT for mpmath.
- mpmath_fft.build_plan(N)[source]
Build a FFT plan for transform of size N.
Plans are cached by (N, mp.mp.dps) so twiddle factors are recomputed when mpmath precision changes.
The cache holds at most _MAX_PLAN_CACHE_SIZE entries (default 128). When the cache is full, the oldest entry is evicted (FIFO).
Thread-safe: uses double-checked locking to protect the plan cache.
Parameters
- Nint
Transform size.
Returns
- plantuple of ndarray
Plan arrays (type, N, p1, p2, c1, c2, tw_data, tw_start).
- mpmath_fft.clear_plan_cache()[source]
Clear the plan cache.
All cached plans are discarded. Subsequent calls to build_plan() will reconstruct plans at the current mpmath precision.
Thread-safe: acquires the plan cache lock.
- mpmath_fft.fft(x, axis=-1)[source]
Discrete Fourier Transform via Cooley-Tukey / radix-2 / Bluestein decomposition. Operates on numpy object arrays of mpmath mpc values.
Parameters
- xndarray[dtype=object]
Input array of mpc values.
- axisint
Axis along which to compute the FFT (default -1).
Returns
- yndarray[dtype=object]
Transformed array.
Examples
>>> import mpmath as mp >>> import numpy as np >>> mp.mp.dps = 15 >>> x = np.array([mp.mpc(1, 0), mp.mpc(2, 0)], dtype=object) >>> y = fft(x) >>> [complex(v) for v in y.flat] [(3+0j), (-1+0j)]
- mpmath_fft.fftfreq(n, d=1.0)[source]
Frequency bins for an n-point FFT.
Returns frequencies f_k = k / (n*d) for k = 0..n-1, wrapped so that f_k = (k-n) / (n*d) for k > n/2 (negative frequencies).
Parameters
- nint
Number of sample points.
- dfloat or mpmath scalar, optional
Sample spacing (default 1.0). Inverse of sampling rate.
Returns
- fndarray[dtype=object]
Array of mp.mpf values of length n.
Examples
>>> import mpmath as mp >>> mp.mp.dps = 15 >>> f = fftfreq(4, d=1.0) >>> [float(v) for v in f.flat] [0.0, 0.25, -0.5, -0.25]
- mpmath_fft.fftshift(x, axes=None)[source]
Shift zero-frequency component to center of spectrum.
Swaps half-spaces for each specified axis. Operates on object arrays (copies the input).
Parameters
- xndarray[dtype=object]
Input array.
- axesint or tuple of int, optional
Axes to shift. Default: all axes.
Returns
- yndarray[dtype=object]
Shifted array.
Examples
>>> import mpmath as mp >>> import numpy as np >>> x = np.array([mp.mpc(0), mp.mpc(1), mp.mpc(2), mp.mpc(3)], dtype=object) >>> y = fftshift(x) >>> [complex(v) for v in y.flat] [(2+0j), (3+0j), 0j, (1+0j)]
- mpmath_fft.hfft(x, n=None, axis=-1)[source]
Hermitian FFT.
Computes the inverse FFT of a Hermitian-symmetric half-spectrum. Equivalent to n_used * irfft(conj(x), n=n, axis=axis).
Parameters
- xndarray[dtype=object] of mp.mpc
Half-spectrum input.
- nint, optional
Output length. Default: 2 * (x.shape[axis] - 1).
- axisint
Transform axis (default -1).
Returns
- yndarray[dtype=object] of mp.mpf
Real-valued output.
- mpmath_fft.ifft(x, axis=-1)[source]
Inverse FFT: ifft(x) = conj(fft(conj(x))) / N.
Parameters
- xndarray[dtype=object]
Input array of mpc values.
- axisint
Axis along which to compute the iFFT (default -1).
Returns
- yndarray[dtype=object]
Inverse transformed array.
Examples
>>> import mpmath as mp >>> import numpy as np >>> mp.mp.dps = 15 >>> x = np.array([mp.mpc(1, 0), mp.mpc(2, 0)], dtype=object) >>> y = fft(x) >>> z = ifft(y) >>> [complex(v) for v in z.flat] [(1+0j), (2+0j)]
- mpmath_fft.ifftshift(x, axes=None)[source]
Inverse of fftshift.
Undoes the shift so that zero frequency returns to index 0.
Parameters
- xndarray[dtype=object]
Input array.
- axesint or tuple of int, optional
Axes to unshift. Default: all axes.
Returns
- yndarray[dtype=object]
Unshifted array.
- mpmath_fft.ihfft(x, n=None, axis=-1)[source]
Inverse Hermitian FFT.
Computes the forward FFT of a real signal and returns the non-redundant half-spectrum, with 1/n scaling. Equivalent to conj(rfft(x, n=n, axis=axis)) / n_used.
Parameters
- xndarray[dtype=object] of mp.mpf
Real-valued input.
- nint, optional
Transform length. Default: x.shape[axis].
- axisint
Transform axis (default -1).
Returns
- yndarray[dtype=object] of mp.mpc
Half-spectrum output.
- mpmath_fft.irfft(x, n=None, axis=-1)[source]
Complex-to-real inverse FFT.
Reconstructs the full Hermitian-symmetric spectrum from the non-redundant half, computes the inverse DFT, and returns the real part.
Parameters
- xndarray[dtype=object] of mp.mpc
Half-spectrum, shape = (…, M, …) where M = n_orig//2 + 1.
- nint, optional
Output length along transform axis. Default: 2 * (x.shape[axis] - 1).
- axisint
Transform axis (default -1).
Returns
- yndarray[dtype=object] of mp.mpf
Real-valued inverse transform.
- mpmath_fft.rfft(x, n=None, axis=-1)[source]
Real-to-complex FFT.
Computes the forward DFT of real-valued input and returns only the non-redundant half of the spectrum (indices 0..n//2).
Parameters
- xndarray[dtype=object] of mp.mpf
Real-valued input array.
- nint, optional
Transform length. If n > x.shape[axis], zero-pad. If n < x.shape[axis], truncate. Default: x.shape[axis].
- axisint
Transform axis (default -1).
Returns
- yndarray[dtype=object] of mp.mpc
Half-spectrum, shape = (…, n//2 + 1, …).
- mpmath_fft.rfftfreq(n, d=1.0)[source]
Frequency bins for an n-point real FFT.
Returns the non-negative frequencies f_k = k / (n*d) for k = 0..n//2.
Parameters
- nint
Number of sample points.
- dfloat or mpmath scalar, optional
Sample spacing (default 1.0).
Returns
- fndarray[dtype=object] of mp.mpf
Frequencies, shape (n//2 + 1,).