1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//! Calculating Euclidean distances
use ordered_float::OrderedFloat;
/// Return the Euclidean pairwise distances between rows, in
/// a square vec-of-vec matrix.
pub fn rowwise_distances( mat: Vec<Vec<f64>> ) -> Vec< Vec< OrderedFloat<f64> > > {
let nrows = mat.len();
let mut dist = vec![ vec![ OrderedFloat(0f64);nrows]; nrows ];
let _l: f64;
for rowind in 0 .. nrows {
let rowvec = &mat[rowind];
for colind in rowind .. nrows {
let colvec = &mat[colind];
let l = rowvec
.iter().zip( colvec.iter() )
.map(|(x,y)| (x-y)*(x-y) )
.sum::<f64>()
.sqrt();
let l = OrderedFloat(l);
dist[ rowind ][ colind ] = l;
dist[ colind ][ rowind ] = l;
}
}
dist
}