From eb4b49d55274ec4a1c2af978a9545e0ef35d53ac Mon Sep 17 00:00:00 2001 From: Chris McComb Date: Fri, 12 Aug 2022 17:38:13 -0400 Subject: [PATCH] Added additional doctest and fixed indices (#141) --- src/algorithm/neighbour/bbd_tree.rs | 2 +- src/linalg/evd.rs | 19 ++++++++++++++++--- src/optimization/mod.rs | 2 +- src/svm/svr.rs | 2 +- src/tree/decision_tree_classifier.rs | 4 ++-- src/tree/decision_tree_regressor.rs | 2 +- 6 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/algorithm/neighbour/bbd_tree.rs b/src/algorithm/neighbour/bbd_tree.rs index 293a822..93ea050 100644 --- a/src/algorithm/neighbour/bbd_tree.rs +++ b/src/algorithm/neighbour/bbd_tree.rs @@ -59,7 +59,7 @@ impl BBDTree { tree } - pub(in crate) fn clustering( + pub(crate) fn clustering( &self, centroids: &[Vec], sums: &mut Vec>, diff --git a/src/linalg/evd.rs b/src/linalg/evd.rs index bf195a0..fdca1fb 100644 --- a/src/linalg/evd.rs +++ b/src/linalg/evd.rs @@ -25,6 +25,19 @@ //! let eigenvectors: DenseMatrix = evd.V; //! let eigenvalues: Vec = evd.d; //! ``` +//! ``` +//! use smartcore::linalg::naive::dense_matrix::*; +//! use smartcore::linalg::evd::*; +//! +//! let A = DenseMatrix::from_2d_array(&[ +//! &[-5.0, 2.0], +//! &[-7.0, 4.0], +//! ]); +//! +//! let evd = A.evd(false).unwrap(); +//! let eigenvectors: DenseMatrix = evd.V; +//! let eigenvalues: Vec = evd.d; +//! ``` //! //! ## References: //! * ["Numerical Recipes: The Art of Scientific Computing", Press W.H., Teukolsky S.A., Vetterling W.T, Flannery B.P, 3rd ed., Section 11 Eigensystems](http://numerical.recipes/) @@ -799,10 +812,10 @@ fn sort>(d: &mut [T], e: &mut [T], V: &mut M) { } i -= 1; } - d[i as usize + 1] = real; - e[i as usize + 1] = img; + d[(i + 1) as usize] = real; + e[(i + 1) as usize] = img; for (k, temp_k) in temp.iter().enumerate().take(n) { - V.set(k, i as usize + 1, *temp_k); + V.set(k, (i + 1) as usize, *temp_k); } } } diff --git a/src/optimization/mod.rs b/src/optimization/mod.rs index b0be9d6..127b534 100644 --- a/src/optimization/mod.rs +++ b/src/optimization/mod.rs @@ -5,7 +5,7 @@ pub type F<'a, T, X> = dyn for<'b> Fn(&'b X) -> T + 'a; pub type DF<'a, X> = dyn for<'b> Fn(&'b mut X, &'b X) + 'a; #[allow(clippy::upper_case_acronyms)] -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub enum FunctionOrder { SECOND, THIRD, diff --git a/src/svm/svr.rs b/src/svm/svr.rs index 3257111..18c73d1 100644 --- a/src/svm/svr.rs +++ b/src/svm/svr.rs @@ -242,7 +242,7 @@ impl, K: Kernel> SVR { Ok(y_hat) } - pub(in crate) fn predict_for_row(&self, x: M::RowVector) -> T { + pub(crate) fn predict_for_row(&self, x: M::RowVector) -> T { let mut f = self.b; for i in 0..self.instances.len() { diff --git a/src/tree/decision_tree_classifier.rs b/src/tree/decision_tree_classifier.rs index d86f59a..35889e4 100644 --- a/src/tree/decision_tree_classifier.rs +++ b/src/tree/decision_tree_classifier.rs @@ -285,7 +285,7 @@ impl<'a, T: RealNumber, M: Matrix> NodeVisitor<'a, T, M> { } } -pub(in crate) fn which_max(x: &[usize]) -> usize { +pub(crate) fn which_max(x: &[usize]) -> usize { let mut m = x[0]; let mut which = 0; @@ -421,7 +421,7 @@ impl DecisionTreeClassifier { Ok(result.to_row_vector()) } - pub(in crate) fn predict_for_row>(&self, x: &M, row: usize) -> usize { + pub(crate) fn predict_for_row>(&self, x: &M, row: usize) -> usize { let mut result = 0; let mut queue: LinkedList = LinkedList::new(); diff --git a/src/tree/decision_tree_regressor.rs b/src/tree/decision_tree_regressor.rs index 94fa0f8..25f5e7e 100644 --- a/src/tree/decision_tree_regressor.rs +++ b/src/tree/decision_tree_regressor.rs @@ -321,7 +321,7 @@ impl DecisionTreeRegressor { Ok(result.to_row_vector()) } - pub(in crate) fn predict_for_row>(&self, x: &M, row: usize) -> T { + pub(crate) fn predict_for_row>(&self, x: &M, row: usize) -> T { let mut result = T::zero(); let mut queue: LinkedList = LinkedList::new();