mlpack  3.4.2
triangular_kernel.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_KERNELS_TRIANGULAR_KERNEL_HPP
13 #define MLPACK_CORE_KERNELS_TRIANGULAR_KERNEL_HPP
14 
15 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace kernel {
20 
31 {
32  public:
38  TriangularKernel(const double bandwidth = 1.0) : bandwidth(bandwidth) { }
39 
48  template<typename VecTypeA, typename VecTypeB>
49  double Evaluate(const VecTypeA& a, const VecTypeB& b) const
50  {
51  return std::max(0.0, (1 - metric::EuclideanDistance::Evaluate(a, b) /
52  bandwidth));
53  }
54 
61  double Evaluate(const double distance) const
62  {
63  return std::max(0.0, (1 - distance) / bandwidth);
64  }
65 
73  double Gradient(const double distance) const
74  {
75  if (distance < 1)
76  {
77  return -1.0 / bandwidth;
78  }
79  else if (distance > 1)
80  {
81  return 0;
82  }
83  else
84  {
85  return arma::datum::nan;
86  }
87  }
88 
90  double Bandwidth() const { return bandwidth; }
92  double& Bandwidth() { return bandwidth; }
93 
95  template<typename Archive>
96  void serialize(Archive& ar, const unsigned int /* version */)
97  {
98  ar & BOOST_SERIALIZATION_NVP(bandwidth);
99  }
100 
101  private:
103  double bandwidth;
104 };
105 
107 template<>
109 {
110  public:
112  static const bool IsNormalized = true;
114  static const bool UsesSquaredDistance = false;
115 };
116 
117 } // namespace kernel
118 } // namespace mlpack
119 
120 #endif
mlpack::kernel::TriangularKernel::Evaluate
double Evaluate(const double distance) const
Evaluate the triangular kernel given that the distance between the two points is known.
Definition: triangular_kernel.hpp:61
mlpack::kernel::KernelTraits
This is a template class that can provide information about various kernels.
Definition: kernel_traits.hpp:28
mlpack::kernel::KernelTraits::IsNormalized
static const bool IsNormalized
If true, then the kernel is normalized: K(x, x) = K(y, y) = 1 for all x.
Definition: kernel_traits.hpp:33
prereqs.hpp
The core includes that mlpack expects; standard C++ includes and Armadillo.
mlpack::kernel::TriangularKernel::TriangularKernel
TriangularKernel(const double bandwidth=1.0)
Initialize the triangular kernel with the given bandwidth (default 1.0).
Definition: triangular_kernel.hpp:38
mlpack::kernel::TriangularKernel::serialize
void serialize(Archive &ar, const unsigned int)
Serialize the kernel.
Definition: triangular_kernel.hpp:96
mlpack::metric::LMetric< 2, true >::Evaluate
static VecTypeA::elem_type Evaluate(const VecTypeA &a, const VecTypeB &b)
Computes the distance between two points.
lmetric.hpp
mlpack
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_cli11.hpp:21
mlpack::kernel::TriangularKernel::Evaluate
double Evaluate(const VecTypeA &a, const VecTypeB &b) const
Evaluate the triangular kernel for the two given vectors.
Definition: triangular_kernel.hpp:49
mlpack::kernel::TriangularKernel
The trivially simple triangular kernel, defined by.
Definition: triangular_kernel.hpp:31
mlpack::kernel::TriangularKernel::Bandwidth
double Bandwidth() const
Get the bandwidth of the kernel.
Definition: triangular_kernel.hpp:90
mlpack::kernel::TriangularKernel::Bandwidth
double & Bandwidth()
Modify the bandwidth of the kernel.
Definition: triangular_kernel.hpp:92
mlpack::kernel::KernelTraits::UsesSquaredDistance
static const bool UsesSquaredDistance
If true, then the kernel include a squared distance, ||x - y||^2 .
Definition: kernel_traits.hpp:38
mlpack::kernel::TriangularKernel::Gradient
double Gradient(const double distance) const
Evaluate the gradient of triangular kernel given that the distance between the two points is known.
Definition: triangular_kernel.hpp:73