mlpack
mahalanobis_distance_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_METRICS_MAHALANOBIS_DISTANCE_IMPL_HPP
13 #define MLPACK_CORE_METRICS_MAHALANOBIS_DISTANCE_IMPL_HPP
14 
15 #include "mahalanobis_distance.hpp"
16 
17 namespace mlpack {
18 namespace metric {
19 
23 template<>
24 template<typename VecTypeA, typename VecTypeB>
25 double MahalanobisDistance<false>::Evaluate(const VecTypeA& a,
26  const VecTypeB& b)
27 {
28  arma::vec m = (a - b);
29  arma::mat out = trans(m) * covariance * m; // 1x1
30  return out[0];
31 }
36 template<>
37 template<typename VecTypeA, typename VecTypeB>
38 double MahalanobisDistance<true>::Evaluate(const VecTypeA& a,
39  const VecTypeB& b)
40 {
41  // Check if covariance matrix has been initialized.
42  if (covariance.n_rows == 0)
43  covariance = arma::eye<arma::mat>(a.n_elem, a.n_elem);
44 
45  arma::vec m = (a - b);
46  arma::mat out = trans(m) * covariance * m; // 1x1;
47  return sqrt(out[0]);
48 }
49 
50 // Serialize the Mahalanobis distance.
51 template<bool TakeRoot>
52 template<typename Archive>
54  const uint32_t /* version */)
55 {
56  ar(CEREAL_NVP(covariance));
57 }
58 
59 } // namespace metric
60 } // namespace mlpack
61 
62 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
double Evaluate(const VecTypeA &a, const VecTypeB &b)
Evaluate the distance between the two given points using this Mahalanobis distance.
void serialize(Archive &ar, const uint32_t version)
Serialize the Mahalanobis distance.
Definition: mahalanobis_distance_impl.hpp:53