SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Distance.h
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) 2006-2009 Christian Gehl
8  * Written (W) 2006-2009 Soeren Sonnenburg
9  * Copyright (C) 2006-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #ifndef _DISTANCE_H___
13 #define _DISTANCE_H___
14 
15 #include <shogun/lib/config.h>
16 
17 #include <shogun/lib/common.h>
18 #include <shogun/io/File.h>
20 #include <shogun/base/SGObject.h>
23 #include <shogun/lib/SGMatrix.h>
24 
25 namespace shogun
26 {
27 class CFile;
28 class CMath;
29 class CFeatures;
30 
33 {
34  D_UNKNOWN = 0,
37  D_CANBERRA = 30,
39  D_GEODESIC = 50,
40  D_JENSEN = 60,
45  D_EUCLIDEAN = 110,
46  D_CHISQUARE = 120,
47  D_TANIMOTO = 130,
48  D_COSINE = 140,
49  D_BRAYCURTIS = 150,
50  D_CUSTOM = 160,
53  D_DIRECTOR = 190,
55 };
56 
57 
87 class CDistance : public CSGObject
88 {
89  public:
91  CDistance();
92 
100  virtual ~CDistance();
101 
109  virtual float64_t distance(int32_t idx_a, int32_t idx_b);
110 
124  virtual float64_t distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound)
125  {
126  return distance(idx_a, idx_b);
127  }
128 
135  virtual void precompute_rhs(){}
136 
143  virtual void precompute_lhs(){}
144 
150  virtual void reset_precompute(){}
151 
157  {
158  return get_distance_matrix<float64_t>();
159  }
160 
165  template <class T> SGMatrix<T> get_distance_matrix();
166 
173  int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
174  {
175  int32_t i_start;
176 
177  if (symmetric)
178  i_start=(int32_t) CMath::floor(n-CMath::sqrt(CMath::sq((float64_t) n)-offs));
179  else
180  i_start=(int32_t) (offs/int64_t(n));
181 
182  return i_start;
183  }
184 
189  template <class T> static void* get_distance_matrix_helper(void* p);
190 
200  virtual bool init(CFeatures* lhs, CFeatures* rhs);
201 
206  virtual void cleanup()=0;
207 
212  void load(CFile* loader);
213 
218  void save(CFile* writer);
219 
224  inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; };
225 
230  inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; };
231 
241 
251 
253  virtual void remove_lhs_and_rhs();
254 
256  virtual void remove_lhs();
257 
259  virtual void remove_rhs();
260 
267  virtual EDistanceType get_distance_type()=0 ;
268 
275  virtual EFeatureType get_feature_type()=0;
276 
283  virtual EFeatureClass get_feature_class()=0;
284 
290  inline bool get_precompute_matrix() { return precompute_matrix ; }
291 
297  virtual void set_precompute_matrix(bool flag)
298  {
299  precompute_matrix=flag;
300 
301  if (!precompute_matrix)
302  {
303  SG_FREE(precomputed_matrix);
304  precomputed_matrix=NULL;
305  }
306  }
307 
312  virtual int32_t get_num_vec_lhs()
313  {
314  return num_lhs;
315  }
316 
321  virtual int32_t get_num_vec_rhs()
322  {
323  return num_rhs;
324  }
325 
330  virtual bool has_features()
331  {
332  return lhs && rhs;
333  }
334 
339  inline bool lhs_equals_rhs()
340  {
341  return lhs==rhs;
342  }
343 
344  protected:
345 
347  static void* run_distance_thread(void* p);
348 
352  virtual float64_t compute(int32_t idx_a, int32_t idx_b)=0;
353 
355  void do_precompute_matrix();
356 
357  private:
358  void init();
359 
360  protected:
365 
370 
375 
377  int32_t num_lhs;
379  int32_t num_rhs;
380 
381 };
382 } // namespace shogun
383 #endif
virtual EFeatureClass get_feature_class()=0
void do_precompute_matrix()
matrix precomputation
Definition: Distance.cpp:227
virtual bool has_features()
Definition: Distance.h:330
virtual void cleanup()=0
Class Distance, a base class for all the distances used in the Shogun toolbox.
Definition: Distance.h:87
virtual void reset_precompute()
Definition: Distance.h:150
CFeatures * get_lhs()
Definition: Distance.h:224
virtual int32_t get_num_vec_lhs()
Definition: Distance.h:312
CFeatures * replace_lhs(CFeatures *lhs)
Definition: Distance.cpp:167
static T sq(T x)
Definition: Math.h:450
virtual void remove_lhs()
takes all necessary steps if the lhs is removed from distance matrix
Definition: Distance.cpp:130
int32_t num_rhs
Definition: Distance.h:379
virtual EDistanceType get_distance_type()=0
virtual ~CDistance()
Definition: Distance.cpp:70
CFeatures * get_rhs()
Definition: Distance.h:230
static void * run_distance_thread(void *p)
run distance thread
virtual float64_t distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound)
Definition: Distance.h:124
bool get_precompute_matrix()
Definition: Distance.h:290
#define SG_REF(x)
Definition: SGObject.h:51
static float64_t floor(float64_t d)
Definition: Math.h:407
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
bool lhs_equals_rhs()
Definition: Distance.h:339
virtual void set_precompute_matrix(bool flag)
Definition: Distance.h:297
EDistanceType
Definition: Distance.h:32
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
virtual void remove_lhs_and_rhs()
Definition: Distance.cpp:119
double float64_t
Definition: common.h:50
void save(CFile *writer)
Definition: Distance.cpp:113
A File access base class.
Definition: File.h:34
void load(CFile *loader)
Definition: Distance.cpp:107
virtual int32_t get_num_vec_rhs()
Definition: Distance.h:321
int32_t num_lhs
Definition: Distance.h:377
CFeatures * replace_rhs(CFeatures *rhs)
Definition: Distance.cpp:145
float float32_t
Definition: common.h:49
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
Definition: Distance.h:173
virtual float64_t distance(int32_t idx_a, int32_t idx_b)
Definition: Distance.cpp:189
bool precompute_matrix
Definition: Distance.h:369
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
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
virtual EFeatureType get_feature_type()=0
virtual void precompute_lhs()
Definition: Distance.h:143
virtual void precompute_rhs()
Definition: Distance.h:135
SGMatrix< float64_t > get_distance_matrix()
Definition: Distance.h:156
float32_t * precomputed_matrix
Definition: Distance.h:364
static float32_t sqrt(float32_t x)
Definition: Math.h:459
virtual void remove_rhs()
takes all necessary steps if the rhs is removed from distance matrix
Definition: Distance.cpp:138
static void * get_distance_matrix_helper(void *p)
Definition: Distance.cpp:266
virtual float64_t compute(int32_t idx_a, int32_t idx_b)=0

SHOGUN Machine Learning Toolbox - Documentation