1 use na::{DVector, Vector2, Vector3, Vector4, Vector5};
2 use std::panic;
3
4 //
5 // Should mimic calculations in Python's scipy library
6 // >>>from scipy.signal import convolve
7 //
8
9 // >>> convolve([1,2,3,4],[1,2],"same")
10 // array([ 1, 4, 7, 10])
11 #[test]
convolve_same_check()12 fn convolve_same_check() {
13 // Static Tests
14 let actual_s = Vector4::new(1.0, 4.0, 7.0, 10.0);
15 let expected_s = Vector4::new(1.0, 2.0, 3.0, 4.0).convolve_same(Vector2::new(1.0, 2.0));
16
17 assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
18
19 // Dynamic Tests
20 let actual_d = DVector::from_vec(vec![1.0, 4.0, 7.0, 10.0]);
21 let expected_d = DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
22 .convolve_same(DVector::from_vec(vec![1.0, 2.0]));
23
24 assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
25
26 // Panic Tests
27 // These really only apply to dynamic sized vectors
28 assert!(panic::catch_unwind(|| {
29 let _ = DVector::from_vec(vec![1.0, 2.0])
30 .convolve_same(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
31 })
32 .is_err());
33
34 assert!(panic::catch_unwind(|| {
35 let _ = DVector::<f32>::from_vec(vec![])
36 .convolve_same(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
37 })
38 .is_err());
39
40 assert!(panic::catch_unwind(|| {
41 let _ = DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
42 .convolve_same(DVector::<f32>::from_vec(vec![]));
43 })
44 .is_err());
45 }
46
47 // >>> convolve([1,2,3,4],[1,2],"full")
48 // array([ 1, 4, 7, 10, 8])
49 #[test]
convolve_full_check()50 fn convolve_full_check() {
51 // Static Tests
52 let actual_s = Vector5::new(1.0, 4.0, 7.0, 10.0, 8.0);
53 let expected_s = Vector4::new(1.0, 2.0, 3.0, 4.0).convolve_full(Vector2::new(1.0, 2.0));
54
55 assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
56
57 // Dynamic Tests
58 let actual_d = DVector::from_vec(vec![1.0, 4.0, 7.0, 10.0, 8.0]);
59 let expected_d = DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
60 .convolve_full(DVector::from_vec(vec![1.0, 2.0]));
61
62 assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
63
64 // Panic Tests
65 // These really only apply to dynamic sized vectors
66 assert!(panic::catch_unwind(|| {
67 DVector::from_vec(vec![1.0, 2.0])
68 .convolve_full(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
69 })
70 .is_err());
71
72 assert!(panic::catch_unwind(|| {
73 DVector::<f32>::from_vec(vec![]).convolve_full(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
74 })
75 .is_err());
76
77 assert!(panic::catch_unwind(|| {
78 DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]).convolve_full(DVector::<f32>::from_vec(vec![]));
79 })
80 .is_err());
81 }
82
83 // >>> convolve([1, 2, 3, 4],[1, 2],"valid")
84 // array([4, 7, 10])
85 #[test]
convolve_valid_check()86 fn convolve_valid_check() {
87 // Static Tests
88 let actual_s = Vector3::from_vec(vec![4.0, 7.0, 10.0]);
89 let expected_s = Vector4::new(1.0, 2.0, 3.0, 4.0).convolve_valid(Vector2::new(1.0, 2.0));
90
91 assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
92
93 // Dynamic Tests
94 let actual_d = DVector::from_vec(vec![4.0, 7.0, 10.0]);
95 let expected_d = DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
96 .convolve_valid(DVector::from_vec(vec![1.0, 2.0]));
97
98 assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
99
100 // Panic Tests
101 // These really only apply to dynamic sized vectors
102 assert!(panic::catch_unwind(|| {
103 DVector::from_vec(vec![1.0, 2.0])
104 .convolve_valid(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
105 })
106 .is_err());
107
108 assert!(panic::catch_unwind(|| {
109 DVector::<f32>::from_vec(vec![])
110 .convolve_valid(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
111 })
112 .is_err());
113
114 assert!(panic::catch_unwind(|| {
115 DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
116 .convolve_valid(DVector::<f32>::from_vec(vec![]));
117 })
118 .is_err());
119 }
120