SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CustomMahalanobisDistance.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2013 Fernando J. Iglesias Garcia
8  * Copyright (C) 2013 Fernando J. Iglesias Garcia
9  */
10 
12 
13 #ifdef HAVE_EIGEN3
14 
15 #include <Eigen/Dense>
16 
17 using namespace shogun;
18 using namespace Eigen;
19 
21 {
22  register_params();
23 }
24 
26 : CRealDistance()
27 {
28  register_params();
29  CRealDistance::init(l, r);
30  m_mahalanobis_matrix = m;
31 }
32 
33 void CCustomMahalanobisDistance::register_params()
34 {
35  SG_ADD(&m_mahalanobis_matrix, "m_mahalanobis_matrix", "Mahalanobis matrix", MS_NOT_AVAILABLE)
36 }
37 
39 {
40  cleanup();
41 }
42 
44 {
45 }
46 
48 {
49  return "CustomMahalanobisDistance";
50 }
51 
53 {
54  return D_CUSTOMMAHALANOBIS;
55 }
56 
57 float64_t CCustomMahalanobisDistance::compute(int32_t idx_a, int32_t idx_b)
58 {
59  // Get feature vectors that will be used to compute the distance; casts
60  // are safe, features are checked to be dense in DenseDistance::init
61  SGVector<float64_t> avec = static_cast<CDenseFeatures<float64_t>*>(lhs)->get_feature_vector(idx_a);
62  SGVector<float64_t> bvec = static_cast<CDenseFeatures<float64_t>*>(rhs)->get_feature_vector(idx_b);
63 
64  REQUIRE(avec.vlen == bvec.vlen, "In CCustomMahalanobisDistance::compute the "
65  "feature vectors must have the same number of elements")
66 
67  // Compute the distance between the feature vectors
68 
69  // Compute the difference vector and wrap in Eigen vector
70  const VectorXd dvec = Map<const VectorXd>(avec, avec.vlen) - Map<const VectorXd>(bvec, bvec.vlen);
71  // Wrap Mahalanobis distance in Eigen matrix
72  Map<const MatrixXd> M(m_mahalanobis_matrix.matrix, m_mahalanobis_matrix.num_rows,
73  m_mahalanobis_matrix.num_cols);
74 
75  return dvec.transpose()*M*dvec;
76 }
77 
78 #endif /* HAVE_EIGEN3 */
class RealDistance
Definition: RealDistance.h:22
Definition: SGMatrix.h:20
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_cols
Definition: SGMatrix.h:378
index_t num_rows
Definition: SGMatrix.h:376
index_t vlen
Definition: SGVector.h:494
EDistanceType
Definition: Distance.h:32
double float64_t
Definition: common.h:50
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CFeatures * lhs
feature vectors to occur on the left hand side
Definition: Distance.h:343
The class Features is the base class of all feature objects.
Definition: Features.h:68
CFeatures * rhs
feature vectors to occur on the right hand side
Definition: Distance.h:345
#define SG_ADD(...)
Definition: SGObject.h:81

SHOGUN Machine Learning Toolbox - Documentation