mlpack: a scalable c++ machine learning library
mlpack  2.0.2
triangular_kernel.hpp
Go to the documentation of this file.
1 
14 #ifndef mlpack_CORE_KERNELS_TRIANGULAR_KERNEL_HPP
15 #define mlpack_CORE_KERNELS_TRIANGULAR_KERNEL_HPP
16 
17 #include <mlpack/core.hpp>
19 
20 namespace mlpack {
21 namespace kernel {
22 
33 {
34  public:
40  TriangularKernel(const double bandwidth = 1.0) : bandwidth(bandwidth) { }
41 
50  template<typename VecTypeA, typename VecTypeB>
51  double Evaluate(const VecTypeA& a, const VecTypeB& b) const
52  {
53  return std::max(0.0, (1 - metric::EuclideanDistance::Evaluate(a, b) /
54  bandwidth));
55  }
56 
63  double Evaluate(const double distance) const
64  {
65  return std::max(0.0, (1 - distance) / bandwidth);
66  }
67 
75  double Gradient(const double distance) const {
76  if (distance < 1) {
77  return -1.0 / bandwidth;
78  } else if (distance > 1) {
79  return 0;
80  } else {
81  return arma::datum::nan;
82  }
83  }
84 
86  double Bandwidth() const { return bandwidth; }
88  double& Bandwidth() { return bandwidth; }
89 
91  template<typename Archive>
92  void Serialize(Archive& ar, const unsigned int /* version */)
93  {
94  ar & data::CreateNVP(bandwidth, "bandwidth");
95  }
96 
97  private:
99  double bandwidth;
100 };
101 
103 template<>
105 {
106  public:
108  static const bool IsNormalized = true;
110  static const bool UsesSquaredDistance = false;
111 };
112 
113 } // namespace kernel
114 } // namespace mlpack
115 
116 #endif
double Gradient(const double distance) const
Evaluate the gradient of triangular kernel given that the distance between the two points is known...
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.
The trivially simple triangular kernel, defined by.
double Evaluate(const VecTypeA &a, const VecTypeB &b) const
Evaluate the triangular kernel for the two given vectors.
double bandwidth
The bandwidth of the kernel.
static VecTypeA::elem_type Evaluate(const VecTypeA &a, const VecTypeB &b)
Computes the distance between two points.
double & Bandwidth()
Modify the bandwidth of the kernel.
TriangularKernel(const double bandwidth=1.0)
Initialize the triangular kernel with the given bandwidth (default 1.0).
void Serialize(Archive &ar, const unsigned int)
Serialize the kernel.
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
double Evaluate(const double distance) const
Evaluate the triangular kernel given that the distance between the two points is known.
double Bandwidth() const
Get the bandwidth of the kernel.