SynAPSE  0.6.0
Synergistic Analysis of Phase Space Evolution
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
KNearestNeighbours.hh
1 #ifndef KNEARESTNEIGHBOURS_HH
2 #define KNEARESTNEIGHBOURS_HH
3 
4 // C++ includes
5 #include <iostream>
6 #include <cmath>
7 #include <vector>
8 #include <algorithm>
9 
10 // ROOT includes
11 #include "TMatrixD.h"
12 
13 // NanoFLANN includes
14 #include <nanoflann.hpp>
15 #include "KDTreeVectorOfVectorsAdaptor.h"
16 
17 // Additional includes
18 #include "Assert.hh"
19 #include "Statistics.hh"
20 
21 typedef KDTreeVectorOfVectorsAdaptor<std::vector<std::vector<double>>> KDTree;
22 
28  public:
31 
37  KNearestNeighbours(const std::vector<std::vector<double>>& points,
38  const size_t k,
39  bool rotate=false);
40 
43 
46 
51  double operator()(const std::vector<double>& v) const { return Evaluate(v); }
52 
57  double operator()(const double& v) const { return Evaluate(v); }
58 
63  double operator()(const double* v) const { return Evaluate(v); }
64 
67 
69  void Initialize();
70 
75  double Evaluate(const std::vector<double>& v) const;
76 
81  double Evaluate(const double& v) const;
82 
87  double Evaluate(const double* v) const;
88 
90  const size_t& GetDimension() const { return _dim; }
91 
93  double Distance(const std::vector<double>& point) const;
94 
96  double Volume(const std::vector<double>& point) const;
97 
99  double Density(const std::vector<double>& point) const;
100 
101  private:
102 
103  size_t _dim;
104  std::vector<std::vector<double>> _points;
105  size_t _k;
106  KDTree* _kdtree;
107  bool _rotate;
108  TMatrixD _metric;
109  double _scale;
110 };
111 
112 #endif
double Evaluate(const std::vector< double > &v) const
Performs the density estimation at an n-point v.
Definition: KNearestNeighbours.cc:108
const size_t & GetDimension() const
Returns the dimension of the space.
Definition: KNearestNeighbours.hh:90
void Initialize()
Checks that the input is sensible.
Definition: KNearestNeighbours.cc:54
~KNearestNeighbours()
Destructor.
Definition: KNearestNeighbours.cc:49
KNearestNeighbours & operator=(const KNearestNeighbours &knn)
Equality operator.
Definition: KNearestNeighbours.cc:25
TMatrixD _metric
Metric of the points.
Definition: KNearestNeighbours.hh:108
Set of functions that compose the Statistics package.
double Density(const std::vector< double > &point) const
Returns the density of points in the hypersphere that contains the k NN of a point.
Definition: KNearestNeighbours.cc:155
KNearestNeighbours()
Default constructor, initilizes everything to 0.
Definition: KNearestNeighbours.cc:3
double Volume(const std::vector< double > &point) const
Returns the volume of the hypersphere that contains the k NN of a point.
Definition: KNearestNeighbours.cc:147
size_t _dim
Dimension of the space.
Definition: KNearestNeighbours.hh:103
double operator()(const std::vector< double > &v) const
Overloaded function operator, calls Evaluate.
Definition: KNearestNeighbours.hh:51
KDTree * _kdtree
k-d Tree of the points to find NNs
Definition: KNearestNeighbours.hh:106
size_t _k
Number of neighbours to consider.
Definition: KNearestNeighbours.hh:105
Computes the k-Nearest Neighbour (kNN) density estimator of a set of points.
Definition: KNearestNeighbours.hh:27
double _scale
Scaling factor of the metric.
Definition: KNearestNeighbours.hh:109
double Distance(const std::vector< double > &point) const
Returns the distance to the k^th NN of a point.
Definition: KNearestNeighbours.cc:134
bool _rotate
Whether or not a rotated metric is used.
Definition: KNearestNeighbours.hh:107
double operator()(const double &v) const
Overloaded function operator, calls Evaluate.
Definition: KNearestNeighbours.hh:57
std::vector< std::vector< double > > _points
Cloud of points.
Definition: KNearestNeighbours.hh:104
double operator()(const double *v) const
Overloaded function operator, calls Evaluate.
Definition: KNearestNeighbours.hh:63