Feedback updates round 1

This commit is contained in:
Nathan 2019-02-14 20:54:26 -06:00
parent bca385ea6b
commit b08c2ad70d
2 changed files with 53 additions and 29 deletions

5
examples/convolution.rs Normal file
View File

@ -0,0 +1,5 @@
fn main(){
let (x,y) = (1,2);
println!("{}", x);
}

View File

@ -1,45 +1,63 @@
use storage::Storage; 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; use std::cmp;
/// impl<N: Real, D1: Dim, S1: Storage<N,D1>> Vector<N,D1,S1>{
/// The output is the full discrete linear convolution of the inputs
///
pub fn convolve_full<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E>>(
vector: Vector<R, D, S>,
kernel: Vector<R, E, Q>,
) -> Matrix<R, Dynamic, U1, VecStorage<R, Dynamic, U1>> {
let vec = vector.len();
let ker = kernel.len();
if vec == 0 || ker == 0 { /// Returns the convolution of the vector and a kernel
panic!("Convolve's inputs must not be 0-sized. "); ///
} /// # 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<D2: Dim, S2: Storage<N, D2>>(&self, kernel: Vector<N, D2, S2>) -> Vector<N,Add<D1,D2>,Add<S1,S2>>
{
let vec = self.len();
let ker = kernel.len();
if ker > vec { // if vec == 0 || ker == 0 {
return convolve_full(kernel, vector); // 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::<R>::zeros(newlen); let newlen = vec + ker - 1;
let mut conv = DVector::<N>::zeros(newlen);
for i in 0..newlen { for i in 0..newlen {
let u_i = if i > ker { i - ker } else { 0 }; let u_i = if i > ker { i - ker } else { 0 };
let u_f = cmp::min(i, vec - 1); let u_f = cmp::min(i, vec - 1);
if u_i == u_f { if u_i == u_f {
conv[i] += vector[u_i] * kernel[(i - u_i)]; conv[i] += self[u_i] * kernel[(i - u_i)];
} else { } else {
for u in u_i..(u_f + 1) { for u in u_i..(u_f + 1) {
if i - u < ker { if i - u < ker {
conv[i] += vector[u] * kernel[(i - u)]; 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. /// The output convolution consists only of those elements that do not rely on the zero-padding.
@ -102,4 +120,5 @@ pub fn convolve_same<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E>
} }
} }
conv conv
} }