SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
EuclideanDistance.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>
14 
15 #ifdef HAVE_LINALG_LIB
17 #endif
18 
19 using namespace shogun;
20 
22 {
23  init();
24 }
25 
27 : CRealDistance()
28 {
29  init();
30  init(l, r);
31 }
32 
34 {
35  cleanup();
36 }
37 
38 bool CEuclideanDistance::init(CFeatures* l, CFeatures* r)
39 {
40  CRealDistance::init(l, r);
41 
42  return true;
43 }
44 
46 {
47 }
48 
49 float64_t CEuclideanDistance::compute(int32_t idx_a, int32_t idx_b)
50 {
51  int32_t alen, blen;
52  bool afree, bfree;
53  float64_t result=0;
54 
56  get_feature_vector(idx_a, alen, afree);
58  get_feature_vector(idx_b, blen, bfree);
59  ASSERT(alen==blen)
60 
61  result+=CMath::dot(avec, bvec, alen);
62  result*=-2;
63 
65  result+=m_rhs_squared_norms[idx_b];
66  else
67  result+=CMath::dot(bvec, bvec, alen);
68 
70  result+=m_lhs_squared_norms[idx_a];
71  else
72  result+=CMath::dot(avec, avec, alen);
73 
74  ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
75  ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
76 
77  if (disable_sqrt)
78  return result;
79 
80  return CMath::sqrt(result);
81 }
82 
84 {
85 #ifdef HAVE_LINALG_LIB
87 
88  for(int32_t idx_i =0; idx_i<rhs->get_num_vectors(); idx_i++)
89  {
90  SGVector<float64_t> tempvec=((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_i);
91  rhs_sq[idx_i]=linalg::dot(tempvec, tempvec);
92  ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(tempvec, idx_i);
93  }
94 
95  m_rhs_squared_norms=rhs_sq;
96 #endif
97 
98 }
99 
101 {
102 #ifdef HAVE_LINALG_LIB
104 
105  for(int32_t idx_i=0; idx_i<lhs->get_num_vectors(); idx_i++)
106  {
107  SGVector<float64_t> tempvec=((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_i);
108  lhs_sq[idx_i]=linalg::dot(tempvec, tempvec);
109  ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(tempvec, idx_i);
110  }
111 
112  m_lhs_squared_norms=lhs_sq;
113 #endif
114 }
115 
117 {
120 }
121 
122 void CEuclideanDistance::init()
123 {
124  disable_sqrt=false;
126  m_parameters->add(&disable_sqrt, "disable_sqrt", "If sqrt shall not be applied.");
127  m_parameters->add(&m_rhs_squared_norms, "m_rhs_squared_norms", "Squared norms from features of right hand side");
128  m_parameters->add(&m_lhs_squared_norms, "m_lhs_squared_norms", "Squared norms from features of left hand side");
129 }
130 
131 float64_t CEuclideanDistance::distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound)
132 {
133  int32_t alen, blen;
134  bool afree, bfree;
135  float64_t result=0;
136 
137  upper_bound *= upper_bound;
138 
140  get_feature_vector(idx_a, alen, afree);
142  get_feature_vector(idx_b, blen, bfree);
143  ASSERT(alen==blen)
144 
145  for (int32_t i=0; i<alen; i++)
146  {
147  result+=CMath::sq(avec[i] - bvec[i]);
148 
149  if (result > upper_bound)
150  {
152  free_feature_vector(avec, idx_a, afree);
154  free_feature_vector(bvec, idx_b, bfree);
155 
156  if (disable_sqrt)
157  return result;
158  else
159  return CMath::sqrt(result);
160  }
161  }
162 
163  ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
164  ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
165 
166  if (disable_sqrt)
167  return result;
168  else
169  return CMath::sqrt(result);
170 }
Vector::Scalar dot(Vector a, Vector b)
Definition: Redux.h:58
class RealDistance
Definition: RealDistance.h:22
static T sq(T x)
Definition: Math.h:450
virtual int32_t get_num_vectors() const =0
Parameter * m_parameters
Definition: SGObject.h:378
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:37
virtual float64_t distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound)
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
virtual bool init(CFeatures *l, CFeatures *r)
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
Compute dot product between v1 and v2 (blas optimized)
Definition: Math.h:627
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
SGVector< float64_t > m_lhs_squared_norms
CFeatures * lhs
feature vectors to occur on the left hand side
Definition: Distance.h:372
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:374
SGVector< float64_t > m_rhs_squared_norms
static float32_t sqrt(float32_t x)
Definition: Math.h:459

SHOGUN Machine Learning Toolbox - Documentation