mlpack
cosine_distance_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_KERNELS_COSINE_DISTANCE_IMPL_HPP
13 #define MLPACK_CORE_KERNELS_COSINE_DISTANCE_IMPL_HPP
14 
15 #include "cosine_distance.hpp"
16 
17 namespace mlpack {
18 namespace kernel {
19 
20 template<typename VecTypeA, typename VecTypeB>
21 double CosineDistance::Evaluate(const VecTypeA& a, const VecTypeB& b)
22 {
23  // Since we are using the L2 inner product, this is easy. But we have to make
24  // sure we aren't dividing by zero (if we are, then the cosine similarity is
25  // 0: we reason this value because the cosine distance is just a normalized
26  // dot product; take away the normalization, and if ||a|| or ||b|| is equal to
27  // 0, then a^T b is zero too).
28  const double denominator = norm(a, 2) * norm(b, 2);
29  if (denominator == 0.0)
30  return 0;
31  else
32  return dot(a, b) / denominator;
33 }
34 
35 } // namespace kernel
36 } // namespace mlpack
37 
38 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
static double Evaluate(const VecTypeA &a, const VecTypeB &b)
Computes the cosine distance between two points.
Definition: cosine_distance_impl.hpp:21