SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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  result=m_lhs_squared_norms[idx_a]+m_rhs_squared_norms[idx_b]-2*result;
78  if (disable_sqrt)
79  return result;
80  return CMath::sqrt(result);
81 }
82 
84 {
85  REQUIRE(lhs, "Left hand side feature cannot be NULL!\n");
86  const index_t num_vec=lhs->get_num_vectors();
87 
88  if (m_lhs_squared_norms.vlen!=num_vec)
90 
91  CDotFeatures* casted_lhs=static_cast<CDotFeatures*>(lhs);
92 #pragma omp parallel for
93  for(index_t i =0; i<num_vec; ++i)
94  m_lhs_squared_norms[i]=casted_lhs->dot(i, casted_lhs, i);
95 }
96 
98 {
99  REQUIRE(rhs, "Right hand side feature cannot be NULL!\n");
100  const index_t num_vec=rhs->get_num_vectors();
101 
102  if (m_rhs_squared_norms.vlen!=num_vec)
104 
105  CDotFeatures* casted_rhs=static_cast<CDotFeatures*>(rhs);
106 #pragma omp parallel for
107  for(index_t i =0; i<num_vec; ++i)
108  m_rhs_squared_norms[i]=casted_rhs->dot(i, casted_rhs, i);
109 }
110 
112 {
115 }
116 
118 {
119  CFeatures* previous_lhs=CDistance::replace_lhs(l);
120  precompute_lhs();
121  if (lhs==rhs)
123  return previous_lhs;
124 }
125 
127 {
128  CFeatures* previous_rhs=CDistance::replace_rhs(r);
129  if (lhs==rhs)
131  else
132  precompute_rhs();
133  return previous_rhs;
134 }
135 
136 void CEuclideanDistance::register_params()
137 {
138  disable_sqrt=false;
140  SG_ADD(&disable_sqrt, "disable_sqrt", "If sqrt shall not be applied.", MS_NOT_AVAILABLE);
141  SG_ADD(&m_rhs_squared_norms, "m_rhs_squared_norms", "Squared norms from features of right hand side", MS_NOT_AVAILABLE);
142  SG_ADD(&m_lhs_squared_norms, "m_lhs_squared_norms", "Squared norms from features of left hand side", MS_NOT_AVAILABLE);
143 }
144 
145 float64_t CEuclideanDistance::distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound)
146 {
148  "Left hand side (was %s) has to be CDenseFeatures instance!\n", lhs->get_name());
150  "Right hand side (was %s) has to be CDenseFeatures instance!\n", rhs->get_name());
151 
153  "Left hand side (was %s) has to be of double type!\n", lhs->get_name());
155  "Right hand side (was %s) has to be double type!\n", rhs->get_name());
156 
157  CDenseFeatures<float64_t>* casted_lhs=static_cast<CDenseFeatures<float64_t>*>(lhs);
158  CDenseFeatures<float64_t>* casted_rhs=static_cast<CDenseFeatures<float64_t>*>(rhs);
159 
160  upper_bound*=upper_bound;
161 
162  SGVector<float64_t> avec=casted_lhs->get_feature_vector(idx_a);
163  SGVector<float64_t> bvec=casted_rhs->get_feature_vector(idx_b);
164 
165  REQUIRE(avec.vlen==bvec.vlen, "The vector lengths are not equal (%d vs %d)!\n", avec.vlen, bvec.vlen);
166 
167  float64_t result=0;
168  for (int32_t i=0; i<avec.vlen; i++)
169  {
170  result+=CMath::sq(avec[i]-bvec[i]);
171  if (result>upper_bound)
172  break;
173  }
174 
175  if (!disable_sqrt)
176  result=CMath::sqrt(result);
177 
178  return result;
179 }
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:494
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
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:84
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