All 3 modes of direct done

This commit is contained in:
Nathan 2019-02-09 21:51:20 -06:00
parent 3d83860fad
commit d038ec7b73
1 changed files with 18 additions and 47 deletions

View File

@ -1,9 +1,8 @@
extern crate nalgebra as na; extern crate nalgebra as na;
use na::storage::Storage; use na::storage::Storage;
#[allow(unused_imports)]
use na::{ use na::{
DMatrix, DVector, Dim, Dynamic, Matrix, Matrix2x3, Real, VecStorage, Vector, Vector2, Vector3, convert, zero, DMatrix, DVector, Dim, Dynamic, Matrix, Matrix2x3, Real, VecStorage, Vector,
Vector4, U1, Vector2, Vector3, Vector4, Vector5, U1,
}; };
use std::cmp; use std::cmp;
@ -24,7 +23,7 @@ fn convolve_full<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E>>(
let mut conv = DVector::<R>::zeros(newlen); let mut conv = DVector::<R>::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 {
@ -47,6 +46,7 @@ fn convolve_valid<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E>>(
let vec = vector.len(); let vec = vector.len();
let ker = kernel.len(); let ker = kernel.len();
let newlen = vec - ker + 1; let newlen = vec - ker + 1;
let mut conv = DVector::<R>::zeros(newlen); let mut conv = DVector::<R>::zeros(newlen);
for i in 0..newlen { for i in 0..newlen {
@ -63,23 +63,18 @@ fn convolve_same<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E>>(
) -> Matrix<R, Dynamic, U1, VecStorage<R, Dynamic, U1>> { ) -> Matrix<R, Dynamic, U1, VecStorage<R, Dynamic, U1>> {
let vec = vector.len(); let vec = vector.len();
let ker = kernel.len(); let ker = kernel.len();
let newlen = vec + ker - 1;
let mut conv = DVector::<R>::zeros(newlen); let mut conv = DVector::<R>::zeros(vec);
for i in 0..newlen { for i in 0..vec {
// let u_i = cmp::max(0, i - k); for j in 0..ker {
// let u_f = cmp::min(i, v - 1); let val = if i + j < 1 || i + j >= vec + 1 {
zero::<R>()
// if u_i == u_f { } else {
// conv[i as usize] += vector[u_i as usize] * kernel[(i - u_i) as usize]; vector[i + j - 1]
// } else { };
// for u in u_i..(u_f + 1) { conv[i] += val * kernel[ker - j - 1];
// if i - u < k { }
// conv[i as usize] += vector[u as usize] * kernel[(i - u) as usize];
// }
// }
// }
} }
conv conv
} }
@ -89,10 +84,6 @@ fn convolve<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E>>(
kernel: Vector<R, E, Q>, kernel: Vector<R, E, Q>,
mode: Option<ConvolveMode>, mode: Option<ConvolveMode>,
) -> Matrix<R, Dynamic, U1, VecStorage<R, Dynamic, U1>> { ) -> Matrix<R, Dynamic, U1, VecStorage<R, Dynamic, U1>> {
//
// vector is the vector, Kervel is the kervel
// C is the returv vector
//
if kernel.len() > vector.len() { if kernel.len() > vector.len() {
return convolve(kernel, vector, mode); return convolve(kernel, vector, mode);
} }
@ -105,28 +96,8 @@ fn convolve<R: Real, D: Dim, E: Dim, S: Storage<R, D>, Q: Storage<R, E>>(
} }
fn main() { fn main() {
let v1 = Vector2::new(3.0,1.0); let v1 = Vector4::new(1.0, 2.0, 1.0, 0.0);
let v2 = Vector4::new(1.0,2.0,5.0,9.0); let v2 = Vector4::new(1.0, 2.0, 5.0, 9.0);
let x = convolve(v1,v2,Some(ConvolveMode::Valid)); let x = convolve(v1, v2, Some(ConvolveMode::Same));
println!("{:?}",x) println!("{:?}", x);
// let m = Matrix2x3::from_anti_diagonal_element(5.0);
// The two additional arguments represent the matrix dimensions.
// let dm = DMatrix::from_anti_diagonal_element(2, 3, 5.0);
let mut m = Matrix2x3::new(1.1, 1.2, 1.3,
2.1, 2.2, 2.3);
// assert!(m.m11 == 0.0 && m.m12 == 0.0 && m.m13 == 5.0 &&
// m.m21 == 0.0 && m.m22 == 5.0 && m.m23 == 0.0);
// assert!(dm[(0, 0)] == 0.0 && dm[(0, 1)] == 0.0 && dm[(0, 2)] == 5.0 &&
// dm[(1, 0)] == 0.0 && dm[(1, 1)] == 5.0 && dm[(1, 2)] == 0.0);
println!("m={:?}",m);
for i in 0..std::cmp::min(m.nrows(),m.ncols()) {
// for j in 0..3 {
println!("m({:?},{:?})={:?}",i,3-i-1,m[(i,3-i-1)]);
unsafe { println!("m({:?},{:?})={:?}",i,3-i-1,*m.get_unchecked_mut((i, 3-i-1))) }
// }
}
} }