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 <mlpack/prereqs.hpp>
14 
15 namespace mlpack {
16 namespace kernel {
17 
23 {
24  public:
28  SphericalKernel(const double bandwidth = 1.0) :
29  bandwidth(bandwidth),
30  bandwidthSquared(std::pow(bandwidth, 2.0))
31  { /* Nothing to do. */ }
32 
42  template<typename VecTypeA, typename VecTypeB>
43  double Evaluate(const VecTypeA& a, const VecTypeB& b) const
44  {
45  return
46  (metric::SquaredEuclideanDistance::Evaluate(a, b) <= bandwidthSquared) ?
47  1.0 : 0.0;
48  }
60  template<typename VecTypeA, typename VecTypeB>
61  double ConvolutionIntegral(const VecTypeA& a, const VecTypeB& b) const
62  {
63  double distance = sqrt(metric::SquaredEuclideanDistance::Evaluate(a, b));
64  if (distance >= 2.0 * bandwidth)
65  {
66  return 0.0;
67  }
68  double volumeSquared = pow(Normalizer(a.n_rows), 2.0);
69 
70  switch (a.n_rows)
71  {
72  case 1:
73  return 1.0 / volumeSquared * (2.0 * bandwidth - distance);
74  case 2:
75  return 1.0 / volumeSquared *
76  (2.0 * bandwidth * bandwidth * acos(distance/(2.0 * bandwidth)) -
77  distance / 4.0 * sqrt(4.0*bandwidth*bandwidth-distance*distance));
78  default:
79  Log::Fatal << "The spherical kernel does not support convolution\
80  integrals above dimension two, yet..." << std::endl;
81  return -1.0;
82  }
83  }
84  double Normalizer(size_t dimension) const
85  {
86  return pow(bandwidth, (double) dimension) * pow(M_PI, dimension / 2.0) /
87  std::tgamma(dimension / 2.0 + 1.0);
88  }
89 
95  double Evaluate(const double t) const
96  {
97  return (t <= bandwidth) ? 1.0 : 0.0;
98  }
99  double Gradient(double t)
100  {
101  return t == bandwidth ? arma::datum::nan : 0.0;
102  }
103 
105  template<typename Archive>
106  void serialize(Archive& ar, const uint32_t /* version */)
107  {
108  ar(CEREAL_NVP(bandwidth));
109  ar(CEREAL_NVP(bandwidthSquared));
110  }
111 
112  private:
113  double bandwidth;
114  double bandwidthSquared;
115 };
116 
118 template<>
120 {
121  public:
123  static const bool IsNormalized = true;
125  static const bool UsesSquaredDistance = false;
126 };
127 
128 } // namespace kernel
129 } // namespace mlpack
130 
131 #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