mlpack
ip_metric_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_FASTMKS_IP_METRIC_IMPL_HPP
13 #define MLPACK_METHODS_FASTMKS_IP_METRIC_IMPL_HPP
14 
15 // In case it hasn't been included yet.
16 #include "ip_metric.hpp"
17 
20 
21 namespace mlpack {
22 namespace metric {
23 
24 // Constructor with no instantiated kernel.
25 template<typename KernelType>
27  kernel(new KernelType()),
28  kernelOwner(true)
29 {
30  // Nothing to do.
31 }
32 
33 // Constructor with instantiated kernel.
34 template<typename KernelType>
35 IPMetric<KernelType>::IPMetric(KernelType& kernel) :
36  kernel(&kernel),
37  kernelOwner(false)
38 {
39  // Nothing to do.
40 }
41 
42 // Destructor for the IPMetric.
43 template<typename KernelType>
45 {
46  if (kernelOwner)
47  delete kernel;
48 }
49 
50 template<typename KernelType>
52  kernel(new KernelType(*other.kernel)),
53  kernelOwner(true)
54 {
55  // Nothing to do.
56 }
57 
58 template<typename KernelType>
60 {
61  if (this == &other)
62  return *this;
63 
64  if (kernelOwner)
65  delete kernel;
66 
67  kernel = new KernelType(*other.kernel);
68  kernelOwner = true;
69  return *this;
70 }
71 
72 template<typename KernelType>
73 template<typename Vec1Type, typename Vec2Type>
74 inline typename Vec1Type::elem_type IPMetric<KernelType>::Evaluate(
75  const Vec1Type& a,
76  const Vec2Type& b)
77 {
78  // This is the metric induced by the kernel function.
79  // Maybe we can do better by caching some of this?
80  return sqrt(kernel->Evaluate(a, a) + kernel->Evaluate(b, b) -
81  2 * kernel->Evaluate(a, b));
82 }
83 
84 // Serialize the kernel.
85 template<typename KernelType>
86 template<typename Archive>
88  const uint32_t /* version */)
89 {
90  // If we're loading, we need to allocate space for the kernel, and we will own
91  // the kernel.
92  if (cereal::is_loading<Archive>())
93  {
94  if (kernelOwner)
95  delete kernel;
96  kernelOwner = true;
97  }
98 
99  ar(CEREAL_POINTER(kernel));
100 }
101 
102 // A specialization for the linear kernel, which actually just turns out to be
103 // the Euclidean distance.
104 template<>
105 template<typename Vec1Type, typename Vec2Type>
106 inline typename Vec1Type::elem_type IPMetric<kernel::LinearKernel>::Evaluate(
107  const Vec1Type& a,
108  const Vec2Type& b)
109 {
111 }
112 
113 } // namespace metric
114 } // namespace mlpack
115 
116 #endif
IPMetric()
Create the IPMetric without an instantiated kernel.
Definition: ip_metric_impl.hpp:26
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The inner product metric, IPMetric, takes a given Mercer kernel (KernelType), and when Evaluate() is ...
Definition: ip_metric.hpp:32
The L_p metric for arbitrary integer p, with an option to take the root.
Definition: lmetric.hpp:63
#define CEREAL_POINTER(T)
Cereal does not support the serialization of raw pointer.
Definition: pointer_wrapper.hpp:96