rustnum 0.1.0

NumPy-style n-dimensional arrays for Rust: one array type, NumPy names, pure Rust.
Documentation
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Slice-level FFT kernels. Pure Rust, no `Array` dependency.
//!
//! Complex data is stored as split `re` / `im` slices at the module boundary and as an
//! interleaved private [`Complex`] buffer internally. Twiddle factors are always computed in
//! `f64` and rounded once, so `f32` transforms do not accumulate trigonometric error.

use std::ops::{Add, Div, Mul, Neg, Sub};

/// The few float operations the kernels need; implemented for `f32` and `f64` only.
pub(crate) trait Real:
    Copy
    + PartialEq
    + Add<Output = Self>
    + Sub<Output = Self>
    + Mul<Output = Self>
    + Div<Output = Self>
    + Neg<Output = Self>
{
    const ZERO: Self;
    fn from_f64(v: f64) -> Self;
    fn from_usize(n: usize) -> Self;
}

impl Real for f64 {
    const ZERO: Self = 0.0;
    fn from_f64(v: f64) -> Self {
        v
    }
    fn from_usize(n: usize) -> Self {
        n as f64
    }
}

impl Real for f32 {
    const ZERO: Self = 0.0;
    fn from_f64(v: f64) -> Self {
        v as f32
    }
    fn from_usize(n: usize) -> Self {
        n as f32
    }
}

/// A complex number with real part `re` and imaginary part `im`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Complex<F> {
    pub re: F,
    pub im: F,
}

impl<F: Real> Complex<F> {
    pub(crate) fn new(re: F, im: F) -> Self {
        Complex { re, im }
    }

    fn zero() -> Self {
        Complex::new(F::ZERO, F::ZERO)
    }

    fn conj(self) -> Self {
        Complex::new(self.re, -self.im)
    }

    fn scale(self, s: F) -> Self {
        Complex::new(self.re * s, self.im * s)
    }

    /// `exp(-2 pi i * num / den)`, computed in `f64` from the reduced fraction.
    fn unit(num: usize, den: usize) -> Self {
        // Exact values on the axes, so small integer inputs transform without rounding noise.
        let r = num % den;
        if r == 0 {
            return Complex::new(F::from_f64(1.0), F::ZERO);
        } else if 2 * r == den {
            return Complex::new(F::from_f64(-1.0), F::ZERO);
        } else if 4 * r == den {
            return Complex::new(F::ZERO, F::from_f64(-1.0));
        } else if 4 * r == 3 * den {
            return Complex::new(F::ZERO, F::from_f64(1.0));
        }
        let angle = -2.0 * std::f64::consts::PI * (r as f64) / (den as f64);
        let (s, c) = angle.sin_cos();
        Complex::new(F::from_f64(c), F::from_f64(s))
    }
}

impl<F: Real> Add for Complex<F> {
    type Output = Self;
    fn add(self, o: Self) -> Self {
        Complex::new(self.re + o.re, self.im + o.im)
    }
}

impl<F: Real> Sub for Complex<F> {
    type Output = Self;
    fn sub(self, o: Self) -> Self {
        Complex::new(self.re - o.re, self.im - o.im)
    }
}

impl<F: Real> Mul for Complex<F> {
    type Output = Self;
    fn mul(self, o: Self) -> Self {
        Complex::new(
            self.re * o.re - self.im * o.im,
            self.re * o.im + self.im * o.re,
        )
    }
}

/// Non-power-of-two lengths up to this size use the direct O(n^2) DFT, which is the most
/// accurate option and cheaper than Bluestein for tiny n.
const DIRECT_MAX: usize = 16;

// ---------------------------------------------------------------------------------------
// Core in-place transforms (forward, unnormalised)
// ---------------------------------------------------------------------------------------

/// In-place forward DFT `X[k] = sum_j x[j] exp(-2 pi i j k / n)`, any length.
fn forward<F: Real>(buf: &mut [Complex<F>]) {
    let n = buf.len();
    if n <= 1 {
        return;
    }
    if n.is_power_of_two() {
        radix2(buf);
    } else if n <= DIRECT_MAX {
        direct(buf);
    } else {
        bluestein(buf);
    }
}

/// In-place unnormalised inverse DFT via `conj(forward(conj(x)))`.
fn inverse_unscaled<F: Real>(buf: &mut [Complex<F>]) {
    for v in buf.iter_mut() {
        *v = v.conj();
    }
    forward(buf);
    for v in buf.iter_mut() {
        *v = v.conj();
    }
}

/// Iterative radix-2 decimation-in-time. `buf.len()` must be a power of two.
fn radix2<F: Real>(buf: &mut [Complex<F>]) {
    let n = buf.len();
    debug_assert!(n.is_power_of_two());
    if n <= 1 {
        return;
    }
    // Bit-reversal permutation.
    let bits = n.trailing_zeros();
    for i in 0..n {
        let j = i.reverse_bits() >> (usize::BITS - bits);
        if j > i {
            buf.swap(i, j);
        }
    }
    // Twiddles w[k] = exp(-2 pi i k / n), k < n / 2.
    let tw: Vec<Complex<F>> = (0..n / 2).map(|k| Complex::unit(k, n)).collect();
    let mut len = 2;
    while len <= n {
        let half = len / 2;
        let step = n / len;
        for start in (0..n).step_by(len) {
            for k in 0..half {
                let w = tw[k * step];
                let a = buf[start + k];
                let b = buf[start + k + half] * w;
                buf[start + k] = a + b;
                buf[start + k + half] = a - b;
            }
        }
        len <<= 1;
    }
}

/// Direct O(n^2) DFT with an exact twiddle table (`(j*k) mod n` indexing).
fn direct<F: Real>(buf: &mut [Complex<F>]) {
    let n = buf.len();
    let tw: Vec<Complex<F>> = (0..n).map(|k| Complex::unit(k, n)).collect();
    let src = buf.to_vec();
    for (k, out) in buf.iter_mut().enumerate() {
        let mut acc = Complex::zero();
        for (j, &x) in src.iter().enumerate() {
            acc = acc + x * tw[(j * k) % n];
        }
        *out = acc;
    }
}

/// Bluestein's chirp-z algorithm: a length-n DFT as a circular convolution of
/// power-of-two length `m >= 2n - 1`.
fn bluestein<F: Real>(buf: &mut [Complex<F>]) {
    let n = buf.len();
    let m = (2 * n - 1).next_power_of_two();
    // chirp[k] = exp(-i pi k^2 / n) = exp(-2 pi i (k^2 mod 2n) / (2n)).
    let chirp: Vec<Complex<F>> = (0..n)
        .map(|k| Complex::unit((k * k) % (2 * n), 2 * n))
        .collect();

    let mut a = vec![Complex::zero(); m];
    for k in 0..n {
        a[k] = buf[k] * chirp[k];
    }
    let mut b = vec![Complex::zero(); m];
    b[0] = chirp[0].conj();
    for k in 1..n {
        let c = chirp[k].conj();
        b[k] = c;
        b[m - k] = c;
    }

    radix2(&mut a);
    radix2(&mut b);
    for (x, y) in a.iter_mut().zip(&b) {
        *x = *x * *y;
    }
    // Inverse radix-2 of length m (conjugation trick) with 1/m scaling.
    for v in a.iter_mut() {
        *v = v.conj();
    }
    radix2(&mut a);
    let inv_m = F::from_f64(1.0 / m as f64);
    for k in 0..n {
        buf[k] = (a[k].conj().scale(inv_m)) * chirp[k];
    }
}

// ---------------------------------------------------------------------------------------
// Split-slice helpers
// ---------------------------------------------------------------------------------------

fn pack<F: Real>(re: &[F], im: &[F]) -> Vec<Complex<F>> {
    assert_eq!(
        re.len(),
        im.len(),
        "real and imaginary parts must have the same length"
    );
    re.iter()
        .zip(im)
        .map(|(&r, &i)| Complex::new(r, i))
        .collect()
}

fn unpack<F: Real>(buf: &[Complex<F>]) -> (Vec<F>, Vec<F>) {
    (
        buf.iter().map(|c| c.re).collect(),
        buf.iter().map(|c| c.im).collect(),
    )
}

fn scale_all<F: Real>(buf: &mut [Complex<F>]) {
    if buf.is_empty() {
        return;
    }
    let s = F::from_f64(1.0 / buf.len() as f64);
    for v in buf.iter_mut() {
        *v = v.scale(s);
    }
}

// ---------------------------------------------------------------------------------------
// Public kernels (crate-internal)
// ---------------------------------------------------------------------------------------

/// Forward DFT of a complex signal given as `(re, im)`, like `np.fft.fft`.
pub(crate) fn fft<F: Real>(re: &[F], im: &[F]) -> (Vec<F>, Vec<F>) {
    let mut buf = pack(re, im);
    forward(&mut buf);
    unpack(&buf)
}

/// Inverse DFT with `1/n` scaling, like `np.fft.ifft`.
pub(crate) fn ifft<F: Real>(re: &[F], im: &[F]) -> (Vec<F>, Vec<F>) {
    let mut buf = pack(re, im);
    inverse_unscaled(&mut buf);
    scale_all(&mut buf);
    unpack(&buf)
}

/// Forward DFT of a real signal; returns the `n / 2 + 1` non-redundant bins, like `np.fft.rfft`.
pub(crate) fn rfft<F: Real>(x: &[F]) -> (Vec<F>, Vec<F>) {
    let n = x.len();
    let mut buf: Vec<Complex<F>> = x.iter().map(|&v| Complex::new(v, F::ZERO)).collect();
    forward(&mut buf);
    buf.truncate(n / 2 + 1);
    unpack(&buf)
}

/// Inverse of [`rfft`]: `m` Hermitian half-spectrum bins to a real signal of length `n`,
/// like `np.fft.irfft(a, n)`. The half-spectrum is cropped or zero-padded to `n / 2 + 1`
/// bins; the imaginary parts of the DC (and Nyquist, for even `n`) bins are ignored.
pub(crate) fn irfft<F: Real>(re: &[F], im: &[F], n: usize) -> Vec<F> {
    assert_eq!(
        re.len(),
        im.len(),
        "real and imaginary parts must have the same length"
    );
    assert!(n >= 1, "Invalid number of FFT data points ({n}) specified.");
    let half = n / 2 + 1;
    let mut full = vec![Complex::<F>::zero(); n];
    for k in 0..half.min(re.len()) {
        full[k] = Complex::new(re[k], im[k]);
    }
    full[0].im = F::ZERO;
    if n.is_multiple_of(2) {
        full[n / 2].im = F::ZERO;
    }
    for k in 1..n.div_ceil(2) {
        full[n - k] = full[k].conj();
    }
    inverse_unscaled(&mut full);
    let s = F::from_f64(1.0 / n as f64);
    full.iter().map(|c| c.re * s).collect()
}

/// 2-D forward DFT of a row-major `rows x cols` complex array, like `np.fft.fft2`.
pub(crate) fn fft2<F: Real>(re: &[F], im: &[F], rows: usize, cols: usize) -> (Vec<F>, Vec<F>) {
    let mut buf = pack(re, im);
    assert_eq!(buf.len(), rows * cols, "shape does not match data length");
    transform_2d(&mut buf, rows, cols, false);
    unpack(&buf)
}

/// 2-D inverse DFT (scaled by `1 / (rows * cols)`), like `np.fft.ifft2`.
pub(crate) fn ifft2<F: Real>(re: &[F], im: &[F], rows: usize, cols: usize) -> (Vec<F>, Vec<F>) {
    let mut buf = pack(re, im);
    assert_eq!(buf.len(), rows * cols, "shape does not match data length");
    transform_2d(&mut buf, rows, cols, true);
    unpack(&buf)
}

/// Row pass then column pass. The inverse scales each pass by its own length.
fn transform_2d<F: Real>(buf: &mut [Complex<F>], rows: usize, cols: usize, inv: bool) {
    let run = |line: &mut [Complex<F>]| {
        if inv {
            inverse_unscaled(line);
            scale_all(line);
        } else {
            forward(line);
        }
    };
    if cols > 0 {
        for row in buf.chunks_mut(cols) {
            run(row);
        }
    }
    let mut col = vec![Complex::zero(); rows];
    for c in 0..cols {
        for r in 0..rows {
            col[r] = buf[r * cols + c];
        }
        run(&mut col);
        for r in 0..rows {
            buf[r * cols + c] = col[r];
        }
    }
}

/// Sample frequencies of an `n`-point DFT with sample spacing `d`, like `np.fft.fftfreq(n, d)`.
pub(crate) fn fftfreq<F: Real>(n: usize, d: F) -> Vec<F> {
    assert!(n > 0, "n should be an integer greater than 0");
    let val = F::from_usize(1) / (F::from_usize(n) * d);
    let pos = (n - 1) / 2 + 1;
    let mut out = Vec::with_capacity(n);
    for i in 0..pos {
        out.push(F::from_usize(i) * val);
    }
    for i in (1..=n / 2).rev() {
        out.push(-F::from_usize(i) * val);
    }
    out
}

/// Sample frequencies for `rfft` of an `n`-point signal, like `np.fft.rfftfreq(n, d)`.
pub(crate) fn rfftfreq<F: Real>(n: usize, d: F) -> Vec<F> {
    assert!(n > 0, "n should be an integer greater than 0");
    let val = F::from_usize(1) / (F::from_usize(n) * d);
    (0..=n / 2).map(|i| F::from_usize(i) * val).collect()
}

/// Move the zero-frequency bin to the centre on every axis of a row-major array of
/// `shape`, like `np.fft.fftshift(x)` (all axes). `inverse = true` gives `ifftshift`.
pub(crate) fn shift_nd<T: Copy>(data: &[T], shape: &[usize], inverse: bool) -> Vec<T> {
    let size: usize = shape.iter().product();
    assert_eq!(data.len(), size, "shape does not match data length");
    if size == 0 {
        return Vec::new();
    }
    // Destination offset along an axis of length n: fftshift rolls by n/2, ifftshift by -(n/2).
    let roll = |n: usize| if inverse { n - n / 2 } else { n / 2 };
    let nd = shape.len();
    let mut strides = vec![1usize; nd];
    for ax in (0..nd.saturating_sub(1)).rev() {
        strides[ax] = strides[ax + 1] * shape[ax + 1];
    }
    let mut out = data.to_vec();
    let mut idx = vec![0usize; nd];
    for &v in data {
        let dst: usize = (0..nd)
            .map(|ax| ((idx[ax] + roll(shape[ax])) % shape[ax]) * strides[ax])
            .sum();
        out[dst] = v;
        for ax in (0..nd).rev() {
            idx[ax] += 1;
            if idx[ax] < shape[ax] {
                break;
            }
            idx[ax] = 0;
        }
    }
    out
}

/// 1-D `np.fft.fftshift`.
#[cfg(test)]
pub(crate) fn fftshift<T: Copy>(x: &[T]) -> Vec<T> {
    shift_nd(x, &[x.len()], false)
}

/// 1-D `np.fft.ifftshift`.
#[cfg(test)]
pub(crate) fn ifftshift<T: Copy>(x: &[T]) -> Vec<T> {
    shift_nd(x, &[x.len()], true)
}

/// Crop or zero-pad a complex signal to length `n`, as NumPy does for the `n` argument.
#[cfg(test)]
pub(crate) fn resize<F: Real>(re: &[F], im: &[F], n: usize) -> (Vec<F>, Vec<F>) {
    let mut r = re.to_vec();
    let mut i = im.to_vec();
    r.resize(n, F::ZERO);
    i.resize(n, F::ZERO);
    (r, i)
}