fgt
C++ library for fast Gauss transforms
fgt.hpp
Go to the documentation of this file.
1 // fgt — fast Gauss transforms
2 // Copyright (C) 2016 Peter J. Gadomski <pete.gadomski@gmail.com>
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 
23 
24 #pragma once
25 
26 #include <cstddef>
27 #include <memory>
28 
29 #ifdef _MSC_VER
30 #pragma warning(push)
31 #pragma warning(disable : 4365) // unsigned/signed
32 #pragma warning(disable : 5031) // pragma warning mismatch
33 #pragma warning(disable : 4820) // padding
34 #pragma warning(disable : 4626) // assignment operator deleted
35 #pragma warning(disable : 5027) // move assignment operator deleted
36 #endif
37 #include <Eigen/Core>
38 #ifdef _MSC_VER
39 #pragma warning(pop)
40 #endif
41 
43 namespace fgt {
44 
46 class fgt_error : public std::runtime_error {
47 public:
48  explicit fgt_error(const std::string what_arg)
49  : std::runtime_error(what_arg) {}
50 };
51 
56 class ifgt_no_clusters : public fgt_error {
57 public:
59  : fgt_error("IFGT decided that it didn't need any clusters. These "
60  "parameters cannot be used for IFGT, try another method "
61  "instead.") {}
62 };
63 
65 typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
67 
72 typedef Eigen::Ref<const Matrix> MatrixRef;
73 
75 typedef Eigen::VectorXd Vector;
76 
78 typedef Eigen::Ref<const Vector> VectorRef;
79 
81 const char* version();
82 
84 bool with_openmp();
85 
87 Vector direct(const MatrixRef source, const MatrixRef target, double bandwidth);
88 
90 Vector direct(const MatrixRef source, const MatrixRef target, double bandwidth,
91  const VectorRef weights);
92 
94 Vector direct_tree(const MatrixRef source, const MatrixRef target,
95  double bandwidth, double epsilon);
96 
98 Vector direct_tree(const MatrixRef source, const MatrixRef target,
99  double bandwidth, double epsilon, const VectorRef weights);
100 
102 Vector ifgt(const MatrixRef source, const MatrixRef target, double bandwidth,
103  double epsilon);
104 
106 Vector ifgt(const MatrixRef source, const MatrixRef target, double bandwidth,
107  double epsilon, const VectorRef weights);
108 
115 class Transform {
116 public:
118  Transform(const MatrixRef source, double bandwidth);
119 
121  Transform(const Transform&) = delete;
123  Transform& operator=(const Transform&) = delete;
125  Transform& operator=(Transform&&) = delete;
126 
128  virtual ~Transform() {}
129 
131  const MatrixRef source() const { return m_source; }
133  double bandwidth() const { return m_bandwidth; }
134 
136  Vector compute(const MatrixRef target);
138  Vector compute(const MatrixRef target, const VectorRef weights);
139 
140 private:
141  virtual Vector compute_impl(const MatrixRef target,
142  const VectorRef weights) const = 0;
143 
144  const Matrix m_source;
145  double m_bandwidth;
146 };
147 
149 class Direct : public Transform {
150 public:
152  Direct(const MatrixRef source, double bandwidth);
153 
155  Direct(const Direct&) = delete;
157  Direct& operator=(const Direct&) = delete;
159  Direct& operator=(Direct&&) = delete;
160 
161 private:
162  virtual Vector compute_impl(const MatrixRef target,
163  const VectorRef weights) const;
164 };
165 
167 class DirectTree : public Transform {
168 public:
173  DirectTree(const MatrixRef source, double bandwidth, double epsilon);
174 
176  DirectTree(const DirectTree&) = delete;
178  DirectTree& operator=(const DirectTree&) = delete;
180  DirectTree& operator=(DirectTree&&) = delete;
181 
185  virtual ~DirectTree();
186 
188  double epsilon() const { return m_epsilon; }
189 
190 private:
191  struct NanoflannTree;
192 
193  virtual Vector compute_impl(const MatrixRef target,
194  const VectorRef weights) const;
195 
196  double m_epsilon;
197  std::unique_ptr<NanoflannTree> m_tree;
198 };
199 
201 struct Clustering;
202 
204 class Ifgt : public Transform {
205 public:
210  Ifgt(const MatrixRef source, double bandwidth, double epsilon);
211 
213  Ifgt(const Ifgt&) = delete;
215  Ifgt& operator=(const Ifgt&) = delete;
217  Ifgt& operator=(Ifgt&&) = delete;
218 
222  virtual ~Ifgt();
223 
225  double epsilon() const { return m_epsilon; }
227  Matrix::Index nclusters() const { return m_nclusters; }
229  Matrix::Index truncation_number() const { return m_truncation_number; }
231  Matrix::Index p_max_total() const { return m_p_max_total; }
232 
233 private:
234  virtual Vector compute_impl(const MatrixRef target,
235  const VectorRef weights) const;
236  Vector compute_monomials(const VectorRef d) const;
237  Vector compute_constant_series() const;
238 
239  double m_epsilon;
240  Matrix::Index m_nclusters;
241  std::unique_ptr<Clustering> m_clustering;
242  Matrix::Index m_truncation_number;
243  Matrix::Index m_p_max_total;
244  Vector m_constant_series;
245  Vector m_ry_square;
246 };
247 }
bool with_openmp()
Returns true if the library was compiled with OpenMP support.
double bandwidth() const
Returns the bandwidth of the transform.
Definition: fgt.hpp:133
fgt exceptions.
Definition: fgt.hpp:46
Matrix::Index truncation_number() const
Returns the truncation number.
Definition: fgt.hpp:229
Thrown when an IFGT run asks for no clusters.
Definition: fgt.hpp:56
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Matrix
Convenience typedef for our type of matrix.
Definition: fgt.hpp:66
const MatrixRef source() const
Returns the pointer to the source dataset.
Definition: fgt.hpp:131
Top-level namespace for all things fgt.
Definition: fgt.hpp:43
Improved Fast Gauss Transform.
Definition: fgt.hpp:204
Direct Gauss transform.
Definition: fgt.hpp:149
double epsilon() const
Returns the error tolerance value.
Definition: fgt.hpp:225
Vector direct(const MatrixRef source, const MatrixRef target, double bandwidth)
Computes the direct Gauss transform with equal weights.
const char * version()
Returns the version of the fgt library as a string.
virtual ~Transform()
Destroys a transform.
Definition: fgt.hpp:128
Matrix::Index p_max_total() const
Returns the length of each monomial.
Definition: fgt.hpp:231
Abstract base class for all supported variants of the Gauss transform.
Definition: fgt.hpp:115
Eigen::Ref< const Vector > VectorRef
Convenience typedef for a reference to a Vector.
Definition: fgt.hpp:78
Eigen::VectorXd Vector
Convenience typedef for our flavor of Eigen::Vector.
Definition: fgt.hpp:75
Direct Gauss transform using a KD-tree truncation.
Definition: fgt.hpp:167
Eigen::Ref< const Matrix > MatrixRef
Convenience typedef for a reference to our type of Eigen::Matrix.
Definition: fgt.hpp:72
Vector ifgt(const MatrixRef source, const MatrixRef target, double bandwidth, double epsilon)
Computes the Improved Fast Gauss Transform.
double epsilon() const
Returns the error tolerance value.
Definition: fgt.hpp:188
Matrix::Index nclusters() const
Returns the number of clusters.
Definition: fgt.hpp:227
Vector direct_tree(const MatrixRef source, const MatrixRef target, double bandwidth, double epsilon)
Computes the direct Gauss transform using a kd-tree.