nn_rs/metrics/
euclidean_distance.rs

1use nalgebra as na;
2
3/// Calculate the euclidean distance between two vectors
4///
5/// # Parameters
6/// - a: first vector
7/// - b: second vector
8///
9/// # Return Values
10/// - the euclidean distance between a and b
11pub fn euclidean_distance(a: &na::DVector<f64>, b: &na::DVector<f64>) -> f64 {
12    assert_eq!(
13        a.shape(),
14        b.shape(),
15        "expected shape of a and b to be the same but got {:?} and {:?}",
16        a.shape(),
17        b.shape()
18    );
19    a.metric_distance(b)
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25    use na::dvector;
26
27    #[test]
28    /// test the euclidean distance function returns the correct results
29    fn test_euclidean_distance() {
30        assert_eq!(
31            euclidean_distance(&dvector!(1.0, 2.0, 3.0), &dvector!(1.0, 2.0, 3.0)),
32            0.0
33        );
34        assert_eq!(
35            euclidean_distance(&dvector!(1.0, 2.0, 3.0), &dvector!(4.0, 2.0, 7.0)),
36            5.0
37        );
38    }
39}