nalgebra/tests/linalg/convolution.rs

120 lines
3.7 KiB
Rust
Raw Permalink Normal View History

2020-04-06 00:49:48 +08:00
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]
2020-04-06 00:49:48 +08:00
fn convolve_same_check() {
// Static Tests
2020-04-06 00:49:48 +08:00
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));
2023-01-14 23:22:27 +08:00
// Dyn Tests
2020-04-06 00:49:48 +08:00
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
2020-04-06 00:49:48 +08:00
assert!(panic::catch_unwind(|| {
2021-06-18 15:45:37 +08:00
let _ = DVector::from_vec(vec![1.0, 2.0])
2020-04-06 00:49:48 +08:00
.convolve_same(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
})
.is_err());
assert!(panic::catch_unwind(|| {
2021-06-18 15:45:37 +08:00
let _ = DVector::<f32>::from_vec(vec![])
.convolve_same(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
2020-04-06 00:49:48 +08:00
})
.is_err());
assert!(panic::catch_unwind(|| {
2021-06-18 15:45:37 +08:00
let _ = DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])
.convolve_same(DVector::<f32>::from_vec(vec![]));
2020-04-06 00:49:48 +08:00
})
.is_err());
}
// >>> convolve([1,2,3,4],[1,2],"full")
// array([ 1, 4, 7, 10, 8])
#[test]
2020-04-06 00:49:48 +08:00
fn convolve_full_check() {
// Static Tests
2020-04-06 00:49:48 +08:00
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));
2023-01-14 23:22:27 +08:00
// Dyn Tests
2020-04-06 00:49:48 +08:00
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
2020-04-06 00:49:48 +08:00
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]
2020-04-06 00:49:48 +08:00
fn convolve_valid_check() {
// Static Tests
2020-04-06 00:49:48 +08:00
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));
2023-01-14 23:22:27 +08:00
// Dyn Tests
2020-04-06 00:49:48 +08:00
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
2020-04-06 00:49:48 +08:00
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());
}