SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
JensenMetric.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 
20 {
21 }
22 
25 {
26  init(l, r);
27 }
28 
30 {
31  cleanup();
32 }
33 
34 bool CJensenMetric::init(CFeatures* l, CFeatures* r)
35 {
37 }
38 
40 {
41 }
42 
43 float64_t CJensenMetric::compute(int32_t idx_a, int32_t idx_b)
44 {
45  int32_t alen, blen;
46  bool afree, bfree;
47 
48  float64_t* avec=
49  ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
50  float64_t* bvec=
51  ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
52 
53  ASSERT(alen==blen)
54 
55  float64_t absTmp = 0;
56  float64_t result=0;
57  {
58  for (int32_t i=0; i<alen; i++)
59  {
60  absTmp=0.5*(avec[i]+bvec[i]);
61  if (absTmp>0)
62  {
63  if(avec[i]>0)
64  result+=avec[i]*log(avec[i]/absTmp);
65  if(bvec[i]>0)
66  result+=bvec[i]*log(bvec[i]/absTmp);
67  }
68  }
69 
70  }
71 
72  ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
73  ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
74 
75 
76  return result;
77 }
#define ASSERT(x)
Definition: SGIO.h:201
virtual void cleanup()
double float64_t
Definition: common.h:50
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
virtual bool init(CFeatures *l, CFeatures *r)
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: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
template class DenseDistance
Definition: DenseDistance.h:24

SHOGUN Machine Learning Toolbox - Documentation