File: convolution.rs

package info (click to toggle)
rust-nalgebra 0.33.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,340 kB
  • sloc: makefile: 2
file content (119 lines) | stat: -rw-r--r-- 3,956 bytes parent folder | download | duplicates (3)
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
use na::{DVector, Vector2, Vector3, Vector4, Vector5};
use std::panic;

//
// Should mimic calculations in Python's scipy library
// >>>from scipy.signal import convolve
//

// >>> convolve([1,2,3,4],[1,2],"same")
// array([ 1,  4,  7, 10])
#[test]
fn convolve_same_check() {
    // Static Tests
    let actual_s = Vector4::new(1.0, 4.0, 7.0, 10.0);
    let expected_s = Vector4::new(1.0, 2.0, 3.0, 4.0).convolve_same(Vector2::new(1.0, 2.0));

    assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));

    // Dyn Tests
    let actual_d = DVector::from_vec(vec![1.0, 4.0, 7.0, 10.0]);
    let expected_d = DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
        .convolve_same(DVector::from_vec(vec![1.0, 2.0]));

    assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));

    // Panic Tests
    // These really only apply to dynamic sized vectors
    assert!(panic::catch_unwind(|| {
        let _ = DVector::from_vec(vec![1.0, 2.0])
            .convolve_same(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
    })
    .is_err());

    assert!(panic::catch_unwind(|| {
        let _ = DVector::<f32>::from_vec(vec![])
            .convolve_same(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
    })
    .is_err());

    assert!(panic::catch_unwind(|| {
        let _ = DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
            .convolve_same(DVector::<f32>::from_vec(vec![]));
    })
    .is_err());
}

// >>> convolve([1,2,3,4],[1,2],"full")
// array([ 1, 4,  7, 10, 8])
#[test]
fn convolve_full_check() {
    // Static Tests
    let actual_s = Vector5::new(1.0, 4.0, 7.0, 10.0, 8.0);
    let expected_s = Vector4::new(1.0, 2.0, 3.0, 4.0).convolve_full(Vector2::new(1.0, 2.0));

    assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));

    // Dyn Tests
    let actual_d = DVector::from_vec(vec![1.0, 4.0, 7.0, 10.0, 8.0]);
    let expected_d = DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
        .convolve_full(DVector::from_vec(vec![1.0, 2.0]));

    assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));

    // Panic Tests
    // These really only apply to dynamic sized vectors
    assert!(panic::catch_unwind(|| {
        DVector::from_vec(vec![1.0, 2.0])
            .convolve_full(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
    })
    .is_err());

    assert!(panic::catch_unwind(|| {
        DVector::<f32>::from_vec(vec![]).convolve_full(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
    })
    .is_err());

    assert!(panic::catch_unwind(|| {
        DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]).convolve_full(DVector::<f32>::from_vec(vec![]));
    })
    .is_err());
}

// >>> convolve([1, 2, 3, 4],[1, 2],"valid")
// array([4, 7, 10])
#[test]
fn convolve_valid_check() {
    // Static Tests
    let actual_s = Vector3::from_vec(vec![4.0, 7.0, 10.0]);
    let expected_s = Vector4::new(1.0, 2.0, 3.0, 4.0).convolve_valid(Vector2::new(1.0, 2.0));

    assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));

    // Dyn Tests
    let actual_d = DVector::from_vec(vec![4.0, 7.0, 10.0]);
    let expected_d = DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
        .convolve_valid(DVector::from_vec(vec![1.0, 2.0]));

    assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));

    // Panic Tests
    // These really only apply to dynamic sized vectors
    assert!(panic::catch_unwind(|| {
        DVector::from_vec(vec![1.0, 2.0])
            .convolve_valid(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
    })
    .is_err());

    assert!(panic::catch_unwind(|| {
        DVector::<f32>::from_vec(vec![])
            .convolve_valid(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
    })
    .is_err());

    assert!(panic::catch_unwind(|| {
        DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
            .convolve_valid(DVector::<f32>::from_vec(vec![]));
    })
    .is_err());
}