SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EuclidianDistance.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) 2007-2009 Soeren Sonnenburg
8  * Copyright (C) 2007-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/common.h>
12 #include <shogun/io/SGIO.h>
16 
17 using namespace shogun;
18 
20 {
21  init();
22 }
23 
25 : CRealDistance()
26 {
27  init();
28  init(l, r);
29 }
30 
32 {
33  cleanup();
34 }
35 
36 bool CEuclidianDistance::init(CFeatures* l, CFeatures* r)
37 {
38  CRealDistance::init(l, r);
39 
40  return true;
41 }
42 
44 {
45 }
46 
47 float64_t CEuclidianDistance::compute(int32_t idx_a, int32_t idx_b)
48 {
49  int32_t alen, blen;
50  bool afree, bfree;
51  float64_t result=0;
52 
54  get_feature_vector(idx_a, alen, afree);
56  get_feature_vector(idx_b, blen, bfree);
57  ASSERT(alen==blen);
58 
59  for (int32_t i=0; i<alen; i++)
60  result+=CMath::sq(avec[i] - bvec[i]);
61 
62  ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
63  ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
64 
65  if (disable_sqrt)
66  return result;
67 
68  return CMath::sqrt(result);
69 }
70 
71 void CEuclidianDistance::init()
72 {
73  disable_sqrt=false;
74 
75  m_parameters->add(&disable_sqrt, "disable_sqrt", "If sqrt shall not be applied.");
76 }
class RealDistance
Definition: RealDistance.h:20
static T sq(T x)
x^2
Definition: Math.h:277
Parameter * m_parameters
Definition: SGObject.h:297
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:23
#define ASSERT(x)
Definition: SGIO.h:102
double float64_t
Definition: common.h:56
virtual bool init(CFeatures *l, CFeatures *r)
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Distance.h:338
The class Features is the base class of all feature objects.
Definition: Features.h:56
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Distance.h:340
static float32_t sqrt(float32_t x)
x^0.5
Definition: Math.h:283

SHOGUN Machine Learning Toolbox - Documentation