interp 2.1.2

A Rust implementation of Matlab's interp1 function
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
//! An implementation of 1-dimensional linear interpolation in Rust, similar to MATLAB's `interp1`
//! or NumPy's `numpy.interp`.
//!
//! [`numpy.interp`]: https://numpy.org/doc/stable/reference/generated/numpy.interp.html
//! [`interp1`]: https://www.mathworks.com/help/matlab/ref/double.interp1.html
//!
//! # Example
//!
//! ```
//! use interp::{interp, interp_array, interp_slice, InterpMode};
//!
//! let x = vec![0.0, 0.2, 0.5, 0.8, 1.0];
//! let y = vec![0.0, 1.0, 3.0, 3.5, 4.0];
//!
//! // Interpolate at a single point
//! assert_eq!(interp(&x, &y, 0.35, &InterpMode::default()), 2.0);
//!
//! // Interpolate a slice - alloces a new results Vec<T>
//! let xp = vec![0.1, 0.65, 0.9];
//! assert_eq!(interp_slice(&x, &y, &xp, &InterpMode::default()), vec![0.5, 3.25, 3.75]);
//!
//! // Interpolate an array
//! let xp = [0.1, 0.65, 0.9];
//! assert_eq!(interp_array(&x, &y, &xp, &InterpMode::default()), [0.5, 3.25, 3.75]);
//! ```
//!
//! <div class="warning">
//!
//! **Warning:** `x` is expected to be strictly increasing, but this is not explicitly enforced.
//! However, if `x` is non-increasing, interpolation results are meaningless.
//!
//! </div>

#![warn(missing_docs)]
#![allow(unknown_lints)]
#![warn(clippy::all, clippy::pedantic, clippy::cargo)]
#![allow(clippy::many_single_char_names)]

use num_traits::Num;

use itertools::{izip, Itertools};

/// Interpolation method that sets the behaviour of the interpolation functions for points outside
/// the range of sample points.
#[derive(Debug, Clone, Copy, Default)]
pub enum InterpMode<T: std::cmp::PartialOrd + Copy> {
    /// Use slope information to linearly extrapolate the value.
    ///
    /// This was the default behaviour in earlier versions of this crate.
    #[default]
    Extrapolate,
    /// Use `y.first()` for `xp <= x.first()`, or `y.last()` for `xp >= x.last()`. This is similar
    /// to the default behaviour or NumPy's [`numpy.interp`] function.
    ///
    /// [`numpy.interp`]: https://numpy.org/doc/stable/reference/generated/numpy.interp.html
    FirstLast,
    /// Use the given constant for values outside the range.
    ///
    /// This is commonly used to return `T::NAN` (assuming `T: Float`) to mirror the MATLAB
    /// [`interp1`] function's behaviour using the `'linear'` method.
    ///
    /// [`interp1`]: https://www.mathworks.com/help/matlab/ref/double.interp1.html
    Constant(T),
}

/// Finds the delta between adjacent entries in the slice `p`.
///
/// Returns a [`Vec<T>`] which is one element shorter than `p.len()` containing the difference
/// between each pair of values.
///
/// If `p.len()` is one or less, returns an empty vector.
///
/// # Example
///
/// ```ignore
/// let p = vec![0.0, 1.0, 3.0, 3.5];
///
/// assert_eq!(deltas(&p), vec![1.0, 2.0, 0.5]);
/// ```
#[inline]
fn deltas<T>(p: &[T]) -> Vec<T>
where
    T: Num + Copy,
{
    p.iter().tuple_windows().map(|(&p1, &p2)| p2 - p1).collect()
}

/// Finds the slope of a line segment given two slices containing the x and y deltas.
///
/// Parameters `dx` and `dy` are the differences between adjacent x and y coordinates, respectively.
/// Returns a [`Vec<T>`] containing the slope for each segment.
///
/// If the lengths of `dx` and `dy` are not equal, only the number of elements in the shorter slice
/// are considered; excess elements are ignored.
///
/// # Example
///
/// ```ignore
/// let dx = vec![1.0, 2.0, 0.5];
/// let dy = vec![1.0; 3];
///
/// assert_eq!(slopes(&dx, &dy), vec![1.0, 0.5, 2.0]);
/// ```
#[inline]
fn slopes<T>(dx: &[T], dy: &[T]) -> Vec<T>
where
    T: Num + Copy,
{
    izip!(dx, dy).map(|(&dx, &dy)| dy / dx).collect()
}

/// Finds the y-intercept of line segments given by the points `x` and `y`, and slopes `m`.
///
/// Returns a `Vec<T>` of the same length containing each of the intercepts.
///
/// If the lengths of `x`, `y`, and `m` are not equal, only the number of elements in the shortest
/// slice are considered; excess elements are ignored.
///
/// # Example
///
/// ```ignore
/// let x = vec![0.0, 1.0, 3.5];
/// let y = vec![0.0, 1.0, 6.0];
/// let slope = vec![1.0, 2.0, -0.5];
///
/// assert_eq!(intercepts(&x, &y, &slope), vec![0.0, -1.0, 7.75])
/// ```
#[inline]
fn intercepts<T>(x: &[T], y: &[T], m: &[T]) -> Vec<T>
where
    T: Num + Copy,
{
    izip!(x, y, m).map(|(&x, &y, &m)| y - x * m).collect()
}

/// Finds the index of the value in `x` just before `xp`.
///
/// If the values in `x` are not strictly increasing, the first possible result is returned.
///
/// Returns 0 if there are no elements in `x` which are less than `xp`.
///
/// # Example
///
/// ```ignore
/// let x = vec![0.0, 1.0, 3.0, 4.5];
///
/// assert_eq!(prev_index(&x, 3.5), 2);
/// ```
#[inline]
fn prev_index<T>(x: &[T], xp: T) -> usize
where
    T: Num + PartialOrd + Copy,
{
    x.partition_point(|&probe| probe < xp).saturating_sub(1)
}

/// Linearly interpolate the data points given by the `x` and `y` slices at point `xp`,
/// using the interpolation method provided by `mode`.
///
/// Returns the equivalent y coordinate to the x coordinate given by `xp`.
///
/// If the lengths of `x` and `y` differ, only the number of elements in the shorter slice are
/// considered; excess elements are ignored.
///
/// If the length of either `x` or `y` is 0, 0 is returned. If the length of either is 1, `y[0]` is
/// returned. If both are 2 elements or longer the interpolation is performed as expected.
///
/// # Example
///
/// ```
/// use interp::interp;
/// use interp::InterpMode;
///
/// let x = vec![0.0, 1.0, 2.0, 3.0];
/// let y = vec![1.0, 3.0, 4.0, 2.0];
///
/// assert_eq!(interp(&x, &y, 1.5, &InterpMode::Extrapolate), 3.5);
/// ```
///
/// <div class="warning">
///
/// **Warning:** `x` is expected to be strictly increasing, but this is not explicitly enforced.
/// However, if `x` is non-increasing, interpolation results are meaningless.
///
/// </div>
pub fn interp<T>(x: &[T], y: &[T], xp: T, mode: &InterpMode<T>) -> T
where
    T: Num + PartialOrd + Copy,
{
    // The min-length of the x and y vectors. We ignore additional entries in either vec.
    let min_len = std::cmp::min(x.len(), y.len());

    if min_len == 0 {
        T::zero()
    } else if min_len == 1 {
        y[0]
    } else {
        // Difference between subsequent x and y coordinate values
        let dx = deltas(&x[..min_len]);
        let dy = deltas(&y[..min_len]);

        // Slope between subsequent points
        let m = slopes(&dx, &dy);

        // Intercept of the line between adjacent points
        let c = intercepts(x, y, &m);

        // The index of the x coordinate right before xp
        // i = 0 when none found
        let i = prev_index(x, xp).min(min_len - 2);

        let point = m[i] * xp + c[i];
        let x_limits = (&x[0], &x[min_len - 1]);
        let y_limits = (&y[0], &y[min_len - 1]);

        select_outside_point(x_limits, y_limits, &xp, point, mode)
    }
}

/// Linearly interpolate the data points given by the `x` and `y` slices at each of the points in
/// the `xp` slice, using the interpolation method provided by `mode`.
///
/// Returns a `Vec<T>` containing the equivalent y coordinates to each of the x coordinates given
/// by `xp`.
///
/// This is equivalent to running [`interp`] iteratively for each value in `xp`, but more efficient
/// as intermediate calculations are not repeated.
///
/// If the lengths of `x` and `y` differ, only the number of elements in the shorter slice are
/// considered; excess elements are ignored.
///
/// If the length of either `x` or `y` is 0, 0 is returned for each `xp`. If the length of either
/// is 1, `y[0]` is returned. If both are 2 elements or longer the interpolations are performed as
/// expected.
///
/// # Example
///
/// ```
/// use interp::interp_slice;
/// use interp::InterpMode;
///
/// let x = vec![0.0, 1.0, 2.0, 3.0];
/// let y = vec![1.0, 3.0, 4.0, 2.0];
///
/// let xp = vec![0.5, 2.5, 4.0];
///
/// assert_eq!(interp_slice(&x, &y, &xp, &InterpMode::Extrapolate), vec![2.0, 3.0, 0.0]);
///
/// assert_eq!(interp_slice(&x, &y, &xp, &InterpMode::Constant(1e3)), vec![2.0, 3.0, 1e3]);
///
/// assert_eq!(interp_slice(&x, &y, &xp, &InterpMode::FirstLast), vec![2.0, 3.0, 2.0]);
/// ```
///
/// <div class="warning">
///
/// **Warning:** `x` is expected to be strictly increasing, but this is not explicitly enforced.
/// However, if `x` is non-increasing, interpolation results are meaningless.
///
/// </div>
pub fn interp_slice<T>(x: &[T], y: &[T], xp: &[T], mode: &InterpMode<T>) -> Vec<T>
where
    T: Num + PartialOrd + Copy,
{
    // The min-length of the x and y vectors. We ignore additional entries in either vec.
    let min_len = std::cmp::min(x.len(), y.len());

    if min_len == 0 {
        vec![T::zero(); xp.len()]
    } else if min_len == 1 {
        vec![y[0]; xp.len()]
    } else {
        // Difference between subsequent x and y coordinate values
        let dx = deltas(&x[..min_len]);
        let dy = deltas(&y[..min_len]);

        // Slope between subsequent points
        let m = slopes(&dx, &dy);

        // Intercept of the line between adjacent points
        let c = intercepts(x, y, &m);

        let x_limits = (&x[0], &x[min_len - 1]);
        let y_limits = (&y[0], &y[min_len - 1]);

        xp.iter()
            .map(|&xp| {
                // The index of the x coordinate right before xp. Use min to ensure we don't go out
                // of m's and c's bounds when xp > x.last()
                let i = prev_index(x, xp).min(min_len - 2);

                let point = m[i] * xp + c[i];

                select_outside_point(x_limits, y_limits, &xp, point, mode)
            })
            .collect()
    }
}

/// Linearly interpolate the data points given by the `x` and `y` slices at each of the points in
/// the `xp` slice, using the interpolation method provided by `mode`.
///
/// Returns a `[T; N]` containing the equivalent y coordinates to each of the x coordinates given
/// by `xp`.
///
/// This is equivalent to running [`interp`] iteratively for each value in `xp`, but more efficient
/// as intermediate calculations are not repeated.
///
/// If the lengths of `x` and `y` differ, only the number of elements in the shorter slice are
/// considered; excess elements are ignored.
///
/// If the length of either `x` or `y` is 0, 0 is returned for each `xp`. If the length of either
/// is 1, `y[0]` is returned. If both are 2 elements or longer the interpolations are performed as
/// expected.
///
/// # Example
///
/// ```
/// use interp::interp_array;
/// use interp::InterpMode;
///
/// let x = [0.0, 1.0, 2.0, 3.0];
/// let y = [1.0, 3.0, 4.0, 2.0];
///
/// let xp = [0.5, 2.5, 4.0];
///
/// assert_eq!(interp_array(&x, &y, &xp, &InterpMode::Extrapolate), [2.0, 3.0, 0.0]);
/// ```
///
/// <div class="warning">
///
/// **Warning:** `x` is expected to be strictly increasing, but this is not explicitly enforced.
/// However, if `x` is non-increasing, interpolation results are meaningless.
///
/// </div>
pub fn interp_array<T, const N: usize>(
    x: &[T],
    y: &[T],
    xp: &[T; N],
    mode: &InterpMode<T>,
) -> [T; N]
where
    T: Num + PartialOrd + Copy,
{
    // The min-length of the x and y vectors. We ignore additional entries in either vec.
    let min_len = std::cmp::min(x.len(), y.len());

    if min_len == 0 {
        [T::zero(); N]
    } else if min_len == 1 {
        [y[0]; N]
    } else {
        // Difference between subsequent x and y coordinate values
        let dx = deltas(&x[..min_len]);
        let dy = deltas(&y[..min_len]);

        // Slope between subsequent points
        let m = slopes(&dx, &dy);

        // Intercept of the line between adjacent points
        let c = intercepts(x, y, &m);

        let x_limits = (&x[0], &x[min_len - 1]);
        let y_limits = (&y[0], &y[min_len - 1]);

        xp.map(|xp| {
            // The index of the x coordinate right before xp. Use min to ensure we don't go out
            // of m's and c's bounds when xp > x.last()
            let i = prev_index(x, xp).min(min_len - 2);

            let point = m[i] * xp + c[i];

            select_outside_point(x_limits, y_limits, &xp, point, mode)
        })
    }
}

fn select_outside_point<T>(
    x_limits: (&T, &T),
    y_limits: (&T, &T),
    xp: &T,
    default: T,
    mode: &InterpMode<T>,
) -> T
where
    T: Num + PartialOrd + Copy,
{
    if xp == x_limits.0 {
        *y_limits.0
    } else if xp < x_limits.0 {
        match mode {
            InterpMode::Extrapolate => default,
            InterpMode::FirstLast => *y_limits.0,
            InterpMode::Constant(val) => *val,
        }
    } else if xp == x_limits.1 {
        *y_limits.1
    } else if xp > x_limits.1 {
        match mode {
            InterpMode::Extrapolate => default,
            InterpMode::FirstLast => *y_limits.1,
            InterpMode::Constant(val) => *val,
        }
    } else {
        default
    }
}

#[cfg(test)]
mod tests {
    use isclose::assert_is_close;
    use num_traits::float::FloatCore;

    use super::*;

    fn vec_compare<T>(v1: &[T], v2: &[T]) -> bool
    where
        T: Num + PartialOrd + Copy + FloatCore,
    {
        (v1.len() == v2.len()) && izip!(v1, v2).all(|(a, b)| (a.is_nan() && b.is_nan()) || (a == b))
    }

    #[test]
    fn test_deltas() {
        let p = vec![0.0, 1.0, 3.5, 2.5, 6.0];
        let delta = vec![1.0, 2.5, -1.0, 3.5];

        let result = deltas(&p);

        assert_eq!(result.len(), delta.len());

        for (r, d) in izip!(result, delta) {
            assert_is_close!(r, d);
        }
    }

    #[test]
    fn test_slopes() {
        let dx = vec![1.0, 2.5, 2.0, 1.0];
        let dy = vec![1.0, 5.0, -1.0, 3.5];
        let slope = vec![1.0, 2.0, -0.5, 3.5];

        let result = slopes(&dx, &dy);

        assert_eq!(result.len(), slope.len());

        for (r, m) in izip!(result, slope) {
            assert_is_close!(r, m);
        }
    }

    #[test]
    fn test_intercepts() {
        let x = vec![0.0, 1.0, 2.5, 3.5, 6.0];
        let y = vec![0.0, 1.0, 6.0, 5.0, 8.5];
        let slope = vec![1.0, 2.0, -0.5, 3.5, 1.0];
        let intercept = vec![0.0, -1.0, 7.25, -7.25, 2.5];

        let result = intercepts(&x, &y, &slope);

        assert_eq!(result.len(), intercept.len());

        for (r, c) in izip!(result, intercept) {
            assert_is_close!(r, c);
        }
    }

    #[test]
    fn test_prev_index() {
        let x = vec![0.0, 1.0, 2.5, 3.5, 6.0];

        let result = prev_index(&x, -1.0);
        assert_eq!(result, 0);

        let result = prev_index(&x, 3.0);
        assert_eq!(result, 2);

        let result = prev_index(&x, 4.0);
        assert_eq!(result, 3);

        let result = prev_index(&x, 7.0);
        assert_eq!(result, 4);
    }

    #[test]
    fn test_interp() {
        assert_is_close!(interp(&[], &[], 2.0, &InterpMode::Extrapolate), 0.0);

        assert_is_close!(interp(&[1.0], &[2.0], 2.0, &InterpMode::Extrapolate), 2.0);

        let x = vec![0.0, 1.0, 2.0, 3.0, 4.5];
        let y = vec![0.0, 2.0, 5.0, 3.0, 2.0];

        assert_is_close!(interp(&x, &y, 2.5, &InterpMode::Extrapolate), 4.0);
        assert_is_close!(interp(&x, &y, -1.0, &InterpMode::Extrapolate), -2.0);
        assert_is_close!(interp(&x, &y, 7.5, &InterpMode::Extrapolate), 0.0);
    }

    #[test]
    fn test_interp_slice() {
        assert_eq!(
            interp_slice(&[], &[], &[2.0], &InterpMode::Extrapolate),
            vec![0.0]
        );

        assert_eq!(
            interp_slice(&[1.0], &[2.0], &[2.0], &InterpMode::Extrapolate),
            vec![2.0]
        );

        let x = vec![0.0, 1.0, 2.0, 3.0, 4.5];
        let y = vec![0.0, 2.0, 5.0, 3.0, 2.0];

        assert_eq!(interp_slice(&x, &y, &[], &InterpMode::Extrapolate), vec![]);

        let xp = vec![2.5, -1.0, 7.5];
        let result = vec![4.0, -2.0, 0.0];

        assert_eq!(interp_slice(&x, &y, &xp, &InterpMode::Extrapolate), result);
    }

    #[test]
    fn test_interp_slice_constant_outside_bounds() {
        let x = vec![0.0, 1.0, 2.0, 3.0];
        let y = vec![1.0, 3.0, 4.0, 2.0];

        assert_eq!(
            interp_slice(&x, &y, &[], &InterpMode::Constant(f64::NAN)),
            vec![]
        );

        let xp = vec![0.5, 2.5, 4.0];

        let expected = vec![2.0, 3.0, 1e3];
        assert_eq!(
            interp_slice(&x, &y, &xp, &InterpMode::Constant(1e3)),
            expected
        );

        let x = vec![0.0, 1.0, 2.0, 3.0, 4.5];
        let y = vec![0.0, 2.0, 5.0, 3.0, 2.0];

        let xp = vec![2.5, -1.0, 7.5];
        let result = interp_slice(&x, &y, &xp, &InterpMode::Constant(f64::NAN));
        let expected = vec![4.0, f64::NAN, f64::NAN];

        assert!(vec_compare(&result, &expected));
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_interp_on_endpoints() {
        let x = [67.0, 97.0];
        let y = [18.75, 28.75];

        let y_left = interp(&x, &y, x[0], &InterpMode::Extrapolate);
        let y_right = interp(&x, &y, x[1], &InterpMode::Extrapolate);

        assert_eq!(y_left, y[0]);
        assert_eq!(y_right, y[1]);

        let y_left = interp(&x, &y, x[0], &InterpMode::FirstLast);
        let y_right = interp(&x, &y, x[1], &InterpMode::FirstLast);

        assert_eq!(y_left, y[0]);
        assert_eq!(y_right, y[1]);

        let y_left = interp(&x, &y, x[0], &InterpMode::Constant(f64::NAN));
        let y_right = interp(&x, &y, x[1], &InterpMode::Constant(f64::NAN));

        assert_eq!(y_left, y[0]);
        assert_eq!(y_right, y[1]);
    }

    #[test]
    fn test_interp_slice_first_last() {
        let x = vec![0.0, 1.0, 2.0, 3.0, 4.5];
        let y = vec![0.0, 2.0, 5.0, 3.0, 2.0];

        assert_eq!(interp_slice(&x, &y, &[], &InterpMode::FirstLast), vec![]);

        let xp = vec![2.5, -1.0, 7.5];
        let result = vec![4.0, 0.0, 2.0];

        assert_eq!(interp_slice(&x, &y, &xp, &InterpMode::FirstLast), result);
    }

    #[test]
    fn test_interp_array() {
        let result = interp_array(&[], &[], &[2.0], &InterpMode::Extrapolate);
        for (act, exp) in izip!(result, [0.0]) {
            assert_is_close!(act, exp);
        }

        let result = interp_array(&[1.0], &[2.0], &[2.0], &InterpMode::Extrapolate);
        for (act, exp) in izip!(result, [2.0]) {
            assert_is_close!(act, exp);
        }

        let x = vec![0.0, 1.0, 2.0, 3.0, 4.5];
        let y = vec![0.0, 2.0, 5.0, 3.0, 2.0];
        let result = interp_array(&x, &y, &[], &InterpMode::Extrapolate);
        for (act, exp) in izip!(result, [] as [f64; 0]) {
            assert_is_close!(act, exp);
        }

        let xp = [2.5, -1.0, 7.5];
        let expected = [4.0, -2.0, 0.0];
        let result = interp_array(&x, &y, &xp, &InterpMode::Extrapolate);
        for (act, exp) in izip!(result, expected) {
            assert_is_close!(act, exp);
        }
    }
}