mlpack 3.4.2
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
16namespace mlpack {
17namespace 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 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) const
89 {
90 return pow(bandwidth, (double) dimension) * pow(M_PI, dimension / 2.0) /
91 std::tgamma(dimension / 2.0 + 1.0);
92 }
93
99 double Evaluate(const double t) const
100 {
101 return (t <= bandwidth) ? 1.0 : 0.0;
102 }
103 double Gradient(double t)
104 {
105 return t == bandwidth ? arma::datum::nan : 0.0;
106 }
107
109 template<typename Archive>
110 void serialize(Archive& ar, const unsigned int /* version */)
111 {
112 ar & BOOST_SERIALIZATION_NVP(bandwidth);
113 ar & BOOST_SERIALIZATION_NVP(bandwidthSquared);
114 }
115
116 private:
117 double bandwidth;
118 double bandwidthSquared;
119};
120
122template<>
124{
125 public:
127 static const bool IsNormalized = true;
129 static const bool UsesSquaredDistance = false;
130};
131
132} // namespace kernel
133} // namespace mlpack
134
135#endif
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
This is a template class that can provide information about various kernels.
static const bool UsesSquaredDistance
If true, then the kernel include a squared distance, ||x - y||^2 .
static const bool IsNormalized
If true, then the kernel is normalized: K(x, x) = K(y, y) = 1 for all x.
The spherical kernel, which is 1 when the distance between the two argument points is less than or eq...
double Evaluate(const double t) const
Evaluate the kernel when only a distance is given, not two points.
double Evaluate(const VecTypeA &a, const VecTypeB &b) const
Evaluate the spherical kernel with the given two vectors.
double Normalizer(size_t dimension) const
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.
void serialize(Archive &ar, const unsigned int)
Serialize the object.
SphericalKernel(const double bandwidth=1.0)
Construct the SphericalKernel with the given bandwidth.
static VecTypeA::elem_type Evaluate(const VecTypeA &a, const VecTypeB &b)
Computes the distance between two points.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Definition: prereqs.hpp:67
The core includes that mlpack expects; standard C++ includes and Armadillo.
#define M_PI
Definition: prereqs.hpp:39