diff --git a/src/linalg/convolution.rs b/src/linalg/convolution.rs index a943ec21..867d414c 100644 --- a/src/linalg/convolution.rs +++ b/src/linalg/convolution.rs @@ -167,25 +167,9 @@ impl DMatrix { let kernel_min = kernel_size/2; let zero = zero::(); let mut conv = DMatrix::from_element(mat_cols, mat_rows, zero); -// -// for i in 0..(vec + ker - 1) { -// let u_i = if i > vec { i - ker } else { 0 }; -// let u_f = cmp::min(i, vec - 1); -// -// if u_i == u_f { -// conv[i] += self[u_i] * kernel[(i - u_i)]; -// } else { -// for u in u_i..(u_f + 1) { -// if i - u < ker { -// conv[i] += self[u] * kernel[(i - u)]; -// } -// } -// } -// } for i in 0..mat_rows { for j in 0..mat_cols { - let val = *self.index((i,j)); for k_i in 0..kernel_size { for k_j in 0..kernel_size { let i_matrix = (i + k_i - kernel_min) as i32; @@ -195,7 +179,7 @@ impl DMatrix { let is_j_in_range = j_matrix >=0 && j_matrix < mat_cols as i32; let convolved_value = - match is_i_in_range && is_i_in_range { + match is_i_in_range && is_j_in_range { true => { let pixel_value = *self.index((i_matrix as usize, j_matrix as usize)); let kernel_value = *kernel.index((k_i,k_j)); diff --git a/tests/linalg/convolution.rs b/tests/linalg/convolution.rs index 984f1fa3..f4bb076c 100644 --- a/tests/linalg/convolution.rs +++ b/tests/linalg/convolution.rs @@ -1,4 +1,4 @@ -use na::{Vector2,Vector3,Vector4,Vector5,DVector}; +use na::{Vector2,Vector3,Vector4,Vector5,DVector, DMatrix}; use std::panic; // @@ -43,43 +43,6 @@ fn convolve_same_check(){ ); } -//// >>> convolve([1,2,3,4],[1,2],"same") -//// array([ 1, 4, 7, 10]) -//#[test] -//fn convolve_same_integers_check(){ -// // Static Tests -// let actual_s = Vector4::new(1, 4, 7, 10); -// let expected_s = Vector4::new(1, 2, 3, 4).convolve_same(Vector2::new(1, 2)); -// -// assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7)); -// -// // Dynamic Tests -// let actual_d = DVector::from_vec(vec![1, 4, 7, 10]); -// let expected_d = DVector::from_vec(vec![1, 2, 3, 4]).convolve_same(DVector::from_vec(vec![1, 2])); -// -// 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, 2]).convolve_same(DVector::from_vec(vec![1, 2, 3, 4])); -// }).is_err() -// ); -// -// assert!( -// panic::catch_unwind(|| { -// DVector::::from_vec(vec![]).convolve_same(DVector::from_vec(vec![1, 2, 3, 4])); -// }).is_err() -// ); -// -// assert!( -// panic::catch_unwind(|| { -// DVector::from_vec(vec![1, 2, 3, 4]).convolve_same(DVector::::from_vec(vec![])); -// }).is_err() -// ); -//} - // >>> convolve([1,2,3,4],[1,2],"full") // array([ 1, 4, 7, 10, 8]) #[test] @@ -153,4 +116,76 @@ fn convolve_valid_check(){ }).is_err() ); +} + +//// >>> convolve([1,2,3,4],[1,2],"same") +//// array([ 1, 4, 7, 10]) +//#[test] +//fn convolve_same_integers_check(){ +// // Static Tests +// let actual_s = Vector4::new(1, 4, 7, 10); +// let expected_s = Vector4::new(1, 2, 3, 4).convolve_same(Vector2::new(1, 2)); +// +// assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7)); +// +// // Dynamic Tests +// let actual_d = DVector::from_vec(vec![1, 4, 7, 10]); +// let expected_d = DVector::from_vec(vec![1, 2, 3, 4]).convolve_same(DVector::from_vec(vec![1, 2])); +// +// 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, 2]).convolve_same(DVector::from_vec(vec![1, 2, 3, 4])); +// }).is_err() +// ); +// +// assert!( +// panic::catch_unwind(|| { +// DVector::::from_vec(vec![]).convolve_same(DVector::from_vec(vec![1, 2, 3, 4])); +// }).is_err() +// ); +// +// assert!( +// panic::catch_unwind(|| { +// DVector::from_vec(vec![1, 2, 3, 4]).convolve_same(DVector::::from_vec(vec![])); +// }).is_err() +// ); +//} + +// +// 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_dmat_check(){ + let actual_d = DMatrix::from_vec(5,5, vec![3.0,4.0,4.0,4.0,3.0,4.0,5.0,5.0,5.0,4.0,4.0,5.0,5.0,5.0,4.0,4.0,5.0,5.0,5.0,4.0,3.0,4.0,4.0,4.0,3.0]); + let expected_d = DMatrix::from_element(5,5,1.0).mat_convolve_full(DMatrix::from_vec(3,3,vec![0.0,1.0,0.0,1.0,1.0,1.0,0.0,1.0,0.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_same(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0])); +// }).is_err() +// ); +// +// assert!( +// panic::catch_unwind(|| { +// DVector::::from_vec(vec![]).convolve_same(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_same(DVector::::from_vec(vec![])); +// }).is_err() +// ); } \ No newline at end of file