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  * Written (W) 2016 Soumyajit De
9  * Copyright (C) 2007-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #include <shogun/lib/common.h>
18 
19 using namespace shogun;
20 
22 {
23  register_params();
24 }
25 
27 {
28  register_params();
29  init(l, r);
30 }
31 
33 {
34  cleanup();
35 }
36 
37 bool CEuclideanDistance::init(CFeatures* l, CFeatures* r)
38 {
39  cleanup();
40 
41  CDistance::init(l, r);
42  REQUIRE(l->has_property(FP_DOT), "Left hand side features must support dot property!\n");
43  REQUIRE(r->has_property(FP_DOT), "Right hand side features must support dot property!\n");
44 
45  CDotFeatures* casted_l=static_cast<CDotFeatures*>(l);
46  CDotFeatures* casted_r=static_cast<CDotFeatures*>(r);
47 
48  REQUIRE(casted_l->get_dim_feature_space()==casted_r->get_dim_feature_space(),
49  "Number of dimension mismatch (l:%d vs. r:%d)!\n",
50  casted_l->get_dim_feature_space(),casted_r->get_dim_feature_space());
51 
53  if (lhs==rhs)
55  else
57 
58  return true;
59 }
60 
62 {
64 }
65 
66 float64_t CEuclideanDistance::compute(int32_t idx_a, int32_t idx_b)
67 {
68  float64_t result=0;
69  CDotFeatures* casted_lhs=static_cast<CDotFeatures*>(lhs);
70  CDotFeatures* casted_rhs=static_cast<CDotFeatures*>(rhs);
71 
73  result=casted_lhs->dot(idx_a, casted_rhs, idx_b);
74  else
75  result=casted_rhs->dot(idx_b, casted_lhs, idx_a);
76 
77  SG_DEBUG("distance=(%f)+(%f)-2*(%f)\n", m_lhs_squared_norms[idx_a], m_rhs_squared_norms[idx_a], result);
78 
79  result=m_lhs_squared_norms[idx_a]+m_rhs_squared_norms[idx_b]-2*result;
80  if (disable_sqrt)
81  return result;
82  return CMath::sqrt(result);
83 }
84 
86 {
87  REQUIRE(lhs, "Left hand side feature cannot be NULL!\n");
88  const index_t num_vec=lhs->get_num_vectors();
89 
90  if (m_lhs_squared_norms.vlen!=num_vec)
92 
93  CDotFeatures* casted_lhs=static_cast<CDotFeatures*>(lhs);
94 #pragma omp parallel for
95  for(index_t i =0; i<num_vec; ++i)
96  m_lhs_squared_norms[i]=casted_lhs->dot(i, casted_lhs, i);
97 }
98 
100 {
101  REQUIRE(rhs, "Right hand side feature cannot be NULL!\n");
102  const index_t num_vec=rhs->get_num_vectors();
103 
104  if (m_rhs_squared_norms.vlen!=num_vec)
106 
107  CDotFeatures* casted_rhs=static_cast<CDotFeatures*>(rhs);
108 #pragma omp parallel for
109  for(index_t i =0; i<num_vec; ++i)
110  m_rhs_squared_norms[i]=casted_rhs->dot(i, casted_rhs, i);
111 }
112 
114 {
117 }
118 
120 {
121  CFeatures* previous_lhs=CDistance::replace_lhs(l);
122  precompute_lhs();
123  if (lhs==rhs)
125  return previous_lhs;
126 }
127 
129 {
130  CFeatures* previous_rhs=CDistance::replace_rhs(r);
131  if (lhs==rhs)
133  else
134  precompute_rhs();
135  return previous_rhs;
136 }
137 
138 void CEuclideanDistance::register_params()
139 {
140  disable_sqrt=false;
142  SG_ADD(&disable_sqrt, "disable_sqrt", "If sqrt shall not be applied.", MS_NOT_AVAILABLE);
143  SG_ADD(&m_rhs_squared_norms, "m_rhs_squared_norms", "Squared norms from features of right hand side", MS_NOT_AVAILABLE);
144  SG_ADD(&m_lhs_squared_norms, "m_lhs_squared_norms", "Squared norms from features of left hand side", MS_NOT_AVAILABLE);
145 }
146 
147 float64_t CEuclideanDistance::distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound)
148 {
150  "Left hand side (was %s) has to be CDenseFeatures instance!\n", lhs->get_name());
152  "Right hand side (was %s) has to be CDenseFeatures instance!\n", rhs->get_name());
153 
155  "Left hand side (was %s) has to be of double type!\n", lhs->get_name());
157  "Right hand side (was %s) has to be double type!\n", rhs->get_name());
158 
159  CDenseFeatures<float64_t>* casted_lhs=static_cast<CDenseFeatures<float64_t>*>(lhs);
160  CDenseFeatures<float64_t>* casted_rhs=static_cast<CDenseFeatures<float64_t>*>(rhs);
161 
162  upper_bound*=upper_bound;
163 
164  SGVector<float64_t> avec=casted_lhs->get_feature_vector(idx_a);
165  SGVector<float64_t> bvec=casted_rhs->get_feature_vector(idx_b);
166 
167  REQUIRE(avec.vlen==bvec.vlen, "The vector lengths are not equal (%d vs %d)!\n", avec.vlen, bvec.vlen);
168 
169  float64_t result=0;
170  for (int32_t i=0; i<avec.vlen; i++)
171  {
172  result+=CMath::sq(avec[i]-bvec[i]);
173  if (result>upper_bound)
174  break;
175  }
176 
177  if (!disable_sqrt)
178  result=CMath::sqrt(result);
179 
180  return result;
181 }
virtual CFeatures * replace_rhs(CFeatures *rhs)
virtual const char * get_name() const =0
virtual bool support_compatible_class() const
Definition: Features.h:323
ST * get_feature_vector(int32_t num, int32_t &len, bool &dofree)
Class Distance, a base class for all the distances used in the Shogun toolbox.
Definition: Distance.h:87
int32_t index_t
Definition: common.h:62
virtual CFeatures * replace_lhs(CFeatures *lhs)
Definition: Distance.cpp:188
static T sq(T x)
Definition: Math.h:450
virtual float64_t dot(int32_t vec_idx1, CDotFeatures *df, int32_t vec_idx2)=0
virtual int32_t get_num_vectors() const =0
#define REQUIRE(x,...)
Definition: SGIO.h:206
Features that support dot products among other operations.
Definition: DotFeatures.h:44
virtual int32_t get_dim_feature_space() const =0
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
index_t vlen
Definition: SGVector.h:492
virtual float64_t distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound)
virtual CFeatures * replace_lhs(CFeatures *lhs)
double float64_t
Definition: common.h:50
virtual bool init(CFeatures *l, CFeatures *r)
virtual EFeatureClass get_feature_class() const =0
virtual CFeatures * replace_rhs(CFeatures *rhs)
Definition: Distance.cpp:170
#define SG_DEBUG(...)
Definition: SGIO.h:107
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: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
#define SG_ADD(...)
Definition: SGObject.h:81
SGVector< float64_t > m_rhs_squared_norms
static float32_t sqrt(float32_t x)
Definition: Math.h:459
bool has_property(EFeatureProperty p) const
Definition: Features.cpp:295
virtual EFeatureType get_feature_type() const =0

SHOGUN Machine Learning Toolbox - Documentation