SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TanimotoDistance.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) 2008-2009 Christian Gehl
8  * Copyright (C) 2008-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 CTanimotoDistance::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 CTanimotoDistance::compute(int32_t idx_a, int32_t idx_b)
47 {
48  int32_t alen, blen;
49  bool afree, bfree;
50 
51  float64_t* avec=
52  ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
53  float64_t* bvec=
54  ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
55 
56  ASSERT(alen==blen)
57 
58  float64_t s=0;
59  float64_t d=0;
60  float64_t nx=0;
61  float64_t ny=0;
62  {
63  for (int32_t i=0; i<alen; i++)
64  {
65  d+=avec[i]*bvec[i];
66  nx+=avec[i]*avec[i];
67  ny+=bvec[i]*bvec[i];
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  s=nx+ny-d;
75 
76  // trap division by zero
77  if(s==0)
78  return 0;
79 
80  return (d/s);
81 }

SHOGUN Machine Learning Toolbox - Documentation