mlpack  1.0.12
spherical_kernel.hpp
Go to the documentation of this file.
1 
15 #ifndef __MLPACK_CORE_KERNELS_SPHERICAL_KERNEL_H
16 #define __MLPACK_CORE_KERNELS_SPHERICAL_KERNEL_H
17 
18 #include <boost/math/special_functions/gamma.hpp>
19 #include <mlpack/core.hpp>
20 
21 namespace mlpack {
22 namespace kernel {
23 
25 {
26  public:
28  bandwidth(1.0),
29  bandwidthSquared(1.0) {}
30  SphericalKernel(double b) :
31  bandwidth(b),
32  bandwidthSquared(b*b) {}
33 
34  template<typename VecType>
35  double Evaluate(const VecType& a, const VecType& b)
36  {
37  return
39  1.0 : 0.0;
40  }
53  template<typename VecType>
54  double ConvolutionIntegral(const VecType& a, const VecType& b)
55  {
56  double distance = sqrt(metric::SquaredEuclideanDistance::Evaluate(a, b));
57  if (distance >= 2.0 * bandwidth)
58  {
59  return 0.0;
60  }
61  double volumeSquared = pow(Normalizer(a.n_rows), 2.0);
62 
63  switch(a.n_rows)
64  {
65  case 1:
66  return 1.0 / volumeSquared * (2.0 * bandwidth - distance);
67  break;
68  case 2:
69  return 1.0 / volumeSquared *
70  (2.0 * bandwidth * bandwidth * acos(distance/(2.0 * bandwidth)) -
71  distance / 4.0 * sqrt(4.0*bandwidth*bandwidth-distance*distance));
72  break;
73  default:
74  Log::Fatal << "The spherical kernel does not support convolution\
75  integrals above dimension two, yet..." << std::endl;
76  return -1.0;
77  break;
78  }
79  }
80  double Normalizer(size_t dimension)
81  {
82  return pow(bandwidth, (double) dimension) * pow(M_PI, dimension / 2.0) /
83  boost::math::tgamma(dimension / 2.0 + 1.0);
84  }
85  double Evaluate(double t)
86  {
87  return (t <= bandwidth) ? 1.0 : 0.0;
88  }
89 
91  std::string ToString() const
92  {
93  std::ostringstream convert;
94  convert << "SphericalKernel [" << this << "]" << std::endl;
95  convert << " Bandwidth: " << bandwidth << std::endl;
96  return convert.str();
97  }
98 
99  private:
100  double bandwidth;
102 };
103 
105 template<>
107 {
108  public:
110  static const bool IsNormalized = true;
111 };
112 
113 }; // namespace kernel
114 }; // namespace mlpack
115 
116 #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:23
static util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:87
#define M_PI
Definition: prereqs.hpp:42
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)