MLPACK  1.0.11
spherical_kernel.hpp
Go to the documentation of this file.
1 
23 #ifndef __MLPACK_CORE_KERNELS_SPHERICAL_KERNEL_H
24 #define __MLPACK_CORE_KERNELS_SPHERICAL_KERNEL_H
25 
26 #include <boost/math/special_functions/gamma.hpp>
27 #include <mlpack/core.hpp>
28 
29 namespace mlpack {
30 namespace kernel {
31 
33 {
34  public:
36  bandwidth(1.0),
37  bandwidthSquared(1.0) {}
38  SphericalKernel(double b) :
39  bandwidth(b),
40  bandwidthSquared(b*b) {}
41 
42  template<typename VecType>
43  double Evaluate(const VecType& a, const VecType& b)
44  {
45  return
47  1.0 : 0.0;
48  }
61  template<typename VecType>
62  double ConvolutionIntegral(const VecType& a, const VecType& b)
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  break;
76  case 2:
77  return 1.0 / volumeSquared *
78  (2.0 * bandwidth * bandwidth * acos(distance/(2.0 * bandwidth)) -
79  distance / 4.0 * sqrt(4.0*bandwidth*bandwidth-distance*distance));
80  break;
81  default:
82  Log::Fatal << "The spherical kernel does not support convolution\
83  integrals above dimension two, yet..." << std::endl;
84  return -1.0;
85  break;
86  }
87  }
88  double Normalizer(size_t dimension)
89  {
90  return pow(bandwidth, (double) dimension) * pow(M_PI, dimension / 2.0) /
91  boost::math::tgamma(dimension / 2.0 + 1.0);
92  }
93  double Evaluate(double t)
94  {
95  return (t <= bandwidth) ? 1.0 : 0.0;
96  }
97 
99  std::string ToString() const
100  {
101  std::ostringstream convert;
102  convert << "SphericalKernel [" << this << "]" << std::endl;
103  convert << " Bandwidth: " << bandwidth << std::endl;
104  return convert.str();
105  }
106 
107  private:
108  double bandwidth;
110 };
111 
113 template<>
115 {
116  public:
118  static const bool IsNormalized = true;
119 };
120 
121 }; // namespace kernel
122 }; // namespace mlpack
123 
124 #endif
This is a template class that can provide information about various kernels.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:31
static util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:95
#define M_PI
Definition: prereqs.hpp:50
static double Evaluate(const VecType1 &a, const VecType2 &b)
Computes the distance between two points.
double ConvolutionIntegral(const VecType &a, const VecType &b)
Obtains the convolution integral [integral K(||x-a||)K(||b-x||)dx] for the two vectors.
std::string ToString() const
Return a string representation of the kernel.
double Normalizer(size_t dimension)
static const bool IsNormalized
If true, then the kernel is normalized: K(x, x) = K(y, y) = 1 for all x.
double Evaluate(const VecType &a, const VecType &b)