From 5766364311053a348ed838cdb57263bbf57c32f4 Mon Sep 17 00:00:00 2001 From: Volodymyr Orlov Date: Tue, 31 Mar 2020 18:24:01 -0700 Subject: [PATCH] fix: minor refactoring --- src/algorithm/neighbour/bbd_tree.rs | 8 +++---- src/cluster/kmeans.rs | 2 +- src/decomposition/pca.rs | 4 ++-- src/ensemble/random_forest_classifier.rs | 2 +- src/ensemble/random_forest_regressor.rs | 2 +- src/linalg/evd.rs | 22 +++++++++---------- src/linalg/naive/dense_matrix.rs | 20 ++++++++--------- src/linalg/qr.rs | 6 ++--- src/linalg/svd.rs | 6 ++--- src/neighbors/knn.rs | 4 +--- .../first_order/gradient_descent.rs | 3 +-- src/optimization/first_order/lbfgs.rs | 6 ++--- src/optimization/first_order/mod.rs | 4 ++-- src/tree/decision_tree_classifier.rs | 6 ++--- src/tree/decision_tree_regressor.rs | 6 ++--- 15 files changed, 49 insertions(+), 52 deletions(-) diff --git a/src/algorithm/neighbour/bbd_tree.rs b/src/algorithm/neighbour/bbd_tree.rs index 39a27e9..d0cea26 100644 --- a/src/algorithm/neighbour/bbd_tree.rs +++ b/src/algorithm/neighbour/bbd_tree.rs @@ -5,14 +5,14 @@ use crate::linalg::Matrix; use crate::math::distance::euclidian; #[derive(Debug)] -pub struct BBDTree { +pub struct BBDTree { nodes: Vec>, index: Vec, root: usize } #[derive(Debug)] -struct BBDTreeNode { +struct BBDTreeNode { count: usize, index: usize, center: Vec, @@ -23,7 +23,7 @@ struct BBDTreeNode { upper: Option } -impl BBDTreeNode { +impl BBDTreeNode { fn new(d: usize) -> BBDTreeNode { BBDTreeNode { count: 0, @@ -38,7 +38,7 @@ impl BBDTreeNode { } } -impl BBDTree { +impl BBDTree { pub fn new>(data: &M) -> BBDTree { let nodes = Vec::new(); diff --git a/src/cluster/kmeans.rs b/src/cluster/kmeans.rs index 89ad5ef..7000405 100644 --- a/src/cluster/kmeans.rs +++ b/src/cluster/kmeans.rs @@ -56,7 +56,7 @@ impl Default for KMeansParameters { } } -impl KMeans{ +impl KMeans{ pub fn new>(data: &M, k: usize, parameters: KMeansParameters) -> KMeans { let bbd = BBDTree::new(data); diff --git a/src/decomposition/pca.rs b/src/decomposition/pca.rs index e8238f1..68d884c 100644 --- a/src/decomposition/pca.rs +++ b/src/decomposition/pca.rs @@ -3,7 +3,7 @@ use crate::math::num::FloatExt; use crate::linalg::{Matrix}; #[derive(Debug)] -pub struct PCA> { +pub struct PCA> { eigenvectors: M, eigenvalues: Vec, projection: M, @@ -24,7 +24,7 @@ impl Default for PCAParameters { } } -impl> PCA { +impl> PCA { pub fn new(data: &M, n_components: usize, parameters: PCAParameters) -> PCA { diff --git a/src/ensemble/random_forest_classifier.rs b/src/ensemble/random_forest_classifier.rs index e33741e..4a0835c 100644 --- a/src/ensemble/random_forest_classifier.rs +++ b/src/ensemble/random_forest_classifier.rs @@ -39,7 +39,7 @@ impl Default for RandomForestClassifierParameters { } } -impl RandomForestClassifier { +impl RandomForestClassifier { pub fn fit>(x: &M, y: &M::RowVector, parameters: RandomForestClassifierParameters) -> RandomForestClassifier { let (_, num_attributes) = x.shape(); diff --git a/src/ensemble/random_forest_regressor.rs b/src/ensemble/random_forest_regressor.rs index db16935..1d4dec6 100644 --- a/src/ensemble/random_forest_regressor.rs +++ b/src/ensemble/random_forest_regressor.rs @@ -36,7 +36,7 @@ impl Default for RandomForestRegressorParameters { } } -impl RandomForestRegressor { +impl RandomForestRegressor { pub fn fit>(x: &M, y: &M::RowVector, parameters: RandomForestRegressorParameters) -> RandomForestRegressor { let (n_rows, num_attributes) = x.shape(); diff --git a/src/linalg/evd.rs b/src/linalg/evd.rs index adbad8a..a74e186 100644 --- a/src/linalg/evd.rs +++ b/src/linalg/evd.rs @@ -6,13 +6,13 @@ use crate::math::num::FloatExt; use std::fmt::Debug; #[derive(Debug, Clone)] -pub struct EVD> { +pub struct EVD> { pub d: Vec, pub e: Vec, pub V: M } -impl> EVD { +impl> EVD { pub fn new(V: M, d: Vec, e: Vec) -> EVD { EVD { d: d, @@ -22,7 +22,7 @@ impl> EVD { } } -pub trait EVDDecomposableMatrix: BaseMatrix { +pub trait EVDDecomposableMatrix: BaseMatrix { fn evd(&self, symmetric: bool) -> EVD{ self.clone().evd_mut(symmetric) @@ -68,7 +68,7 @@ pub trait EVDDecomposableMatrix: BaseMatrix { } } -fn tred2>(V: &mut M, d: &mut Vec, e: &mut Vec) { +fn tred2>(V: &mut M, d: &mut Vec, e: &mut Vec) { let (n, _) = V.shape(); for i in 0..n { @@ -172,7 +172,7 @@ fn tred2>(V: &mut M, d: &mut Vec, e: &m e[0] = T::zero(); } -fn tql2>(V: &mut M, d: &mut Vec, e: &mut Vec) { +fn tql2>(V: &mut M, d: &mut Vec, e: &mut Vec) { let (n, _) = V.shape(); for i in 1..n { e[i - 1] = e[i]; @@ -288,7 +288,7 @@ fn tql2>(V: &mut M, d: &mut Vec, e: &mu } } -fn balance>(A: &mut M) -> Vec { +fn balance>(A: &mut M) -> Vec { let radix = T::two(); let sqrdx = radix * radix; @@ -341,7 +341,7 @@ fn balance>(A: &mut M) -> Vec { return scale; } -fn elmhes>(A: &mut M) -> Vec { +fn elmhes>(A: &mut M) -> Vec { let (n, _) = A.shape(); let mut perm = vec![0; n]; @@ -387,7 +387,7 @@ fn elmhes>(A: &mut M) -> Vec { return perm; } -fn eltran>(A: &M, V: &mut M, perm: &Vec) { +fn eltran>(A: &M, V: &mut M, perm: &Vec) { let (n, _) = A.shape(); for mp in (1..n - 1).rev() { for k in mp + 1..n { @@ -404,7 +404,7 @@ fn eltran>(A: &M, V: &mut M, perm: &Vec>(A: &mut M, V: &mut M, d: &mut Vec, e: &mut Vec) { +fn hqr2>(A: &mut M, V: &mut M, d: &mut Vec, e: &mut Vec) { let (n, _) = A.shape(); let mut z = T::zero(); let mut s = T::zero(); @@ -742,7 +742,7 @@ fn hqr2>(A: &mut M, V: &mut M, d: &mut Vec } } -fn balbak>(V: &mut M, scale: &Vec) { +fn balbak>(V: &mut M, scale: &Vec) { let (n, _) = V.shape(); for i in 0..n { for j in 0..n { @@ -751,7 +751,7 @@ fn balbak>(V: &mut M, scale: &Vec) { } } -fn sort>(d: &mut Vec, e: &mut Vec, V: &mut M) { +fn sort>(d: &mut Vec, e: &mut Vec, V: &mut M) { let n = d.len(); let mut temp = vec![T::zero(); n]; for j in 1..n { diff --git a/src/linalg/naive/dense_matrix.rs b/src/linalg/naive/dense_matrix.rs index 3be158e..dd38361 100644 --- a/src/linalg/naive/dense_matrix.rs +++ b/src/linalg/naive/dense_matrix.rs @@ -16,7 +16,7 @@ use crate::linalg::qr::QRDecomposableMatrix; use crate::math::num::FloatExt; #[derive(Debug, Clone)] -pub struct DenseMatrix { +pub struct DenseMatrix { ncols: usize, nrows: usize, @@ -24,7 +24,7 @@ pub struct DenseMatrix { } -impl fmt::Display for DenseMatrix { +impl fmt::Display for DenseMatrix { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let mut rows: Vec> = Vec::new(); for r in 0..self.nrows { @@ -34,7 +34,7 @@ impl fmt::Display for DenseMatrix { } } -impl DenseMatrix { +impl DenseMatrix { fn new(nrows: usize, ncols: usize, values: Vec) -> Self { DenseMatrix { @@ -182,15 +182,15 @@ impl Serialize for DenseMatrix { } } -impl SVDDecomposableMatrix for DenseMatrix {} +impl SVDDecomposableMatrix for DenseMatrix {} -impl EVDDecomposableMatrix for DenseMatrix {} +impl EVDDecomposableMatrix for DenseMatrix {} -impl QRDecomposableMatrix for DenseMatrix {} +impl QRDecomposableMatrix for DenseMatrix {} -impl Matrix for DenseMatrix {} +impl Matrix for DenseMatrix {} -impl PartialEq for DenseMatrix { +impl PartialEq for DenseMatrix { fn eq(&self, other: &Self) -> bool { if self.ncols != other.ncols || self.nrows != other.nrows { return false @@ -213,13 +213,13 @@ impl PartialEq for DenseMatrix { } } -impl Into> for DenseMatrix { +impl Into> for DenseMatrix { fn into(self) -> Vec { self.values } } -impl BaseMatrix for DenseMatrix { +impl BaseMatrix for DenseMatrix { type RowVector = Vec; diff --git a/src/linalg/qr.rs b/src/linalg/qr.rs index bdc1f1d..853e1a3 100644 --- a/src/linalg/qr.rs +++ b/src/linalg/qr.rs @@ -6,13 +6,13 @@ use crate::math::num::FloatExt; use crate::linalg::BaseMatrix; #[derive(Debug, Clone)] -pub struct QR> { +pub struct QR> { QR: M, tau: Vec, singular: bool } -impl> QR { +impl> QR { pub fn new(QR: M, tau: Vec) -> QR { let mut singular = false; @@ -112,7 +112,7 @@ impl> QR { } } -pub trait QRDecomposableMatrix: BaseMatrix { +pub trait QRDecomposableMatrix: BaseMatrix { fn qr(&self) -> QR { self.clone().qr_mut() diff --git a/src/linalg/svd.rs b/src/linalg/svd.rs index a975960..67298a5 100644 --- a/src/linalg/svd.rs +++ b/src/linalg/svd.rs @@ -5,7 +5,7 @@ use crate::math::num::FloatExt; use std::fmt::Debug; #[derive(Debug, Clone)] -pub struct SVD> { +pub struct SVD> { pub U: M, pub V: M, pub s: Vec, @@ -15,7 +15,7 @@ pub struct SVD> { tol: T } -pub trait SVDDecomposableMatrix: BaseMatrix { +pub trait SVDDecomposableMatrix: BaseMatrix { fn svd_solve_mut(self, b: Self) -> Self { self.svd_mut().solve(b) @@ -373,7 +373,7 @@ pub trait SVDDecomposableMatrix: BaseMatrix { } } -impl> SVD { +impl> SVD { pub fn new(U: M, V: M, s: Vec) -> SVD { let m = U.shape().0; let n = V.shape().0; diff --git a/src/neighbors/knn.rs b/src/neighbors/knn.rs index 070373b..3cb96eb 100644 --- a/src/neighbors/knn.rs +++ b/src/neighbors/knn.rs @@ -1,5 +1,3 @@ -use std::fmt::Debug; - use crate::math::num::FloatExt; use crate::linalg::{Matrix, row_iter}; use crate::algorithm::neighbour::{KNNAlgorithm, KNNAlgorithmName}; @@ -13,7 +11,7 @@ pub struct KNNClassifier<'a, T: FloatExt> { k: usize, } -impl<'a, T: FloatExt + Debug> KNNClassifier<'a, T> { +impl<'a, T: FloatExt> KNNClassifier<'a, T> { pub fn fit>(x: &M, y: &M::RowVector, k: usize, distance: &'a dyn Fn(&Vec, &Vec) -> T, algorithm: KNNAlgorithmName) -> KNNClassifier<'a, T> { diff --git a/src/optimization/first_order/gradient_descent.rs b/src/optimization/first_order/gradient_descent.rs index bf54cc5..b642534 100644 --- a/src/optimization/first_order/gradient_descent.rs +++ b/src/optimization/first_order/gradient_descent.rs @@ -1,5 +1,4 @@ use std::default::Default; -use std::fmt::Debug; use crate::math::num::FloatExt; use crate::linalg::Matrix; @@ -23,7 +22,7 @@ impl Default for GradientDescent { } } -impl FirstOrderOptimizer for GradientDescent +impl FirstOrderOptimizer for GradientDescent { fn optimize<'a, X: Matrix, LS: LineSearchMethod>(&self, f: &'a F, df: &'a DF, x0: &X, ls: &'a LS) -> OptimizerResult { diff --git a/src/optimization/first_order/lbfgs.rs b/src/optimization/first_order/lbfgs.rs index d2edc53..9b1605e 100644 --- a/src/optimization/first_order/lbfgs.rs +++ b/src/optimization/first_order/lbfgs.rs @@ -35,7 +35,7 @@ impl Default for LBFGS { } } -impl LBFGS { +impl LBFGS { fn two_loops>(&self, state: &mut LBFGSState) { @@ -169,7 +169,7 @@ impl LBFGS { } #[derive(Debug)] -struct LBFGSState> { +struct LBFGSState> { x: X, x_prev: X, x_f: T, @@ -189,7 +189,7 @@ struct LBFGSState> { alpha: T } -impl FirstOrderOptimizer for LBFGS { +impl FirstOrderOptimizer for LBFGS { fn optimize<'a, X: Matrix, LS: LineSearchMethod>(&self, f: &F, df: &'a DF, x0: &X, ls: &'a LS) -> OptimizerResult { diff --git a/src/optimization/first_order/mod.rs b/src/optimization/first_order/mod.rs index 499db21..fae47ea 100644 --- a/src/optimization/first_order/mod.rs +++ b/src/optimization/first_order/mod.rs @@ -9,12 +9,12 @@ use crate::linalg::Matrix; use crate::optimization::line_search::LineSearchMethod; use crate::optimization::{F, DF}; -pub trait FirstOrderOptimizer { +pub trait FirstOrderOptimizer { fn optimize<'a, X: Matrix, LS: LineSearchMethod>(&self, f: &F, df: &'a DF, x0: &X, ls: &'a LS) -> OptimizerResult; } #[derive(Debug, Clone)] -pub struct OptimizerResult> +pub struct OptimizerResult> { pub x: X, pub f_x: T, diff --git a/src/tree/decision_tree_classifier.rs b/src/tree/decision_tree_classifier.rs index 985196b..a5ff657 100644 --- a/src/tree/decision_tree_classifier.rs +++ b/src/tree/decision_tree_classifier.rs @@ -68,7 +68,7 @@ impl Node { } } -struct NodeVisitor<'a, T: FloatExt + Debug, M: Matrix> { +struct NodeVisitor<'a, T: FloatExt, M: Matrix> { x: &'a M, y: &'a Vec, node: usize, @@ -115,7 +115,7 @@ fn impurity(criterion: &SplitCriterion, count: &Vec, n: usiz return impurity; } -impl<'a, T: FloatExt + Debug, M: Matrix> NodeVisitor<'a, T, M> { +impl<'a, T: FloatExt, M: Matrix> NodeVisitor<'a, T, M> { fn new(node_id: usize, samples: Vec, order: &'a Vec>, x: &'a M, y: &'a Vec, level: u16) -> Self { NodeVisitor { @@ -147,7 +147,7 @@ pub(in crate) fn which_max(x: &Vec) -> usize { return which; } -impl DecisionTreeClassifier { +impl DecisionTreeClassifier { pub fn fit>(x: &M, y: &M::RowVector, parameters: DecisionTreeClassifierParameters) -> DecisionTreeClassifier { let (x_nrows, num_attributes) = x.shape(); diff --git a/src/tree/decision_tree_regressor.rs b/src/tree/decision_tree_regressor.rs index c347c93..a0c574a 100644 --- a/src/tree/decision_tree_regressor.rs +++ b/src/tree/decision_tree_regressor.rs @@ -56,7 +56,7 @@ impl Node { } } -struct NodeVisitor<'a, T: FloatExt + Debug, M: Matrix> { +struct NodeVisitor<'a, T: FloatExt, M: Matrix> { x: &'a M, y: &'a M, node: usize, @@ -67,7 +67,7 @@ struct NodeVisitor<'a, T: FloatExt + Debug, M: Matrix> { level: u16 } -impl<'a, T: FloatExt + Debug, M: Matrix> NodeVisitor<'a, T, M> { +impl<'a, T: FloatExt, M: Matrix> NodeVisitor<'a, T, M> { fn new(node_id: usize, samples: Vec, order: &'a Vec>, x: &'a M, y: &'a M, level: u16) -> Self { NodeVisitor { @@ -84,7 +84,7 @@ impl<'a, T: FloatExt + Debug, M: Matrix> NodeVisitor<'a, T, M> { } -impl DecisionTreeRegressor { +impl DecisionTreeRegressor { pub fn fit>(x: &M, y: &M::RowVector, parameters: DecisionTreeRegressorParameters) -> DecisionTreeRegressor { let (x_nrows, num_attributes) = x.shape();