Debian Codegraph @

debian/missing-sources/nalgebra/tests/linalg/convolution.rs

raw
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]
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 // Dyn 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!(
29 panic::catch_unwind(|| {
30 let _ = DVector::from_vec(vec![1.0, 2.0])
31 .convolve_same(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
32 })
33 .is_err()
34 );
35
36 assert!(
37 panic::catch_unwind(|| {
38 let _ = DVector::<f32>::from_vec(vec![])
39 .convolve_same(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
40 })
41 .is_err()
42 );
43
44 assert!(
45 panic::catch_unwind(|| {
46 let _ = DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
47 .convolve_same(DVector::<f32>::from_vec(vec![]));
48 })
49 .is_err()
50 );
51 }
52
53 // >>> convolve([1,2,3,4],[1,2],"full")
54 // array([ 1, 4, 7, 10, 8])
55 #[test]
56 fn convolve_full_check() {
57 // Static Tests
58 let actual_s = Vector5::new(1.0, 4.0, 7.0, 10.0, 8.0);
59 let expected_s = Vector4::new(1.0, 2.0, 3.0, 4.0).convolve_full(Vector2::new(1.0, 2.0));
60
61 assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
62
63 // Dyn Tests
64 let actual_d = DVector::from_vec(vec![1.0, 4.0, 7.0, 10.0, 8.0]);
65 let expected_d = DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
66 .convolve_full(DVector::from_vec(vec![1.0, 2.0]));
67
68 assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
69
70 // Panic Tests
71 // These really only apply to dynamic sized vectors
72 assert!(
73 panic::catch_unwind(|| {
74 DVector::from_vec(vec![1.0, 2.0])
75 .convolve_full(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
76 })
77 .is_err()
78 );
79
80 assert!(
81 panic::catch_unwind(|| {
82 DVector::<f32>::from_vec(vec![])
83 .convolve_full(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
84 })
85 .is_err()
86 );
87
88 assert!(
89 panic::catch_unwind(|| {
90 DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
91 .convolve_full(DVector::<f32>::from_vec(vec![]));
92 })
93 .is_err()
94 );
95 }
96
97 // >>> convolve([1, 2, 3, 4],[1, 2],"valid")
98 // array([4, 7, 10])
99 #[test]
100 fn convolve_valid_check() {
101 // Static Tests
102 let actual_s = Vector3::from_vec(vec![4.0, 7.0, 10.0]);
103 let expected_s = Vector4::new(1.0, 2.0, 3.0, 4.0).convolve_valid(Vector2::new(1.0, 2.0));
104
105 assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
106
107 // Dyn Tests
108 let actual_d = DVector::from_vec(vec![4.0, 7.0, 10.0]);
109 let expected_d = DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
110 .convolve_valid(DVector::from_vec(vec![1.0, 2.0]));
111
112 assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
113
114 // Panic Tests
115 // These really only apply to dynamic sized vectors
116 assert!(
117 panic::catch_unwind(|| {
118 DVector::from_vec(vec![1.0, 2.0])
119 .convolve_valid(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
120 })
121 .is_err()
122 );
123
124 assert!(
125 panic::catch_unwind(|| {
126 DVector::<f32>::from_vec(vec![])
127 .convolve_valid(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
128 })
129 .is_err()
130 );
131
132 assert!(
133 panic::catch_unwind(|| {
134 DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
135 .convolve_valid(DVector::<f32>::from_vec(vec![]));
136 })
137 .is_err()
138 );
139 }