torsh-functional 0.2.0

Functional programming utilities for ToRSh tensors
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
613
614
615
616
617
618
619
620
621
//! Interpolation functions for tensor operations
//!
//! This module provides various interpolation methods commonly used in
//! signal processing, image processing, and scientific computing.

use torsh_core::{Result as TorshResult, TorshError};
use torsh_tensor::Tensor;

/// Interpolation methods supported.
///
/// This is the single interpolation mode of the crate: image resizing
/// ([`crate::image::resize`]), grid sampling and the numeric interpolators all
/// take these variants, so a mode chosen for one of them means the same thing in
/// the others. Not every mode is meaningful everywhere — a function that cannot
/// honour a mode returns an error naming it instead of silently degrading to a
/// cheaper kernel.
///
/// `Bilinear`/`Bicubic` are the 2-D spellings of `Linear`/`Cubic` and are treated
/// as their synonyms; both spellings are accepted for PyTorch familiarity.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InterpolationMode {
    /// Linear interpolation
    Linear,
    /// Bilinear interpolation (2-D spelling of [`InterpolationMode::Linear`])
    Bilinear,
    /// Nearest neighbor interpolation
    Nearest,
    /// Cubic interpolation
    Cubic,
    /// Bicubic interpolation (2-D spelling of [`InterpolationMode::Cubic`])
    Bicubic,
    /// Area (box) averaging, as used for image downsampling
    Area,
    /// Spline interpolation
    Spline,
    /// Lanczos interpolation
    Lanczos,
}

impl InterpolationMode {
    /// Whether this mode is a linear kernel (`Linear` or its 2-D spelling).
    pub fn is_linear(self) -> bool {
        matches!(self, Self::Linear | Self::Bilinear)
    }

    /// Whether this mode is a cubic kernel (`Cubic` or its 2-D spelling).
    pub fn is_cubic(self) -> bool {
        matches!(self, Self::Cubic | Self::Bicubic)
    }
}

/// 1D linear interpolation
///
/// Interpolates values at specified points using linear interpolation
pub fn interp1d(x: &Tensor, y: &Tensor, x_new: &Tensor, extrapolate: bool) -> TorshResult<Tensor> {
    let x_data = x.data()?;
    let y_data = y.data()?;
    let x_new_data = x_new.data()?;

    if x_data.len() != y_data.len() {
        return Err(TorshError::InvalidArgument(
            "x and y arrays must have the same length".to_string(),
        ));
    }

    if x_data.len() < 2 {
        return Err(TorshError::InvalidArgument(
            "At least 2 data points required for interpolation".to_string(),
        ));
    }

    let mut result = Vec::with_capacity(x_new_data.len());

    for &x_val in x_new_data.iter() {
        let y_val = linear_interpolate(&x_data, &y_data, x_val, extrapolate)?;
        result.push(y_val);
    }

    Tensor::from_data(result, x_new.shape().dims().to_vec(), x_new.device())
}

/// 1D cubic spline interpolation
///
/// Performs cubic spline interpolation with natural boundary conditions
pub fn spline1d(x: &Tensor, y: &Tensor, x_new: &Tensor, extrapolate: bool) -> TorshResult<Tensor> {
    let x_data = x.data()?;
    let y_data = y.data()?;
    let x_new_data = x_new.data()?;

    if x_data.len() != y_data.len() {
        return Err(TorshError::InvalidArgument(
            "x and y arrays must have the same length".to_string(),
        ));
    }

    if x_data.len() < 4 {
        return Err(TorshError::InvalidArgument(
            "At least 4 data points required for cubic spline interpolation".to_string(),
        ));
    }

    // Compute spline coefficients
    let spline_coeffs = compute_cubic_spline_coefficients(&x_data, &y_data)?;

    let mut result = Vec::with_capacity(x_new_data.len());

    for &x_val in x_new_data.iter() {
        let y_val = evaluate_cubic_spline(&x_data, &y_data, &spline_coeffs, x_val, extrapolate)?;
        result.push(y_val);
    }

    Tensor::from_data(result, x_new.shape().dims().to_vec(), x_new.device())
}

/// 2D bilinear interpolation
///
/// Performs bilinear interpolation on a 2D grid
pub fn interp2d(
    input: &Tensor,
    x_coords: &Tensor,
    y_coords: &Tensor,
    mode: InterpolationMode,
) -> TorshResult<Tensor> {
    let input_shape = input.shape();
    if input_shape.ndim() != 2 {
        return Err(TorshError::InvalidArgument(
            "Input must be a 2D tensor".to_string(),
        ));
    }

    if x_coords.shape() != y_coords.shape() {
        return Err(TorshError::ShapeMismatch {
            expected: x_coords.shape().dims().to_vec(),
            got: y_coords.shape().dims().to_vec(),
        });
    }

    let (height, width) = (input_shape.dims()[0], input_shape.dims()[1]);
    let input_data = input.data()?;
    let x_coords_data = x_coords.data()?;
    let y_coords_data = y_coords.data()?;

    let mut result = Vec::with_capacity(x_coords_data.len());

    for (_i, (&x, &y)) in x_coords_data.iter().zip(y_coords_data.iter()).enumerate() {
        let val = match mode {
            InterpolationMode::Linear | InterpolationMode::Bilinear => {
                bilinear_sample(&input_data, width, height, x, y)
            }
            InterpolationMode::Nearest => nearest_sample(&input_data, width, height, x, y),
            InterpolationMode::Cubic | InterpolationMode::Bicubic => {
                bicubic_sample(&input_data, width, height, x, y)
            }
            InterpolationMode::Area | InterpolationMode::Spline | InterpolationMode::Lanczos => {
                return Err(TorshError::UnsupportedOperation {
                    op: format!("{:?}", mode),
                    dtype: "2D interpolation".to_string(),
                });
            }
        };
        result.push(val);
    }

    Tensor::from_data(result, x_coords.shape().dims().to_vec(), x_coords.device())
}

/// Grid sample function (similar to PyTorch's grid_sample)
///
/// Samples input using the sampling grid specified by coordinates
pub fn grid_sample(
    input: &Tensor,
    grid: &Tensor,
    mode: InterpolationMode,
    _padding_mode: &str,
    align_corners: bool,
) -> TorshResult<Tensor> {
    let input_shape = input.shape();
    let grid_shape = grid.shape();

    // Expect input: [N, C, H, W] and grid: [N, H_out, W_out, 2]
    if input_shape.ndim() != 4 || grid_shape.ndim() != 4 || grid_shape.dims()[3] != 2 {
        return Err(TorshError::InvalidArgument(
            "Expected input [N,C,H,W] and grid [N,H_out,W_out,2]".to_string(),
        ));
    }

    let (batch_size, channels, in_height, in_width) = (
        input_shape.dims()[0],
        input_shape.dims()[1],
        input_shape.dims()[2],
        input_shape.dims()[3],
    );

    let (out_height, out_width) = (grid_shape.dims()[1], grid_shape.dims()[2]);

    let input_data = input.data()?;
    let grid_data = grid.data()?;

    let mut result = vec![0.0; batch_size * channels * out_height * out_width];

    for n in 0..batch_size {
        for c in 0..channels {
            for h in 0..out_height {
                for w in 0..out_width {
                    let grid_idx = ((n * out_height + h) * out_width + w) * 2;
                    let x = grid_data[grid_idx];
                    let y = grid_data[grid_idx + 1];

                    // Convert from [-1, 1] to pixel coordinates
                    let (pixel_x, pixel_y) = if align_corners {
                        (
                            (x + 1.0) * (in_width - 1) as f32 / 2.0,
                            (y + 1.0) * (in_height - 1) as f32 / 2.0,
                        )
                    } else {
                        (
                            (x + 1.0) * in_width as f32 / 2.0 - 0.5,
                            (y + 1.0) * in_height as f32 / 2.0 - 0.5,
                        )
                    };

                    // Sample from input
                    let channel_offset = (n * channels + c) * in_height * in_width;
                    let input_slice =
                        &input_data[channel_offset..channel_offset + in_height * in_width];

                    let sampled_value = match mode {
                        InterpolationMode::Linear | InterpolationMode::Bilinear => {
                            bilinear_sample(input_slice, in_width, in_height, pixel_x, pixel_y)
                        }
                        InterpolationMode::Nearest => {
                            nearest_sample(input_slice, in_width, in_height, pixel_x, pixel_y)
                        }
                        InterpolationMode::Cubic | InterpolationMode::Bicubic => {
                            bicubic_sample(input_slice, in_width, in_height, pixel_x, pixel_y)
                        }
                        InterpolationMode::Area
                        | InterpolationMode::Spline
                        | InterpolationMode::Lanczos => {
                            // Silently returning zeros would look like a black image;
                            // report the unsupported mode instead.
                            return Err(TorshError::UnsupportedOperation {
                                op: format!("{:?}", mode),
                                dtype: "grid_sample".to_string(),
                            });
                        }
                    };

                    let out_idx = ((n * channels + c) * out_height + h) * out_width + w;
                    result[out_idx] = sampled_value;
                }
            }
        }
    }

    Tensor::from_data(
        result,
        vec![batch_size, channels, out_height, out_width],
        input.device(),
    )
}

/// Lanczos interpolation
///
/// High-quality interpolation using Lanczos kernel
pub fn lanczos_interp1d(
    x: &Tensor,
    y: &Tensor,
    x_new: &Tensor,
    a: usize, // Lanczos parameter (typically 2 or 3)
) -> TorshResult<Tensor> {
    let x_data = x.data()?;
    let y_data = y.data()?;
    let x_new_data = x_new.data()?;

    if x_data.len() != y_data.len() {
        return Err(TorshError::InvalidArgument(
            "x and y arrays must have the same length".to_string(),
        ));
    }

    let mut result = Vec::with_capacity(x_new_data.len());

    for &x_val in x_new_data.iter() {
        let y_val = lanczos_interpolate(&x_data, &y_data, x_val, a)?;
        result.push(y_val);
    }

    Tensor::from_data(result, x_new.shape().dims().to_vec(), x_new.device())
}

/// Barycentric interpolation
///
/// Efficient method for polynomial interpolation
pub fn barycentric_interp(x: &Tensor, y: &Tensor, x_new: &Tensor) -> TorshResult<Tensor> {
    let x_data = x.data()?;
    let y_data = y.data()?;
    let x_new_data = x_new.data()?;

    if x_data.len() != y_data.len() {
        return Err(TorshError::InvalidArgument(
            "x and y arrays must have the same length".to_string(),
        ));
    }

    // Compute barycentric weights
    let weights = compute_barycentric_weights(&x_data)?;

    let mut result = Vec::with_capacity(x_new_data.len());

    for &x_val in x_new_data.iter() {
        let y_val = evaluate_barycentric(&x_data, &y_data, &weights, x_val)?;
        result.push(y_val);
    }

    Tensor::from_data(result, x_new.shape().dims().to_vec(), x_new.device())
}

// Helper functions

fn linear_interpolate(
    x_data: &[f32],
    y_data: &[f32],
    x_val: f32,
    extrapolate: bool,
) -> TorshResult<f32> {
    let n = x_data.len();

    // Find surrounding points
    let mut i = 0;
    while i < n - 1 && x_data[i + 1] < x_val {
        i += 1;
    }

    // Handle extrapolation
    if !extrapolate {
        if x_val < x_data[0] || x_val > x_data[n - 1] {
            return Err(TorshError::InvalidArgument(
                "Value outside interpolation range and extrapolation disabled".to_string(),
            ));
        }
    }

    if i == n - 1 {
        return Ok(y_data[n - 1]);
    }

    if x_data[i] == x_data[i + 1] {
        return Ok(y_data[i]);
    }

    let t = (x_val - x_data[i]) / (x_data[i + 1] - x_data[i]);
    Ok(y_data[i] * (1.0 - t) + y_data[i + 1] * t)
}

fn compute_cubic_spline_coefficients(x_data: &[f32], y_data: &[f32]) -> TorshResult<Vec<f32>> {
    let n = x_data.len();
    let mut a = vec![0.0; n];
    let mut b = vec![0.0; n];
    let _c = vec![0.0; n];
    let _d = vec![0.0; n];

    // Natural spline boundary conditions
    // This is a simplified implementation - in practice, you'd use a more robust solver
    for i in 0..n {
        a[i] = y_data[i];
    }

    // For now, use simple finite differences for derivatives
    for i in 1..n - 1 {
        let h1 = x_data[i] - x_data[i - 1];
        let h2 = x_data[i + 1] - x_data[i];
        let delta1 = (y_data[i] - y_data[i - 1]) / h1;
        let delta2 = (y_data[i + 1] - y_data[i]) / h2;

        b[i] = (delta1 + delta2) / 2.0;
    }

    Ok(b) // Return second derivatives as a simple approximation
}

fn evaluate_cubic_spline(
    x_data: &[f32],
    y_data: &[f32],
    _coeffs: &[f32],
    x_val: f32,
    extrapolate: bool,
) -> TorshResult<f32> {
    // For now, fall back to linear interpolation
    // A full cubic spline implementation would use the coefficients
    linear_interpolate(x_data, y_data, x_val, extrapolate)
}

fn bilinear_sample(data: &[f32], width: usize, height: usize, x: f32, y: f32) -> f32 {
    let x0 = x.floor() as i32;
    let y0 = y.floor() as i32;
    let x1 = x0 + 1;
    let y1 = y0 + 1;

    let wx = x - x0 as f32;
    let wy = y - y0 as f32;

    // Bounds checking
    let safe_sample = |x: i32, y: i32| -> f32 {
        if x >= 0 && x < width as i32 && y >= 0 && y < height as i32 {
            data[y as usize * width + x as usize]
        } else {
            0.0 // Zero padding
        }
    };

    let v00 = safe_sample(x0, y0);
    let v01 = safe_sample(x0, y1);
    let v10 = safe_sample(x1, y0);
    let v11 = safe_sample(x1, y1);

    let v0 = v00 * (1.0 - wx) + v10 * wx;
    let v1 = v01 * (1.0 - wx) + v11 * wx;

    v0 * (1.0 - wy) + v1 * wy
}

fn nearest_sample(data: &[f32], width: usize, height: usize, x: f32, y: f32) -> f32 {
    let x_idx = (x + 0.5) as usize;
    let y_idx = (y + 0.5) as usize;

    if x_idx < width && y_idx < height {
        data[y_idx * width + x_idx]
    } else {
        0.0
    }
}

/// Bicubic sample of a `[height, width]` plane, for use by other modules of the crate.
pub(crate) fn sample_bicubic(data: &[f32], width: usize, height: usize, x: f32, y: f32) -> f32 {
    bicubic_sample(data, width, height, x, y)
}

/// Cubic convolution kernel with `a = -0.75`, the coefficient PyTorch and OpenCV use.
///
/// `w(t) = (a+2)|t|³ - (a+3)|t|² + 1` for `|t| <= 1` and
/// `w(t) = a|t|³ - 5a|t|² + 8a|t| - 4a` for `1 < |t| < 2`.
fn cubic_weight(t: f32) -> f32 {
    const A: f32 = -0.75;
    let t = t.abs();
    if t <= 1.0 {
        ((A + 2.0) * t - (A + 3.0)) * t * t + 1.0
    } else if t < 2.0 {
        (((t - 5.0) * t + 8.0) * t - 4.0) * A
    } else {
        0.0
    }
}

/// Bicubic sample of a `[height, width]` plane at fractional coordinates.
///
/// Uses the separable 4x4 cubic convolution kernel; samples outside the plane are
/// clamped to the border, which is the convention of `torch.nn.functional.interpolate`.
fn bicubic_sample(data: &[f32], width: usize, height: usize, x: f32, y: f32) -> f32 {
    if width == 0 || height == 0 {
        return 0.0;
    }
    let x_floor = x.floor();
    let y_floor = y.floor();
    let dx = x - x_floor;
    let dy = y - y_floor;

    let clamp = |value: i64, limit: usize| -> usize { value.clamp(0, limit as i64 - 1) as usize };

    let mut result = 0.0f32;
    for m in -1i64..=2 {
        let sample_y = clamp(y_floor as i64 + m, height);
        let weight_y = cubic_weight(m as f32 - dy);
        let mut row = 0.0f32;
        for n in -1i64..=2 {
            let sample_x = clamp(x_floor as i64 + n, width);
            row += cubic_weight(n as f32 - dx) * data[sample_y * width + sample_x];
        }
        result += weight_y * row;
    }
    result
}

fn lanczos_kernel(x: f32, a: usize) -> f32 {
    if x.abs() >= a as f32 {
        0.0
    } else if x.abs() < 1e-6 {
        1.0
    } else {
        let pi_x = std::f32::consts::PI * x;
        let pi_x_a = pi_x / a as f32;
        a as f32 * pi_x.sin() * pi_x_a.sin() / (pi_x * pi_x)
    }
}

fn lanczos_interpolate(x_data: &[f32], y_data: &[f32], x_val: f32, a: usize) -> TorshResult<f32> {
    let mut numerator = 0.0;
    let mut denominator = 0.0;

    for (_i, (&xi, &yi)) in x_data.iter().zip(y_data.iter()).enumerate() {
        let weight = lanczos_kernel(x_val - xi, a);
        numerator += weight * yi;
        denominator += weight;
    }

    if denominator.abs() < 1e-10 {
        // Fall back to nearest neighbor
        let mut min_dist = f32::INFINITY;
        let mut nearest_val = 0.0;
        for (i, &xi) in x_data.iter().enumerate() {
            let dist = (x_val - xi).abs();
            if dist < min_dist {
                min_dist = dist;
                nearest_val = y_data[i];
            }
        }
        Ok(nearest_val)
    } else {
        Ok(numerator / denominator)
    }
}

fn compute_barycentric_weights(x_data: &[f32]) -> TorshResult<Vec<f32>> {
    let n = x_data.len();
    let mut weights = vec![1.0; n];

    for i in 0..n {
        for j in 0..n {
            if i != j {
                weights[i] /= x_data[i] - x_data[j];
            }
        }
    }

    Ok(weights)
}

fn evaluate_barycentric(
    x_data: &[f32],
    y_data: &[f32],
    weights: &[f32],
    x_val: f32,
) -> TorshResult<f32> {
    let mut numerator = 0.0;
    let mut denominator = 0.0;

    for (_i, (&xi, (&yi, &wi))) in x_data
        .iter()
        .zip(y_data.iter().zip(weights.iter()))
        .enumerate()
    {
        if (x_val - xi).abs() < 1e-10 {
            return Ok(yi); // Exact match
        }

        let term = wi / (x_val - xi);
        numerator += term * yi;
        denominator += term;
    }

    if denominator.abs() < 1e-10 {
        Err(TorshError::InvalidArgument(
            "Barycentric interpolation failed: denominator near zero".to_string(),
        ))
    } else {
        Ok(numerator / denominator)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use torsh_tensor::creation::tensor_1d;

    #[test]
    fn test_linear_interpolation() {
        let x = tensor_1d(&[0.0, 1.0, 2.0, 3.0]).unwrap();
        let y = tensor_1d(&[0.0, 1.0, 4.0, 9.0]).unwrap();
        let x_new = tensor_1d(&[0.5, 1.5, 2.5]).unwrap();

        let result = interp1d(&x, &y, &x_new, false).unwrap();
        let result_data = result.data().expect("tensor should have data");

        // Expected values: [0.5, 2.5, 6.5]
        assert!((result_data[0] - 0.5).abs() < 1e-6);
        assert!((result_data[1] - 2.5).abs() < 1e-6);
        assert!((result_data[2] - 6.5).abs() < 1e-6);
    }

    #[test]
    fn test_bilinear_sampling() {
        let data = vec![1.0, 2.0, 3.0, 4.0]; // 2x2 grid
        let result = bilinear_sample(&data, 2, 2, 0.5, 0.5);

        // Should be average of all four values: (1+2+3+4)/4 = 2.5
        assert!((result - 2.5).abs() < 1e-6);
    }

    #[test]
    fn test_lanczos_kernel() {
        // Test Lanczos kernel properties
        assert!((lanczos_kernel(0.0, 3) - 1.0).abs() < 1e-6); // Should be 1 at origin
        assert!((lanczos_kernel(3.0, 3)).abs() < 1e-6); // Should be 0 at boundary
        assert!((lanczos_kernel(-3.0, 3)).abs() < 1e-6); // Should be 0 at negative boundary
    }

    #[test]
    fn test_grid_sample_basic() {
        use torsh_tensor::creation::zeros;

        let input = zeros(&[1, 1, 2, 2]).unwrap(); // 1x1x2x2
        let grid = zeros(&[1, 1, 1, 2]).unwrap(); // 1x1x1x2 (sample at origin)

        let result = grid_sample(&input, &grid, InterpolationMode::Linear, "zeros", false);

        // Should not panic and return correct shape
        assert!(result.is_ok());
        let result_tensor = result.unwrap();
        assert_eq!(result_tensor.shape().dims(), &[1, 1, 1, 1]);
    }
}