forked from M-Labs/nalgebra
Fixing type traits based on feedback, convolve_full
still broken
This commit is contained in:
parent
b08c2ad70d
commit
9f52019385
@ -1,71 +1,110 @@
|
||||
use storage::Storage;
|
||||
use {zero, DVector, Dim, Dynamic, Matrix, Real, VecStorage, Vector, U1, Add};
|
||||
use base::allocator::Allocator;
|
||||
use base::default_allocator::DefaultAllocator;
|
||||
use base::dimension::{DimAdd, DimDiff, DimMax, DimMaximum, DimName, DimSub, DimSum,Dim};
|
||||
use std::cmp;
|
||||
use storage::Storage;
|
||||
use {zero, Real, Vector, VectorN, U1};
|
||||
|
||||
impl<N: Real, D1: Dim, S1: Storage<N,D1>> Vector<N,D1,S1>{
|
||||
/// Returns the convolution of the vector and a kernel
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `vector` - A Vector with size > 0
|
||||
/// * `kernel` - A Vector with size > 0
|
||||
///
|
||||
/// # Note:
|
||||
/// This function is commutative. If kernel > vector,
|
||||
/// they will swap their roles as in
|
||||
/// (self, kernel) = (kernel,self)
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// let vec = Vector3::new(1.0,2.0,3.0);
|
||||
/// let ker = Vector2::new(0.4,0.6);
|
||||
/// let convolve = convolve_full(vec,ker);
|
||||
/// ```
|
||||
pub fn convolve_full<N, D1, D2, S1, S2>(
|
||||
vector: Vector<N, D1, S1>,
|
||||
kernel: Vector<N, D2, S2>,
|
||||
) -> VectorN<N, DimDiff<DimSum<D1, D2>, U1>>
|
||||
where
|
||||
N: Real,
|
||||
D1: DimAdd<D2>,
|
||||
D2: DimAdd<D1, Output = DimSum<D1, D2>>,
|
||||
DimSum<D1, D2>: DimSub<U1>,
|
||||
S1: Storage<N, D1>,
|
||||
S2: Storage<N, D2>,
|
||||
DimSum<D1, D2>: Dim,
|
||||
DefaultAllocator: Allocator<N, DimDiff<DimSum<D1, D2>, U1>>,
|
||||
{
|
||||
let vec = vector.len();
|
||||
let ker = kernel.len();
|
||||
|
||||
/// 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<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 vec == 0 || ker == 0 {
|
||||
panic!("Convolve's inputs must not be 0-sized. ");
|
||||
}
|
||||
|
||||
// if vec == 0 || ker == 0 {
|
||||
// panic!("Convolve's inputs must not be 0-sized. ");
|
||||
// }
|
||||
if ker > vec {
|
||||
return convolve_full(kernel, vector);
|
||||
}
|
||||
|
||||
// if ker > vec {
|
||||
// return kernel::convolve_full(vector);
|
||||
// }
|
||||
let result_len = vector.data.shape().0.add(kernel.data.shape().0).sub(U1);
|
||||
let mut conv = VectorN::zeros_generic(result_len, U1);
|
||||
|
||||
let newlen = vec + ker - 1;
|
||||
let mut conv = DVector::<N>::zeros(newlen);
|
||||
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);
|
||||
|
||||
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] += 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)];
|
||||
}
|
||||
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)];
|
||||
}
|
||||
}
|
||||
}
|
||||
// conv
|
||||
}
|
||||
conv
|
||||
}
|
||||
///
|
||||
/// The output is the full discrete linear convolution of the inputs
|
||||
///
|
||||
|
||||
|
||||
|
||||
/// Returns the convolution of the vector and a kernel
|
||||
/// The output convolution consists only of those elements that do not rely on the zero-padding.
|
||||
/// # Arguments
|
||||
///
|
||||
/// The output convolution consists only of those elements that do not rely on the zero-padding.
|
||||
///
|
||||
pub fn convolve_valid<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>> {
|
||||
/// * `vector` - A Vector with size > 0
|
||||
/// * `kernel` - A Vector with size > 0
|
||||
///
|
||||
/// # Note:
|
||||
/// This function is commutative. If kernel > vector,
|
||||
/// they will swap their roles as in
|
||||
/// (self, kernel) = (kernel,self)
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// let vec = Vector3::new(1.0,2.0,3.0);
|
||||
/// let ker = Vector2::new(0.4,0.6);
|
||||
/// let convolve = convolve_valid(vec,ker);
|
||||
/// ```
|
||||
pub fn convolve_valid<N, D1, D2, S1, S2>(
|
||||
vector: Vector<N, D1, S1>,
|
||||
kernel: Vector<N, D2, S2>,
|
||||
) -> VectorN<N, DimSum<DimDiff<D1, D2>, U1>>
|
||||
where
|
||||
N: Real,
|
||||
D1: DimSub<D2>,
|
||||
D2: DimSub<D1, Output = DimDiff<D1, D2>>,
|
||||
DimDiff<D1, D2>: DimAdd<U1>,
|
||||
S1: Storage<N, D1>,
|
||||
S2: Storage<N, D2>,
|
||||
DimDiff<D1, D2>: DimName,
|
||||
DefaultAllocator: Allocator<N, DimSum<DimDiff<D1, D2>, U1>>
|
||||
{
|
||||
|
||||
let vec = vector.len();
|
||||
let ker = kernel.len();
|
||||
|
||||
@ -76,12 +115,10 @@ pub fn convolve_valid<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E
|
||||
if ker > vec {
|
||||
return convolve_valid(kernel, vector);
|
||||
}
|
||||
let result_len = vector.data.shape().0.sub(kernel.data.shape().0).add(U1);
|
||||
let mut conv = VectorN::zeros_generic(result_len, U1);
|
||||
|
||||
let newlen = vec - ker + 1;
|
||||
|
||||
let mut conv = DVector::<R>::zeros(newlen);
|
||||
|
||||
for i in 0..newlen {
|
||||
for i in 0..(vec - ker + 1) {
|
||||
for j in 0..ker {
|
||||
conv[i] += vector[i + j] * kernel[ker - j - 1];
|
||||
}
|
||||
@ -89,13 +126,38 @@ pub fn convolve_valid<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E
|
||||
conv
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns the convolution of the vector and a kernel
|
||||
/// The output convolution is the same size as vector, centered with respect to the ‘full’ output.
|
||||
///
|
||||
pub fn convolve_same<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>> {
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `vector` - A Vector with size > 0
|
||||
/// * `kernel` - A Vector with size > 0
|
||||
///
|
||||
/// # Note:
|
||||
/// This function is commutative. If kernel > vector,
|
||||
/// they will swap their roles as in
|
||||
/// (self, kernel) = (kernel,self)
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// let vec = Vector3::new(1.0,2.0,3.0);
|
||||
/// let ker = Vector2::new(0.4,0.6);
|
||||
/// let convolve = convolve_same(vec,ker);
|
||||
/// ```
|
||||
pub fn convolve_same<N, D1, D2, S1, S2>(
|
||||
vector: Vector<N, D1, S1>,
|
||||
kernel: Vector<N, D2, S2>,
|
||||
) -> VectorN<N, DimMaximum<D1, D2>>
|
||||
where
|
||||
N: Real,
|
||||
D1: DimMax<D2>,
|
||||
D2: DimMax<D1, Output = DimMaximum<D1, D2>>,
|
||||
S1: Storage<N, D1>,
|
||||
S2: Storage<N, D2>,
|
||||
DimMaximum<D1, D2>: Dim,
|
||||
DefaultAllocator: Allocator<N, DimMaximum<D1, D2>>,
|
||||
{
|
||||
let vec = vector.len();
|
||||
let ker = kernel.len();
|
||||
|
||||
@ -107,12 +169,13 @@ pub fn convolve_same<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E>
|
||||
return convolve_same(kernel, vector);
|
||||
}
|
||||
|
||||
let mut conv = DVector::<R>::zeros(vec);
|
||||
let result_len = vector.data.shape().0.max(kernel.data.shape().0);
|
||||
let mut conv = VectorN::zeros_generic(result_len, U1);
|
||||
|
||||
for i in 0..vec {
|
||||
for j in 0..ker {
|
||||
let val = if i + j < 1 || i + j >= vec + 1 {
|
||||
zero::<R>()
|
||||
zero::<N>()
|
||||
} else {
|
||||
vector[i + j - 1]
|
||||
};
|
||||
@ -121,4 +184,3 @@ pub fn convolve_same<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E>
|
||||
}
|
||||
conv
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
#[allow(unused_imports)] // remove after fixing unit test
|
||||
use na::linalg::{convolve_full,convolve_valid,convolve_same};
|
||||
use na::{Vector2,Vector4,DVector};
|
||||
#[allow(unused_imports)]
|
||||
use na::{Vector2,Vector3,Vector4,Vector5,DVector};
|
||||
|
||||
//
|
||||
// Should mimic calculations in Python's scipy library
|
||||
@ -10,40 +12,70 @@ use na::{Vector2,Vector4,DVector};
|
||||
// array([ 1, 4, 7, 10])
|
||||
#[test]
|
||||
fn convolve_same_check(){
|
||||
let vec = Vector4::new(1.0,2.0,3.0,4.0);
|
||||
let ker = Vector2::new(1.0,2.0);
|
||||
let vec_s = Vector4::new(1.0,2.0,3.0,4.0);
|
||||
let ker_s = Vector2::new(1.0,2.0);
|
||||
|
||||
let actual = DVector::from_vec(4, vec![1.0,4.0,7.0,10.0]);
|
||||
let actual_s = Vector4::from_vec(vec![1.0,4.0,7.0,10.0]);
|
||||
|
||||
let expected = convolve_same(vec,ker);
|
||||
let expected_s = convolve_same(vec_s,ker_s);
|
||||
let expected_s_r = convolve_same(ker_s,vec_s);
|
||||
|
||||
assert!(relative_eq!(actual, expected, epsilon = 1.0e-7));
|
||||
assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
|
||||
assert!(relative_eq!(actual_s, expected_s_r, epsilon = 1.0e-7));
|
||||
|
||||
let vec_d = DVector::from_vec(4,vec![1.0,2.0,3.0,4.0]);
|
||||
let ker_d = DVector::from_vec(2,vec![1.0,2.0]);
|
||||
|
||||
let actual_d = DVector::from_vec(4,vec![1.0,4.0,7.0,10.0]);
|
||||
|
||||
let expected_d = convolve_same(vec_d.clone(),ker_d.clone());
|
||||
let expected_d_r = convolve_same(ker_d,vec_d);
|
||||
|
||||
assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
|
||||
assert!(relative_eq!(actual_d, expected_d_r, epsilon = 1.0e-7));
|
||||
}
|
||||
|
||||
// >>> convolve([1,2,3,4],[1,2],"valid")
|
||||
// >>> convolve([1,2,3,4],[1,2],"full")
|
||||
// array([ 1, 4, 7, 10, 8])
|
||||
#[test]
|
||||
fn convolve_full_check(){
|
||||
let vec = Vector4::new(1.0,2.0,3.0,4.0);
|
||||
let ker = Vector2::new(1.0,2.0);
|
||||
let vec_s = Vector4::new(1.0,2.0,3.0,4.0);
|
||||
let ker_s = Vector2::new(1.0,2.0);
|
||||
|
||||
let actual = DVector::from_vec(5, vec![1.0,4.0,7.0,10.0,8.0]);
|
||||
let actual_s = Vector5::new(1.0,4.0,7.0,10.0,8.0);
|
||||
|
||||
let expected = convolve_full(vec,ker);
|
||||
let expected_s = convolve_full(vec_s,ker_s);
|
||||
let expected_s_r = convolve_full(ker_s,vec_s);
|
||||
|
||||
assert!(relative_eq!(actual, expected, epsilon = 1.0e-7));
|
||||
assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
|
||||
assert!(relative_eq!(actual_s, expected_s_r, epsilon = 1.0e-7));
|
||||
|
||||
let vec_d = DVector::from_vec(4,vec![1.0,2.0,3.0,4.0]);
|
||||
let ker_d = DVector::from_vec(2,vec![1.0,2.0]);
|
||||
|
||||
let actual_d = DVector::from_vec(5,vec![1.0,4.0,7.0,10.0,8.0]);
|
||||
|
||||
let expected_d = convolve_full(vec_d.clone(),ker_d.clone());
|
||||
let expected_d_r = convolve_full(ker_d,vec_d);
|
||||
|
||||
assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
|
||||
assert!(relative_eq!(actual_d, expected_d_r, epsilon = 1.0e-7));
|
||||
}
|
||||
|
||||
// >>> convolve([1,2,3,4],[1,2],"valid")
|
||||
// array([ 4, 7, 10])
|
||||
#[test]
|
||||
fn convolve_valid_check(){
|
||||
let vec = Vector4::new(1.0,2.0,3.0,4.0);
|
||||
let ker = Vector2::new(1.0,2.0);
|
||||
// #[test]
|
||||
// fn convolve_valid_check(){
|
||||
// let vec = Vector4::new(1.0,2.0,3.0,4.0);
|
||||
// let ker = Vector2::new(1.0,2.0);
|
||||
|
||||
let actual = DVector::from_vec(3, vec![4.0,7.0,10.0]);
|
||||
// let actual = Vector3::from_vec(vec![4.0,7.0,10.0]);
|
||||
|
||||
let expected = convolve_valid(vec,ker);
|
||||
// let expected1 = convolve_valid(vec, ker);
|
||||
// let expected2 = convolve_valid(ker, vec);
|
||||
|
||||
assert!(relative_eq!(actual, expected, epsilon = 1.0e-7));
|
||||
}
|
||||
|
||||
// assert!(relative_eq!(actual, expected1, epsilon = 1.0e-7));
|
||||
// assert!(relative_eq!(actual, expected2, epsilon = 1.0e-7));
|
||||
|
||||
// }
|
Loading…
Reference in New Issue
Block a user