Skip to content

Commit 5568b80

Browse files
committed
Add test for noramlizers
1 parent b09146d commit 5568b80

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/util.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ use num_traits::Float;
66
use super::vector::*;
77
use std::ops::Div;
88

9+
/// construct matrix from diag
10+
pub fn from_diag<A>(d: &[A]) -> Array2<A>
11+
where A: LinalgScalar
12+
{
13+
let n = d.len();
14+
let mut e = Array::zeros((n, n));
15+
for i in 0..n {
16+
e[(i, i)] = d[i];
17+
}
18+
e
19+
}
20+
921
/// stack vectors into matrix horizontally
1022
pub fn hstack<A, S>(xs: &[ArrayBase<S, Ix1>]) -> Result<Array<A, Ix2>, ShapeError>
1123
where A: LinalgScalar,

tests/normalize.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
include!("header.rs");
2+
3+
#[test]
4+
fn n_columns() {
5+
let a = random_owned(3, 2, true);
6+
let (n, v) = normalize_columns(&a);
7+
all_close_l2(&n.dot(&from_diag(&v)), &a, 1e-7).unwrap();
8+
}
9+
10+
#[test]
11+
fn n_rows() {
12+
let a = random_owned(3, 2, true);
13+
let (v, n) = normalize_rows(&a);
14+
all_close_l2(&from_diag(&v).dot(&n), &a, 1e-7).unwrap();
15+
}

0 commit comments

Comments
 (0)