mlpack: a scalable c++ machine learning library
mlpack  2.0.2
spherical_kernel.hpp
Go to the documentation of this file.
1 
12 #ifndef mlpack_CORE_KERNELS_SPHERICAL_KERNEL_HPP
13 #define mlpack_CORE_KERNELS_SPHERICAL_KERNEL_HPP
14 
15 #include <boost/math/special_functions/gamma.hpp>
16 #include <mlpack/core.hpp>
17 
18 namespace mlpack {
19 namespace kernel {
20 
26 {
27  public:
31  SphericalKernel(const double bandwidth = 1.0) :
33  bandwidthSquared(std::pow(bandwidth, 2.0))
34  { /* Nothing to do. */ }
35 
45  template<typename VecTypeA, typename VecTypeB>
46  double Evaluate(const VecTypeA& a, const VecTypeB& b) const
47  {
48  return
50  1.0 : 0.0;
51  }
63  template<typename VecTypeA, typename VecTypeB>
64  double ConvolutionIntegral(const VecTypeA& a, const VecTypeB& b) const
65  {
66  double distance = sqrt(metric::SquaredEuclideanDistance::Evaluate(a, b));
67  if (distance >= 2.0 * bandwidth)
68  {
69  return 0.0;
70  }
71  double volumeSquared = pow(Normalizer(a.n_rows), 2.0);
72 
73  switch(a.n_rows)
74  {
75  case 1:
76  return 1.0 / volumeSquared * (2.0 * bandwidth - distance);
77  break;
78  case 2:
79  return 1.0 / volumeSquared *
80  (2.0 * bandwidth * bandwidth * acos(distance/(2.0 * bandwidth)) -
81  distance / 4.0 * sqrt(4.0*bandwidth*bandwidth-distance*distance));
82  break;
83  default:
84  Log::Fatal << "The spherical kernel does not support convolution\
85  integrals above dimension two, yet..." << std::endl;
86  return -1.0;
87  break;
88  }
89  }
90  double Normalizer(size_t dimension) const
91  {
92  return pow(bandwidth, (double) dimension) * pow(M_PI, dimension / 2.0) /
93  boost::math::tgamma(dimension / 2.0 + 1.0);
94  }
95 
101  double Evaluate(const double t) const
102  {
103  return (t <= bandwidth) ? 1.0 : 0.0;
104  }
105  double Gradient(double t) {
106  return t == bandwidth ? arma::datum::nan : 0.0;
107  }
108 
110  template<typename Archive>
111  void Serialize(Archive& ar, const unsigned int /* version */)
112  {
113  ar & data::CreateNVP(bandwidth, "bandwidth");
114  ar & data::CreateNVP(bandwidthSquared, "bandwidthSquared");
115  }
116 
117  private:
118  double bandwidth;
120 };
121 
123 template<>
125 {
126  public:
128  static const bool IsNormalized = true;
130  static const bool UsesSquaredDistance = false;
131 };
132 
133 } // namespace kernel
134 } // namespace mlpack
135 
136 #endif
void Serialize(Archive &ar, const unsigned int)
Serialize the object.
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.
This is a template class that can provide information about various kernels.
Linear algebra utility functions, generally performed on matrices or vectors.
FirstShim< T > CreateNVP(T &t, const std::string &name, typename boost::enable_if< HasSerialize< T >>::type *=0)
Call this function to produce a name-value pair; this is similar to BOOST_SERIALIZATION_NVP(), but should be used for types that have a Serialize() function (or contain a type that has a Serialize() function) instead of a serialize() function.
SphericalKernel(const double bandwidth=1.0)
Construct the SphericalKernel with the given bandwidth.
double Evaluate(const VecTypeA &a, const VecTypeB &b) const
Evaluate the spherical kernel with the given two vectors.
#define M_PI
Definition: prereqs.hpp:44
double Normalizer(size_t dimension) const
double Evaluate(const double t) const
Evaluate the kernel when only a distance is given, not two points.
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:92
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
The spherical kernel, which is 1 when the distance between the two argument points is less than or eq...