mlpack  2.2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  if (distance < 1) {
75  return -1.0 / bandwidth;
76  } else if (distance > 1) {
77  return 0;
78  } else {
79  return arma::datum::nan;
80  }
81  }
82 
84  double Bandwidth() const { return bandwidth; }
86  double& Bandwidth() { return bandwidth; }
87 
89  template<typename Archive>
90  void Serialize(Archive& ar, const unsigned int /* version */)
91  {
92  ar & data::CreateNVP(bandwidth, "bandwidth");
93  }
94 
95  private:
97  double bandwidth;
98 };
99 
101 template<>
103 {
104  public:
106  static const bool IsNormalized = true;
108  static const bool UsesSquaredDistance = false;
109 };
110 
111 } // namespace kernel
112 } // namespace mlpack
113 
114 #endif
This is a template class that can provide information about various kernels.
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.
double & Bandwidth()
Modify the bandwidth of the kernel.
The trivially simple triangular kernel, defined by.
The core includes that mlpack expects; standard C++ includes and Armadillo.
static VecTypeA::elem_type Evaluate(const VecTypeA &a, const VecTypeB &b)
Computes the distance between two points.
double Bandwidth() const
Get the bandwidth of the kernel.
double Evaluate(const VecTypeA &a, const VecTypeB &b) const
Evaluate the triangular kernel for the two given vectors.
double Gradient(const double distance) const
Evaluate the gradient of triangular kernel given that the distance between the two points is known...
double Evaluate(const double distance) const
Evaluate the triangular kernel given that the distance between the two points is known.
static const bool IsNormalized
If true, then the kernel is normalized: K(x, x) = K(y, y) = 1 for all x.
void Serialize(Archive &ar, const unsigned int)
Serialize the kernel.
static const bool UsesSquaredDistance
If true, then the kernel include a squared distance, ||x - y||^2 .
TriangularKernel(const double bandwidth=1.0)
Initialize the triangular kernel with the given bandwidth (default 1.0).