diff --git a/examples/convolution.rs b/examples/convolution.rs new file mode 100644 index 00000000..5290440c --- /dev/null +++ b/examples/convolution.rs @@ -0,0 +1,5 @@ +fn main(){ + let (x,y) = (1,2); + + println!("{}", x); +} \ No newline at end of file diff --git a/src/linalg/convolution.rs b/src/linalg/convolution.rs index e7e20c21..ac0ba71e 100644 --- a/src/linalg/convolution.rs +++ b/src/linalg/convolution.rs @@ -1,45 +1,63 @@ use storage::Storage; -use {zero, DVector, Dim, Dynamic, Matrix, Real, VecStorage, Vector, U1}; +use {zero, DVector, Dim, Dynamic, Matrix, Real, VecStorage, Vector, U1, Add}; use std::cmp; -/// -/// The output is the full discrete linear convolution of the inputs -/// -pub fn convolve_full, Q: Storage>( - vector: Vector, - kernel: Vector, -) -> Matrix> { - let vec = vector.len(); - let ker = kernel.len(); +impl> Vector{ - if vec == 0 || ker == 0 { - panic!("Convolve's inputs must not be 0-sized. "); - } + /// Returns the convolution of the vector and a kernel + /// + /// # Arguments + /// + /// * `self` - A DVector with size D > 0 + /// * `kernel` - A DVector with size D > 0 + /// + /// # Note: + /// This function is commutative. If D_kernel > D_vector, + /// they will swap their roles as in + /// (self, kernel) = (kernel,self) + /// + /// # Example + /// + /// ``` + /// + /// ``` + pub fn convolve_full>(&self, kernel: Vector) -> Vector,Add> + { + let vec = self.len(); + let ker = kernel.len(); - if ker > vec { - return convolve_full(kernel, vector); - } + // if vec == 0 || ker == 0 { + // panic!("Convolve's inputs must not be 0-sized. "); + // } - let newlen = vec + ker - 1; + // if ker > vec { + // return kernel::convolve_full(vector); + // } - let mut conv = DVector::::zeros(newlen); + let newlen = vec + ker - 1; + let mut conv = DVector::::zeros(newlen); - for i in 0..newlen { - let u_i = if i > ker { i - ker } else { 0 }; - let u_f = cmp::min(i, vec - 1); + for i in 0..newlen { + let u_i = if i > ker { i - ker } else { 0 }; + let u_f = cmp::min(i, vec - 1); - if u_i == u_f { - conv[i] += vector[u_i] * kernel[(i - u_i)]; - } else { - for u in u_i..(u_f + 1) { - if i - u < ker { - conv[i] += vector[u] * kernel[(i - u)]; + 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)]; + } } } } + // conv } - conv } +/// +/// The output is the full discrete linear convolution of the inputs +/// + /// /// The output convolution consists only of those elements that do not rely on the zero-padding. @@ -102,4 +120,5 @@ pub fn convolve_same, Q: Storage } } conv -} \ No newline at end of file +} +