SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CanberraMetric.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) 2006-2009 Christian Gehl
8  * Copyright (C) 2006-2009 Fraunhofer Institute FIRST
9  */
10 
11 #include <shogun/lib/config.h>
12 #include <shogun/lib/common.h>
13 #include <shogun/io/SGIO.h>
16 
17 using namespace shogun;
18 
21 {
22 }
23 
26 {
27  init(l, r);
28 }
29 
31 {
32  cleanup();
33 }
34 
35 bool CCanberraMetric::init(CFeatures* l, CFeatures* r)
36 {
37  bool result=CDenseDistance<float64_t>::init(l,r);
38 
39  return result;
40 }
41 
43 {
44 }
45 
46 float64_t CCanberraMetric::compute(int32_t idx_a, int32_t idx_b)
47 {
48 
49  int32_t alen, blen;
50  bool afree, bfree;
51 
52  float64_t* avec=
53  ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
54  float64_t* bvec=
55  ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
56 
57  ASSERT(alen==blen)
58 
59  float64_t absTmp = 0;
60  float64_t result=0;
61  {
62  for (int32_t i=0; i<alen; i++)
63  {
64  absTmp=fabs(avec[i])+fabs(bvec[i]);
65  if(absTmp!=0)
66  result+=fabs(avec[i]-fabs(bvec[i]))/absTmp;
67  }
68 
69  }
70 
71  ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
72  ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
73 
74  return result;
75 }
virtual bool init(CFeatures *l, CFeatures *r)
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
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
template class DenseDistance
Definition: DenseDistance.h:24

SHOGUN Machine Learning Toolbox - Documentation