From 799c8a5261fa4a45d52104ce9cd943150f46192d Mon Sep 17 00:00:00 2001 From: "Lorenzo (Mec-iS)" Date: Wed, 24 Aug 2022 11:01:33 +0100 Subject: [PATCH 1/4] Add example --- src/algorithm/neighbour/fastpair.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/algorithm/neighbour/fastpair.rs b/src/algorithm/neighbour/fastpair.rs index dfc6f586..dcb616d1 100644 --- a/src/algorithm/neighbour/fastpair.rs +++ b/src/algorithm/neighbour/fastpair.rs @@ -1,7 +1,7 @@ #![allow(non_snake_case)] use itertools::Itertools; /// -/// FastPair: Data-structure for the dynamic closest-pair problem. +/// # FastPair: Data-structure for the dynamic closest-pair problem. /// /// Reference: /// Eppstein, David: Fast hierarchical clustering and other applications of @@ -16,14 +16,25 @@ use crate::math::distance::euclidian::Euclidian; use crate::math::num::RealNumber; /// -/// FastPair -/// -/// Ported from Python implementation: +/// Inspired by Python implementation: /// /// MIT License (MIT) Copyright (c) 2016 Carson Farmer /// /// affinity used is Euclidean so to allow linkage with single, ward, complete and average /// +/// Example: +/// ``` +/// let x = DenseMatrix::::from_2d_array(&[ +/// &[5.1, 3.5, 1.4, 0.2], +/// &[4.9, 3.0, 1.4, 0.2], +/// &[4.7, 3.2, 1.3, 0.2], +/// &[4.6, 3.1, 1.5, 0.2], +/// &[5.0, 3.6, 1.4, 0.2], +/// &[5.4, 3.9, 1.7, 0.4], +// ]); +// let fastpair = FastPair::new(&x); +// let closest_pair: PairwiseDistance = fastpair.unwrap().closest_pair(); +/// ``` #[derive(Debug, Clone)] pub struct FastPair<'a, T: RealNumber, M: Matrix> { /// initial matrix From ffd9de2058dacb0486f75b6afac55cfa4ba92d66 Mon Sep 17 00:00:00 2001 From: "Lorenzo (Mec-iS)" Date: Wed, 24 Aug 2022 11:09:03 +0100 Subject: [PATCH 2/4] Move to top --- src/algorithm/neighbour/fastpair.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/algorithm/neighbour/fastpair.rs b/src/algorithm/neighbour/fastpair.rs index dcb616d1..a0c637a2 100644 --- a/src/algorithm/neighbour/fastpair.rs +++ b/src/algorithm/neighbour/fastpair.rs @@ -7,6 +7,21 @@ use itertools::Itertools; /// Eppstein, David: Fast hierarchical clustering and other applications of /// dynamic closest pairs. Journal of Experimental Algorithmics 5 (2000) 1. /// +/// Example: +/// ``` +/// let x = DenseMatrix::::from_2d_array(&[ +/// &[5.1, 3.5, 1.4, 0.2], +/// &[4.9, 3.0, 1.4, 0.2], +/// &[4.7, 3.2, 1.3, 0.2], +/// &[4.6, 3.1, 1.5, 0.2], +/// &[5.0, 3.6, 1.4, 0.2], +/// &[5.4, 3.9, 1.7, 0.4], +/// ]); +/// let fastpair = FastPair::new(&x); +/// let closest_pair: PairwiseDistance = fastpair.unwrap().closest_pair(); +/// ``` +/// +/// use std::collections::HashMap; use crate::algorithm::neighbour::distances::PairwiseDistance; @@ -22,19 +37,6 @@ use crate::math::num::RealNumber; /// /// affinity used is Euclidean so to allow linkage with single, ward, complete and average /// -/// Example: -/// ``` -/// let x = DenseMatrix::::from_2d_array(&[ -/// &[5.1, 3.5, 1.4, 0.2], -/// &[4.9, 3.0, 1.4, 0.2], -/// &[4.7, 3.2, 1.3, 0.2], -/// &[4.6, 3.1, 1.5, 0.2], -/// &[5.0, 3.6, 1.4, 0.2], -/// &[5.4, 3.9, 1.7, 0.4], -// ]); -// let fastpair = FastPair::new(&x); -// let closest_pair: PairwiseDistance = fastpair.unwrap().closest_pair(); -/// ``` #[derive(Debug, Clone)] pub struct FastPair<'a, T: RealNumber, M: Matrix> { /// initial matrix From d719c28bc2e4c8f4605454f65cb7e27043a87dc6 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Wed, 24 Aug 2022 11:20:21 +0100 Subject: [PATCH 3/4] Add imports to example --- src/algorithm/neighbour/fastpair.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/algorithm/neighbour/fastpair.rs b/src/algorithm/neighbour/fastpair.rs index a0c637a2..616651b1 100644 --- a/src/algorithm/neighbour/fastpair.rs +++ b/src/algorithm/neighbour/fastpair.rs @@ -9,6 +9,9 @@ use itertools::Itertools; /// /// Example: /// ``` +/// use crate::algorithm::neighbour::distances::PairwiseDistance; +/// use crate::linalg::Matrix; +/// use crate::algorithm::neighbour::fastpair::FastPair /// let x = DenseMatrix::::from_2d_array(&[ /// &[5.1, 3.5, 1.4, 0.2], /// &[4.9, 3.0, 1.4, 0.2], From 059fa7e6244faa72142c89889ab5f4e08ef078c8 Mon Sep 17 00:00:00 2001 From: "Lorenzo (Mec-iS)" Date: Wed, 24 Aug 2022 11:38:53 +0100 Subject: [PATCH 4/4] Fix imports --- src/algorithm/neighbour/fastpair.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/algorithm/neighbour/fastpair.rs b/src/algorithm/neighbour/fastpair.rs index 616651b1..e14c2b35 100644 --- a/src/algorithm/neighbour/fastpair.rs +++ b/src/algorithm/neighbour/fastpair.rs @@ -9,9 +9,9 @@ use itertools::Itertools; /// /// Example: /// ``` -/// use crate::algorithm::neighbour::distances::PairwiseDistance; -/// use crate::linalg::Matrix; -/// use crate::algorithm::neighbour::fastpair::FastPair +/// use smartcore::algorithm::neighbour::distances::PairwiseDistance; +/// use smartcore::linalg::naive::dense_matrix::DenseMatrix; +/// use smartcore::algorithm::neighbour::fastpair::FastPair; /// let x = DenseMatrix::::from_2d_array(&[ /// &[5.1, 3.5, 1.4, 0.2], /// &[4.9, 3.0, 1.4, 0.2], @@ -21,7 +21,7 @@ use itertools::Itertools; /// &[5.4, 3.9, 1.7, 0.4], /// ]); /// let fastpair = FastPair::new(&x); -/// let closest_pair: PairwiseDistance = fastpair.unwrap().closest_pair(); +/// let closest_pair: PairwiseDistance = fastpair.unwrap().closest_pair(); /// ``` /// ///