tenflowers-core 0.2.0

Core tensor operations and execution engine for TenfloweRS
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
//! In-place FFT operations
//!
//! This module provides in-place FFT implementations that modify the input tensor
//! directly. Each function calls the real oxifft-backed computation and writes the
//! result back into `*input`, so callers always see a transformed tensor.

use crate::tensor::TensorStorage;
use crate::{Result, Tensor, TensorError};
use num_complex::Complex;
use oxifft::{Direction, Flags, Plan};
use scirs2_core::ndarray::{ArrayD, IxDyn};
use scirs2_core::numeric::{Float, FromPrimitive, Signed, Zero};
use std::fmt::Debug;

use super::fft1d::ifft;
use super::fft2d::ifft2;
use super::fft3d::ifft3;

// ---------------------------------------------------------------------------
// Memory-layout helpers (identical repr(C) trick used in fft1d/fft2d/fft3d)
// ---------------------------------------------------------------------------

#[inline]
fn to_oxifft_complex<T: oxifft::Float>(data: &[Complex<T>]) -> &[oxifft::kernel::Complex<T>] {
    // Safety: Both num_complex::Complex and oxifft::Complex have #[repr(C)] layout
    // with identical memory representation (re: T, im: T).
    unsafe {
        std::slice::from_raw_parts(
            data.as_ptr() as *const oxifft::kernel::Complex<T>,
            data.len(),
        )
    }
}

#[inline]
fn to_oxifft_complex_mut<T: oxifft::Float>(
    data: &mut [Complex<T>],
) -> &mut [oxifft::kernel::Complex<T>] {
    // Safety: same repr(C) guarantee as above.
    unsafe {
        std::slice::from_raw_parts_mut(
            data.as_mut_ptr() as *mut oxifft::kernel::Complex<T>,
            data.len(),
        )
    }
}

// ---------------------------------------------------------------------------
// 1D in-place FFT
// ---------------------------------------------------------------------------

/// In-place 1D forward FFT along the last axis (complex → complex).
///
/// Applies a complex-to-complex DFT in the forward direction (`e^{-2πi·k·n/N}`).
/// The input tensor is replaced with its frequency-domain representation.
pub fn fft_inplace<T>(input: &mut Tensor<Complex<T>>) -> Result<()>
where
    T: Float
        + Send
        + Sync
        + 'static
        + FromPrimitive
        + Signed
        + Debug
        + Default
        + bytemuck::Pod
        + bytemuck::Zeroable
        + oxifft::Float,
    Complex<T>: Default,
{
    match &input.storage {
        TensorStorage::Cpu(arr) => {
            let shape = arr.shape();
            let ndim = shape.len();

            if ndim == 0 {
                return Err(TensorError::InvalidShape {
                    operation: "fft_inplace".to_string(),
                    reason: "FFT requires at least 1D input".to_string(),
                    shape: Some(shape.to_vec()),
                    context: None,
                });
            }

            let n = shape[ndim - 1];
            let total_elements: usize = shape.iter().product();
            let num_ffts = total_elements / n;

            let plan = Plan::dft_1d(n, Direction::Forward, Flags::ESTIMATE).ok_or_else(|| {
                TensorError::InvalidShape {
                    operation: "fft_inplace".to_string(),
                    reason: "Failed to create FFT plan".to_string(),
                    shape: Some(shape.to_vec()),
                    context: None,
                }
            })?;

            let input_slice = arr.as_slice().ok_or_else(|| {
                TensorError::unsupported_operation_simple(
                    "fft_inplace: input array is not contiguous".to_string(),
                )
            })?;

            let mut output_data = vec![Complex::zero(); total_elements];

            for i in 0..num_ffts {
                let start = i * n;
                let end = start + n;
                let row_in = input_slice[start..end].to_vec();
                let mut row_out = vec![Complex::zero(); n];
                plan.execute(
                    to_oxifft_complex(&row_in),
                    to_oxifft_complex_mut(&mut row_out),
                );
                output_data[start..end].copy_from_slice(&row_out);
            }

            let owned_shape = shape.to_vec();
            let output_array =
                ArrayD::from_shape_vec(IxDyn(&owned_shape), output_data).map_err(|e| {
                    TensorError::InvalidShape {
                        operation: "fft_inplace".to_string(),
                        reason: e.to_string(),
                        shape: None,
                        context: None,
                    }
                })?;

            *input = Tensor::from_array(output_array);
            Ok(())
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(_) => {
            let mut cpu_complex = input.to_cpu()?;
            fft_inplace(&mut cpu_complex)?;
            *input = cpu_complex;
            Ok(())
        }
    }
}

/// In-place 1D inverse FFT along the last axis (complex → complex).
///
/// Applies the normalised inverse DFT (`1/N · Σ X[k] e^{+2πi·k·n/N}`).
/// The input tensor is replaced with its time-domain reconstruction.
pub fn ifft_inplace<T>(input: &mut Tensor<Complex<T>>) -> Result<()>
where
    T: Float
        + Send
        + Sync
        + 'static
        + FromPrimitive
        + Signed
        + Debug
        + Default
        + bytemuck::Pod
        + bytemuck::Zeroable
        + oxifft::Float,
    Complex<T>: Default,
{
    let result = ifft(input as &Tensor<Complex<T>>)?;
    *input = result;
    Ok(())
}

// ---------------------------------------------------------------------------
// 2D in-place FFT
// ---------------------------------------------------------------------------

/// In-place 2D forward FFT along the last two axes (complex → complex).
///
/// Applies a separable 2D complex-to-complex DFT: first across rows (width),
/// then across columns (height).
pub fn fft2_inplace<T>(input: &mut Tensor<Complex<T>>) -> Result<()>
where
    T: Float
        + Send
        + Sync
        + 'static
        + FromPrimitive
        + Signed
        + Debug
        + Default
        + bytemuck::Pod
        + bytemuck::Zeroable
        + oxifft::Float,
    Complex<T>: Default,
{
    match &input.storage {
        TensorStorage::Cpu(arr) => {
            let shape = arr.shape();
            let ndim = shape.len();

            if ndim < 2 {
                return Err(TensorError::InvalidShape {
                    operation: "fft2_inplace".to_string(),
                    reason: "FFT2 requires at least 2D input".to_string(),
                    shape: Some(shape.to_vec()),
                    context: None,
                });
            }

            let height = shape[ndim - 2];
            let width = shape[ndim - 1];
            let total_elements: usize = shape.iter().product();
            let elements_per_slice = height * width;
            let num_slices = total_elements / elements_per_slice;

            let fft_width =
                Plan::dft_1d(width, Direction::Forward, Flags::ESTIMATE).ok_or_else(|| {
                    TensorError::InvalidShape {
                        operation: "fft2_inplace".to_string(),
                        reason: "Failed to create width FFT plan".to_string(),
                        shape: Some(shape.to_vec()),
                        context: None,
                    }
                })?;
            let fft_height =
                Plan::dft_1d(height, Direction::Forward, Flags::ESTIMATE).ok_or_else(|| {
                    TensorError::InvalidShape {
                        operation: "fft2_inplace".to_string(),
                        reason: "Failed to create height FFT plan".to_string(),
                        shape: Some(shape.to_vec()),
                        context: None,
                    }
                })?;

            let input_slice = arr.as_slice().ok_or_else(|| {
                TensorError::unsupported_operation_simple(
                    "fft2_inplace: input array is not contiguous".to_string(),
                )
            })?;

            let mut output_data = vec![Complex::zero(); total_elements];

            for slice_idx in 0..num_slices {
                let slice_start = slice_idx * elements_per_slice;
                let mut slice_buf: Vec<Complex<T>> =
                    input_slice[slice_start..slice_start + elements_per_slice].to_vec();

                // Forward FFT along rows (width dimension)
                for row in 0..height {
                    let row_start = row * width;
                    let row_end = row_start + width;
                    let row_in = slice_buf[row_start..row_end].to_vec();
                    let mut row_out = vec![Complex::zero(); width];
                    fft_width.execute(
                        to_oxifft_complex(&row_in),
                        to_oxifft_complex_mut(&mut row_out),
                    );
                    slice_buf[row_start..row_end].copy_from_slice(&row_out);
                }

                // Forward FFT along columns (height dimension)
                for col in 0..width {
                    let col_in: Vec<Complex<T>> = (0..height)
                        .map(|row| slice_buf[row * width + col])
                        .collect();
                    let mut col_out = vec![Complex::zero(); height];
                    fft_height.execute(
                        to_oxifft_complex(&col_in),
                        to_oxifft_complex_mut(&mut col_out),
                    );
                    for (row, &val) in col_out.iter().enumerate() {
                        slice_buf[row * width + col] = val;
                    }
                }

                output_data[slice_start..slice_start + elements_per_slice]
                    .copy_from_slice(&slice_buf);
            }

            let owned_shape = shape.to_vec();
            let output_array =
                ArrayD::from_shape_vec(IxDyn(&owned_shape), output_data).map_err(|e| {
                    TensorError::InvalidShape {
                        operation: "fft2_inplace".to_string(),
                        reason: e.to_string(),
                        shape: None,
                        context: None,
                    }
                })?;

            *input = Tensor::from_array(output_array);
            Ok(())
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(_) => {
            let mut cpu_complex = input.to_cpu()?;
            fft2_inplace(&mut cpu_complex)?;
            *input = cpu_complex;
            Ok(())
        }
    }
}

/// In-place 2D inverse FFT along the last two axes (complex → complex).
///
/// Applies the separable normalised inverse 2D DFT.
pub fn ifft2_inplace<T>(input: &mut Tensor<Complex<T>>) -> Result<()>
where
    T: Float
        + Send
        + Sync
        + 'static
        + FromPrimitive
        + Signed
        + Debug
        + Default
        + bytemuck::Pod
        + bytemuck::Zeroable
        + oxifft::Float,
    Complex<T>: Default,
{
    let result = ifft2(input as &Tensor<Complex<T>>)?;
    *input = result;
    Ok(())
}

// ---------------------------------------------------------------------------
// 3D in-place FFT
// ---------------------------------------------------------------------------

/// In-place 3D forward FFT along the last three axes (complex → complex).
///
/// Applies a separable 3D complex-to-complex DFT: first along width, then
/// height, then depth.
pub fn fft3_inplace<T>(input: &mut Tensor<Complex<T>>) -> Result<()>
where
    T: Float
        + Send
        + Sync
        + 'static
        + FromPrimitive
        + Signed
        + Debug
        + Default
        + bytemuck::Pod
        + bytemuck::Zeroable
        + oxifft::Float,
    Complex<T>: Default,
{
    match &input.storage {
        TensorStorage::Cpu(arr) => {
            let shape = arr.shape();
            let ndim = shape.len();

            if ndim < 3 {
                return Err(TensorError::InvalidShape {
                    operation: "fft3_inplace".to_string(),
                    reason: "FFT3 requires at least 3D input".to_string(),
                    shape: Some(shape.to_vec()),
                    context: None,
                });
            }

            let depth = shape[ndim - 3];
            let height = shape[ndim - 2];
            let width = shape[ndim - 1];
            let total_elements: usize = shape.iter().product();
            let elements_per_volume = depth * height * width;
            let num_volumes = total_elements / elements_per_volume;

            let fft_width =
                Plan::dft_1d(width, Direction::Forward, Flags::ESTIMATE).ok_or_else(|| {
                    TensorError::InvalidShape {
                        operation: "fft3_inplace".to_string(),
                        reason: "Failed to create width FFT plan".to_string(),
                        shape: Some(shape.to_vec()),
                        context: None,
                    }
                })?;
            let fft_height =
                Plan::dft_1d(height, Direction::Forward, Flags::ESTIMATE).ok_or_else(|| {
                    TensorError::InvalidShape {
                        operation: "fft3_inplace".to_string(),
                        reason: "Failed to create height FFT plan".to_string(),
                        shape: Some(shape.to_vec()),
                        context: None,
                    }
                })?;
            let fft_depth =
                Plan::dft_1d(depth, Direction::Forward, Flags::ESTIMATE).ok_or_else(|| {
                    TensorError::InvalidShape {
                        operation: "fft3_inplace".to_string(),
                        reason: "Failed to create depth FFT plan".to_string(),
                        shape: Some(shape.to_vec()),
                        context: None,
                    }
                })?;

            let input_slice = arr.as_slice().ok_or_else(|| {
                TensorError::unsupported_operation_simple(
                    "fft3_inplace: input array is not contiguous".to_string(),
                )
            })?;

            let mut output_data = vec![Complex::zero(); total_elements];

            for volume_idx in 0..num_volumes {
                let volume_start = volume_idx * elements_per_volume;
                let mut vol: Vec<Complex<T>> =
                    input_slice[volume_start..volume_start + elements_per_volume].to_vec();

                // Forward FFT along width (last dimension)
                for d in 0..depth {
                    for h in 0..height {
                        let row_start = (d * height + h) * width;
                        let row_end = row_start + width;
                        let row_in = vol[row_start..row_end].to_vec();
                        let mut row_out = vec![Complex::zero(); width];
                        fft_width.execute(
                            to_oxifft_complex(&row_in),
                            to_oxifft_complex_mut(&mut row_out),
                        );
                        vol[row_start..row_end].copy_from_slice(&row_out);
                    }
                }

                // Forward FFT along height (second-to-last dimension)
                for d in 0..depth {
                    for w in 0..width {
                        let col_in: Vec<Complex<T>> = (0..height)
                            .map(|h| vol[(d * height + h) * width + w])
                            .collect();
                        let mut col_out = vec![Complex::zero(); height];
                        fft_height.execute(
                            to_oxifft_complex(&col_in),
                            to_oxifft_complex_mut(&mut col_out),
                        );
                        for (h, &val) in col_out.iter().enumerate() {
                            vol[(d * height + h) * width + w] = val;
                        }
                    }
                }

                // Forward FFT along depth (third-to-last dimension)
                for h in 0..height {
                    for w in 0..width {
                        let depth_in: Vec<Complex<T>> = (0..depth)
                            .map(|d| vol[(d * height + h) * width + w])
                            .collect();
                        let mut depth_out = vec![Complex::zero(); depth];
                        fft_depth.execute(
                            to_oxifft_complex(&depth_in),
                            to_oxifft_complex_mut(&mut depth_out),
                        );
                        for (d, &val) in depth_out.iter().enumerate() {
                            vol[(d * height + h) * width + w] = val;
                        }
                    }
                }

                output_data[volume_start..volume_start + elements_per_volume].copy_from_slice(&vol);
            }

            let owned_shape = shape.to_vec();
            let output_array =
                ArrayD::from_shape_vec(IxDyn(&owned_shape), output_data).map_err(|e| {
                    TensorError::InvalidShape {
                        operation: "fft3_inplace".to_string(),
                        reason: e.to_string(),
                        shape: None,
                        context: None,
                    }
                })?;

            *input = Tensor::from_array(output_array);
            Ok(())
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(_) => {
            let mut cpu_complex = input.to_cpu()?;
            fft3_inplace(&mut cpu_complex)?;
            *input = cpu_complex;
            Ok(())
        }
    }
}

/// In-place 3D inverse FFT along the last three axes (complex → complex).
///
/// Applies the separable normalised inverse 3D DFT.
pub fn ifft3_inplace<T>(input: &mut Tensor<Complex<T>>) -> Result<()>
where
    T: Float
        + Send
        + Sync
        + 'static
        + FromPrimitive
        + Signed
        + Debug
        + Default
        + bytemuck::Pod
        + bytemuck::Zeroable
        + oxifft::Float,
    Complex<T>: Default,
{
    let result = ifft3(input as &Tensor<Complex<T>>)?;
    *input = result;
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::{ArrayD, IxDyn};

    fn make_complex_tensor(data: Vec<Complex<f32>>, shape: &[usize]) -> Tensor<Complex<f32>> {
        let arr = ArrayD::from_shape_vec(IxDyn(shape), data).expect("shape must match data len");
        Tensor::from_array(arr)
    }

    /// Round-trip: fft_inplace followed by ifft_inplace must recover the
    /// original signal to within floating-point tolerance.
    #[test]
    fn test_fft_ifft_1d_roundtrip() {
        let signal: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0];
        let n = signal.len();
        let data: Vec<Complex<f32>> = signal.iter().map(|&x| Complex::new(x, 0.0)).collect();

        let mut tensor = make_complex_tensor(data.clone(), &[n]);

        fft_inplace(&mut tensor).expect("fft_inplace must not fail");
        // Verify tensor has actually changed (not a no-op)
        let after_fft = tensor.data().to_vec();
        let unchanged = after_fft
            .iter()
            .zip(data.iter())
            .all(|(a, b)| (a.re - b.re).abs() < 1e-6 && (a.im - b.im).abs() < 1e-6);
        assert!(!unchanged, "fft_inplace must change the tensor data");

        ifft_inplace(&mut tensor).expect("ifft_inplace must not fail");

        let result = tensor.data().to_vec();
        for (i, (got, &orig)) in result.iter().zip(signal.iter()).enumerate() {
            assert!(
                (got.re - orig).abs() < 1e-4,
                "element[{i}]: re = {:.6}, expected {:.6}",
                got.re,
                orig,
            );
            assert!(
                got.im.abs() < 1e-4,
                "element[{i}]: im = {:.6}, expected ~0",
                got.im,
            );
        }
    }

    /// 2D round-trip: fft2_inplace then ifft2_inplace.
    #[test]
    fn test_fft2_ifft2_2d_roundtrip() {
        // 2×4 complex tensor
        let signal: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let shape = [2usize, 4];
        let data: Vec<Complex<f32>> = signal.iter().map(|&x| Complex::new(x, 0.0)).collect();

        let mut tensor = make_complex_tensor(data, &shape);

        fft2_inplace(&mut tensor).expect("fft2_inplace must not fail");
        ifft2_inplace(&mut tensor).expect("ifft2_inplace must not fail");

        let result = tensor.data().to_vec();
        for (i, (got, &orig)) in result.iter().zip(signal.iter()).enumerate() {
            assert!(
                (got.re - orig).abs() < 1e-4,
                "element[{i}]: re = {:.6}, expected {:.6}",
                got.re,
                orig,
            );
            assert!(
                got.im.abs() < 1e-4,
                "element[{i}]: im = {:.6}, expected ~0",
                got.im,
            );
        }
    }

    /// 3D round-trip: fft3_inplace then ifft3_inplace.
    #[test]
    fn test_fft3_ifft3_3d_roundtrip() {
        // 2×2×4 complex tensor
        let signal: Vec<f32> = (1..=16).map(|x| x as f32).collect();
        let shape = [2usize, 2, 4];
        let data: Vec<Complex<f32>> = signal.iter().map(|&x| Complex::new(x, 0.0)).collect();

        let mut tensor = make_complex_tensor(data, &shape);

        fft3_inplace(&mut tensor).expect("fft3_inplace must not fail");
        ifft3_inplace(&mut tensor).expect("ifft3_inplace must not fail");

        let result = tensor.data().to_vec();
        for (i, (got, &orig)) in result.iter().zip(signal.iter()).enumerate() {
            assert!(
                (got.re - orig).abs() < 1e-4,
                "element[{i}]: re = {:.6}, expected {:.6}",
                got.re,
                orig,
            );
            assert!(
                got.im.abs() < 1e-4,
                "element[{i}]: im = {:.6}, expected ~0",
                got.im,
            );
        }
    }
}