Convolutions for Integers + tests

This commit is contained in:
Marc Haubenstock 2019-08-05 20:59:11 +02:00
parent f7e01f0641
commit 98dbac7563
2 changed files with 23 additions and 7 deletions

View File

@ -4,10 +4,11 @@ use crate::base::allocator::Allocator;
use crate::base::default_allocator::DefaultAllocator; use crate::base::default_allocator::DefaultAllocator;
use crate::base::dimension::{Dim, DimAdd, DimDiff, DimSub, DimSum, DimName}; use crate::base::dimension::{Dim, DimAdd, DimDiff, DimSub, DimSum, DimName};
use crate::storage::Storage; use crate::storage::Storage;
use crate::{zero, RealField, Vector, VectorN, U1, Matrix, MatrixMN, DMatrix}; use crate::{RealField, Vector, VectorN, U1, Matrix, MatrixMN, DMatrix, Scalar, zero};
use crate::alga::general::Field; use crate::alga::general::{ClosedMul, ClosedAdd, Identity, Additive};
use crate::num::Zero;
impl<N: RealField, D1: Dim, S1: Storage<N, D1>> Vector<N, D1, S1> { impl<N: Scalar + ClosedMul<N> + ClosedAdd<N> + Zero + Identity<Additive>, D1: Dim, S1: Storage<N, D1>> Vector<N, D1, S1> {
/// Returns the convolution of the target vector and a kernel.s /// Returns the convolution of the target vector and a kernel.s
/// ///
/// # Arguments /// # Arguments
@ -130,7 +131,7 @@ impl<N: RealField, D1: Dim, S1: Storage<N, D1>> Vector<N, D1, S1> {
} }
} }
impl<N: RealField> DMatrix<N> { impl<N: Scalar + ClosedMul<N> + ClosedAdd<N> + Zero + Identity<Additive>> DMatrix<N> {
/// Returns the convolution of the target vector and a kernel. /// Returns the convolution of the target vector and a kernel.
/// ///
/// # Arguments /// # Arguments
@ -164,7 +165,7 @@ impl<N: RealField> DMatrix<N> {
} }
impl<N: RealField, R1: Dim + DimName, C1: Dim + DimName> MatrixMN<N, R1, C1> where DefaultAllocator: Allocator<N, R1, C1> { impl<N: Scalar + ClosedMul<N> + ClosedAdd<N> + Zero + Identity<Additive>, R1: Dim + DimName, C1: Dim + DimName> MatrixMN<N, R1, C1> where DefaultAllocator: Allocator<N, R1, C1> {
/// Returns the convolution of the target vector and a kernel. /// Returns the convolution of the target vector and a kernel.
/// ///
/// # Arguments /// # Arguments
@ -200,7 +201,7 @@ impl<N: RealField, R1: Dim + DimName, C1: Dim + DimName> MatrixMN<N, R1, C1> whe
fn convolve<N, R1, C1, R2, C2, S2>(mat: &MatrixMN<N,R1,C1>, kernel: &Matrix<N, R2, C2, S2>, target: &mut MatrixMN<N,R1,C1>, mat_rows: i32, mat_cols: i32) fn convolve<N, R1, C1, R2, C2, S2>(mat: &MatrixMN<N,R1,C1>, kernel: &Matrix<N, R2, C2, S2>, target: &mut MatrixMN<N,R1,C1>, mat_rows: i32, mat_cols: i32)
where where
N: RealField, N: Scalar + ClosedMul<N> + ClosedAdd<N> + Zero + Identity<Additive>,
R1: Dim, R1: Dim,
C1: Dim, C1: Dim,
R2: Dim, R2: Dim,

View File

@ -16,6 +16,11 @@ fn convolve_same_check(){
assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7)); assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
let actual_s_int = Vector4::new(1, 4, 7, 10);
let expected_s_int = Vector4::new(1, 2, 3, 4).convolve_same(Vector2::new(1, 2));
assert_eq!(actual_s_int, expected_s_int);
// 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]));
@ -51,7 +56,12 @@ fn convolve_full_check(){
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));
let actual_s_int = Vector5::new(1, 4, 7, 10, 8);
let expected_s_int = Vector4::new(1, 2, 3, 4).convolve_full(Vector2::new(1, 2));
assert_eq!(actual_s_int, expected_s_int);
// 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]);
@ -141,6 +151,11 @@ fn convolve_same_mat_check(){
assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7)); assert!(relative_eq!(actual_s, expected_s, epsilon = 1.0e-7));
let actual_s_int = Matrix5::from_vec( vec![3,4,4,4,3,4,5,5,5,4,4,5,5,5,4,4,5,5,5,4,3,4,4,4,3]);
let expected_s_int = Matrix5::from_element(1).smat_convolve_full(Matrix3::from_vec(vec![0,1,0,1,1,1,0,1,0]));
assert_eq!(actual_s_int, expected_s_int);
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 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).dmat_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])); let expected_d = DMatrix::from_element(5,5,1.0).dmat_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]));