spherical_kernel.hpp
Go to the documentation of this file.
1 
10 #ifndef MLPACK_CORE_KERNELS_SPHERICAL_KERNEL_HPP
11 #define MLPACK_CORE_KERNELS_SPHERICAL_KERNEL_HPP
12 
13 #include <boost/math/special_functions/gamma.hpp>
14 #include <mlpack/prereqs.hpp>
15 
16 namespace mlpack {
17 namespace kernel {
18 
24 {
25  public:
29  SphericalKernel(const double bandwidth = 1.0) :
30  bandwidth(bandwidth),
31  bandwidthSquared(std::pow(bandwidth, 2.0))
32  { /* Nothing to do. */ }
33 
43  template<typename VecTypeA, typename VecTypeB>
44  double Evaluate(const VecTypeA& a, const VecTypeB& b) const
45  {
46  return
47  (metric::SquaredEuclideanDistance::Evaluate(a, b) <= bandwidthSquared) ?
48  1.0 : 0.0;
49  }
61  template<typename VecTypeA, typename VecTypeB>
62  double ConvolutionIntegral(const VecTypeA& a, const VecTypeB& b) const
63  {
64  double distance = sqrt(metric::SquaredEuclideanDistance::Evaluate(a, b));
65  if (distance >= 2.0 * bandwidth)
66  {
67  return 0.0;
68  }
69  double volumeSquared = pow(Normalizer(a.n_rows), 2.0);
70 
71  switch (a.n_rows)
72  {
73  case 1:
74  return 1.0 / volumeSquared * (2.0 * bandwidth - distance);
75  case 2:
76  return 1.0 / volumeSquared *
77  (2.0 * bandwidth * bandwidth * acos(distance/(2.0 * bandwidth)) -
78  distance / 4.0 * sqrt(4.0*bandwidth*bandwidth-distance*distance));
79  default:
80  Log::Fatal << "The spherical kernel does not support convolution\
81  integrals above dimension two, yet..." << std::endl;
82  return -1.0;
83  }
84  }
85  double Normalizer(size_t dimension) const
86  {
87  return pow(bandwidth, (double) dimension) * pow(M_PI, dimension / 2.0) /
88  std::tgamma(dimension / 2.0 + 1.0);
89  }
90 
96  double Evaluate(const double t) const
97  {
98  return (t <= bandwidth) ? 1.0 : 0.0;
99  }
100  double Gradient(double t)
101  {
102  return t == bandwidth ? arma::datum::nan : 0.0;
103  }
104 
106  template<typename Archive>
107  void serialize(Archive& ar, const uint32_t /* version */)
108  {
109  ar(CEREAL_NVP(bandwidth));
110  ar(CEREAL_NVP(bandwidthSquared));
111  }
112 
113  private:
114  double bandwidth;
115  double bandwidthSquared;
116 };
117 
119 template<>
121 {
122  public:
124  static const bool IsNormalized = true;
126  static const bool UsesSquaredDistance = false;
127 };
128 
129 } // namespace kernel
130 } // namespace mlpack
131 
132 #endif
double Evaluate(const double t) const
Evaluate the kernel when only a distance is given, not two points.
void serialize(Archive &ar, const uint32_t)
Serialize the object.
This is a template class that can provide information about various kernels.
Linear algebra utility functions, generally performed on matrices or vectors.
double Evaluate(const VecTypeA &a, const VecTypeB &b) const
Evaluate the spherical kernel with the given two vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
#define M_PI
Definition: prereqs.hpp:39
static VecTypeA::elem_type Evaluate(const VecTypeA &a, const VecTypeB &b)
Computes the distance between two points.
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
SphericalKernel(const double bandwidth=1.0)
Construct the SphericalKernel with the given bandwidth.
double ConvolutionIntegral(const VecTypeA &a, const VecTypeB &b) const
Obtains the convolution integral [integral K(||x-a||)K(||b-x||)dx] for the two vectors.
The spherical kernel, which is 1 when the distance between the two argument points is less than or eq...
double Normalizer(size_t dimension) const