SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MahalanobisDistance.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) 2012 Fernando José Iglesias García
8  * Copyright (C) 2012 Fernando José Iglesias García
9  */
10 
11 #include <shogun/lib/config.h>
12 
13 #ifdef HAVE_LAPACK
14 
15 #include <shogun/lib/common.h>
16 #include <shogun/io/SGIO.h>
21 
22 using namespace shogun;
23 
25 {
26  init();
27 }
28 
30 : CRealDistance()
31 {
32  init();
33  init(l, r);
34 }
35 
37 {
38  cleanup();
39 }
40 
41 bool CMahalanobisDistance::init(CFeatures* l, CFeatures* r)
42 {
43  CRealDistance::init(l, r);
44 
45 
46  if ( l == r)
47  {
48  mean = ((CDenseFeatures<float64_t>*) l)->get_mean();
49  icov = ((CDenseFeatures<float64_t>*) l)->get_cov();
50  }
51  else
52  {
55  }
56 
58 
59  return true;
60 }
61 
63 {
64 }
65 
66 float64_t CMahalanobisDistance::compute(int32_t idx_a, int32_t idx_b)
67 {
68 
70  get_feature_vector(idx_b);
71 
74 
75  if (use_mean)
76  diff = mean.clone();
77  else
78  {
79  avec = ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a);
80  diff=avec.clone();
81  }
82 
83  ASSERT(diff.vlen == bvec.vlen)
84 
85  for (int32_t i=0; i < diff.vlen; i++)
86  diff[i] = bvec.vector[i] - diff[i];
87 
88  SGVector<float64_t> v = diff.clone();
89  cblas_dgemv(CblasColMajor, CblasNoTrans,
91  diff.vlen, diff.vector, 1, 0.0, v.vector, 1);
92 
93  float64_t result = cblas_ddot(v.vlen, v.vector, 1, diff.vector, 1);
94 
95  if (!use_mean)
96  ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a);
97 
98  ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b);
99 
100  if (disable_sqrt)
101  return result;
102  else
103  return CMath::sqrt(result);
104 }
105 
106 void CMahalanobisDistance::init()
107 {
108  disable_sqrt=false;
109  use_mean=false;
110 
111  m_parameters->add(&disable_sqrt, "disable_sqrt", "If sqrt shall not be applied.");
112  m_parameters->add(&use_mean, "use_mean", "If distance shall be computed between mean vector and vector from rhs or between lhs and rhs.");
113 }
114 
115 #endif /* HAVE_LAPACK */
class RealDistance
Definition: RealDistance.h:22
Parameter * m_parameters
Definition: SGObject.h:546
index_t num_cols
Definition: SGMatrix.h:376
Features that support dot products among other operations.
Definition: DotFeatures.h:44
index_t num_rows
Definition: SGMatrix.h:374
static SGMatrix< float64_t > compute_cov(CDotFeatures *lhs, CDotFeatures *rhs)
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:37
index_t vlen
Definition: SGVector.h:494
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
CFeatures * lhs
feature vectors to occur on the left hand side
Definition: Distance.h:381
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:383
virtual bool init(CFeatures *l, CFeatures *r)
SGVector< T > clone() const
Definition: SGVector.cpp:207
static void inverse(SGMatrix< float64_t > matrix)
inverses square matrix in-place
Definition: SGMatrix.cpp:881
static float32_t sqrt(float32_t x)
Definition: Math.h:459

SHOGUN Machine Learning Toolbox - Documentation