Fix the return type of `convolve_same` to match the documentation.
This commit is contained in:
parent
ae4afa3d2c
commit
bb06701eff
|
@ -7,14 +7,14 @@ use crate::storage::Storage;
|
||||||
use crate::{zero, RealField, Vector, VectorN, U1};
|
use crate::{zero, RealField, Vector, VectorN, U1};
|
||||||
|
|
||||||
impl<N: RealField, D1: Dim, S1: Storage<N, D1>> Vector<N, D1, S1> {
|
impl<N: RealField, D1: Dim, S1: Storage<N, D1>> Vector<N, D1, S1> {
|
||||||
/// Returns the convolution of the target vector and a kernel
|
/// Returns the convolution of the target vector and a kernel.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `kernel` - A Vector with size > 0
|
/// * `kernel` - A Vector with size > 0
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// Inputs must statisfy `vector.len() >= kernel.len() > 0`.
|
/// Inputs must satisfy `vector.len() >= kernel.len() > 0`.
|
||||||
///
|
///
|
||||||
pub fn convolve_full<D2, S2>(
|
pub fn convolve_full<D2, S2>(
|
||||||
&self,
|
&self,
|
||||||
|
@ -53,7 +53,8 @@ impl<N: RealField, D1: Dim, S1: Storage<N, D1>> Vector<N, D1, S1> {
|
||||||
}
|
}
|
||||||
conv
|
conv
|
||||||
}
|
}
|
||||||
/// Returns the convolution of the target vector and a kernel
|
/// Returns the convolution of the target vector and a kernel.
|
||||||
|
///
|
||||||
/// 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.
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
|
@ -61,10 +62,9 @@ impl<N: RealField, D1: Dim, S1: Storage<N, D1>> Vector<N, D1, S1> {
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// Inputs must statisfy `self.len() >= kernel.len() > 0`.
|
/// Inputs must satisfy `self.len() >= kernel.len() > 0`.
|
||||||
///
|
///
|
||||||
pub fn convolve_valid<D2, S2>(&self, kernel: Vector<N, D2, S2>,
|
pub fn convolve_valid<D2, S2>(&self, kernel: Vector<N, D2, S2>) -> VectorN<N, DimDiff<DimSum<D1, U1>, D2>>
|
||||||
) -> VectorN<N, DimDiff<DimSum<D1, U1>, D2>>
|
|
||||||
where
|
where
|
||||||
D1: DimAdd<U1>,
|
D1: DimAdd<U1>,
|
||||||
D2: Dim,
|
D2: Dim,
|
||||||
|
@ -90,20 +90,20 @@ impl<N: RealField, D1: Dim, S1: Storage<N, D1>> Vector<N, D1, S1> {
|
||||||
conv
|
conv
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the convolution of the targetvector and a kernel
|
/// Returns the convolution of the target vector and a kernel.
|
||||||
|
///
|
||||||
/// The output convolution is the same size as vector, centered with respect to the ‘full’ output.
|
/// The output convolution is the same size as vector, centered with respect to the ‘full’ output.
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `kernel` - A Vector with size > 0
|
/// * `kernel` - A Vector with size > 0
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// Inputs must statisfy `self.len() >= kernel.len() > 0`.
|
/// Inputs must satisfy `self.len() >= kernel.len() > 0`.
|
||||||
pub fn convolve_same<D2, S2>(&self, kernel: Vector<N, D2, S2>) -> VectorN<N, DimMaximum<D1, D2>>
|
pub fn convolve_same<D2, S2>(&self, kernel: Vector<N, D2, S2>) -> VectorN<N, D1>
|
||||||
where
|
where
|
||||||
D1: DimMax<D2>,
|
D2: Dim,
|
||||||
D2: DimMax<D1, Output = DimMaximum<D1, D2>>,
|
|
||||||
S2: Storage<N, D2>,
|
S2: Storage<N, D2>,
|
||||||
DefaultAllocator: Allocator<N, DimMaximum<D1, D2>>,
|
DefaultAllocator: Allocator<N, D1>,
|
||||||
{
|
{
|
||||||
let vec = self.len();
|
let vec = self.len();
|
||||||
let ker = kernel.len();
|
let ker = kernel.len();
|
||||||
|
@ -112,8 +112,7 @@ impl<N: RealField, D1: Dim, S1: Storage<N, D1>> Vector<N, D1, S1> {
|
||||||
panic!("convolve_same expects `self.len() >= kernel.len() > 0`, received {} and {} respectively.",vec,ker);
|
panic!("convolve_same expects `self.len() >= kernel.len() > 0`, received {} and {} respectively.",vec,ker);
|
||||||
}
|
}
|
||||||
|
|
||||||
let result_len = self.data.shape().0.max(kernel.data.shape().0);
|
let mut conv = VectorN::zeros_generic(self.data.shape().0, U1);
|
||||||
let mut conv = VectorN::zeros_generic(result_len, U1);
|
|
||||||
|
|
||||||
for i in 0..vec {
|
for i in 0..vec {
|
||||||
for j in 0..ker {
|
for j in 0..ker {
|
||||||
|
@ -125,6 +124,7 @@ impl<N: RealField, D1: Dim, S1: Storage<N, D1>> Vector<N, D1, S1> {
|
||||||
conv[i] += val * kernel[ker - j - 1];
|
conv[i] += val * kernel[ker - j - 1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
conv
|
conv
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,14 +11,14 @@ use std::panic;
|
||||||
#[test]
|
#[test]
|
||||||
fn convolve_same_check(){
|
fn convolve_same_check(){
|
||||||
// Static Tests
|
// Static Tests
|
||||||
let actual_s = Vector4::from_vec(vec![1.0,4.0,7.0,10.0]);
|
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));
|
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));
|
assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
|
||||||
|
|
||||||
// Dynamic Tests
|
// Dynamic Tests
|
||||||
let actual_d = DVector::from_vec(vec![1.0,4.0,7.0,10.0]);
|
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]));
|
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));
|
assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
|
||||||
|
|
||||||
|
@ -26,19 +26,19 @@ fn convolve_same_check(){
|
||||||
// These really only apply to dynamic sized vectors
|
// These really only apply to dynamic sized vectors
|
||||||
assert!(
|
assert!(
|
||||||
panic::catch_unwind(|| {
|
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]));
|
DVector::from_vec(vec![1.0, 2.0]).convolve_same(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
|
||||||
}).is_err()
|
}).is_err()
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
panic::catch_unwind(|| {
|
panic::catch_unwind(|| {
|
||||||
DVector::<f32>::from_vec(vec![]).convolve_same(DVector::from_vec(vec![1.0,2.0,3.0,4.0]));
|
DVector::<f32>::from_vec(vec![]).convolve_same(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
|
||||||
}).is_err()
|
}).is_err()
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
panic::catch_unwind(|| {
|
panic::catch_unwind(|| {
|
||||||
DVector::from_vec(vec![1.0,2.0,3.0,4.0]).convolve_same(DVector::<f32>::from_vec(vec![]));
|
DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]).convolve_same(DVector::<f32>::from_vec(vec![]));
|
||||||
}).is_err()
|
}).is_err()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -48,14 +48,14 @@ fn convolve_same_check(){
|
||||||
#[test]
|
#[test]
|
||||||
fn convolve_full_check(){
|
fn convolve_full_check(){
|
||||||
// Static Tests
|
// Static Tests
|
||||||
let actual_s = Vector5::new(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_s = Vector4::new(1.0,2.0,3.0,4.0).convolve_full(Vector2::new(1.0,2.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));
|
assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
|
||||||
|
|
||||||
// Dynamic Tests
|
// Dynamic Tests
|
||||||
let actual_d = DVector::from_vec(vec![1.0,4.0,7.0,10.0,8.0]);
|
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]));
|
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));
|
assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
|
||||||
|
|
||||||
|
@ -63,36 +63,36 @@ fn convolve_full_check(){
|
||||||
// These really only apply to dynamic sized vectors
|
// These really only apply to dynamic sized vectors
|
||||||
assert!(
|
assert!(
|
||||||
panic::catch_unwind(|| {
|
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]));
|
DVector::from_vec(vec![1.0, 2.0] ).convolve_full(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0] ));
|
||||||
}).is_err()
|
}).is_err()
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
panic::catch_unwind(|| {
|
panic::catch_unwind(|| {
|
||||||
DVector::<f32>::from_vec(vec![]).convolve_full(DVector::from_vec(vec![1.0,2.0,3.0,4.0]));
|
DVector::<f32>::from_vec(vec![]).convolve_full(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0] ));
|
||||||
}).is_err()
|
}).is_err()
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
panic::catch_unwind(|| {
|
panic::catch_unwind(|| {
|
||||||
DVector::from_vec(vec![1.0,2.0,3.0,4.0]).convolve_full(DVector::<f32>::from_vec(vec![]));
|
DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0] ).convolve_full(DVector::<f32>::from_vec(vec![]));
|
||||||
}).is_err()
|
}).is_err()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// >>> convolve([1,2,3,4],[1,2],"valid")
|
// >>> convolve([1, 2, 3, 4],[1, 2],"valid")
|
||||||
// array([ 4, 7, 10])
|
// array([4, 7, 10])
|
||||||
#[test]
|
#[test]
|
||||||
fn convolve_valid_check(){
|
fn convolve_valid_check(){
|
||||||
// Static Tests
|
// Static Tests
|
||||||
let actual_s = Vector3::from_vec(vec![4.0,7.0,10.0]);
|
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));
|
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));
|
assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
|
||||||
|
|
||||||
// Dynamic Tests
|
// Dynamic Tests
|
||||||
let actual_d = DVector::from_vec(vec![4.0,7.0,10.0]);
|
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]));
|
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));
|
assert!(relative_eq!(actual_d, expected_d, epsilon = 1.0e-7));
|
||||||
|
|
||||||
|
@ -100,19 +100,19 @@ fn convolve_valid_check(){
|
||||||
// These really only apply to dynamic sized vectors
|
// These really only apply to dynamic sized vectors
|
||||||
assert!(
|
assert!(
|
||||||
panic::catch_unwind(|| {
|
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]));
|
DVector::from_vec(vec![1.0, 2.0]).convolve_valid(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
|
||||||
}).is_err()
|
}).is_err()
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
panic::catch_unwind(|| {
|
panic::catch_unwind(|| {
|
||||||
DVector::<f32>::from_vec(vec![]).convolve_valid(DVector::from_vec(vec![1.0,2.0,3.0,4.0]));
|
DVector::<f32>::from_vec(vec![]).convolve_valid(DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]));
|
||||||
}).is_err()
|
}).is_err()
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
panic::catch_unwind(|| {
|
panic::catch_unwind(|| {
|
||||||
DVector::from_vec(vec![1.0,2.0,3.0,4.0]).convolve_valid(DVector::<f32>::from_vec(vec![]));
|
DVector::from_vec(vec![1.0, 2.0, 3.0, 4.0]).convolve_valid(DVector::<f32>::from_vec(vec![]));
|
||||||
}).is_err()
|
}).is_err()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue