SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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 
194  virtual bool init(CFeatures* lhs, CFeatures* rhs);
195 
200  virtual void cleanup()=0;
201 
206  void load(CFile* loader);
207 
212  void save(CFile* writer);
213 
218  inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; };
219 
224  inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; };
225 
234  virtual CFeatures* replace_rhs(CFeatures* rhs);
235 
244  virtual CFeatures* replace_lhs(CFeatures* lhs);
245 
247  virtual void remove_lhs_and_rhs();
248 
250  virtual void remove_lhs();
251 
253  virtual void remove_rhs();
254 
261  virtual EDistanceType get_distance_type()=0 ;
262 
269  virtual EFeatureType get_feature_type()=0;
270 
277  virtual EFeatureClass get_feature_class()=0;
278 
284  inline bool get_precompute_matrix() { return precompute_matrix ; }
285 
291  virtual void set_precompute_matrix(bool flag)
292  {
293  precompute_matrix=flag;
294 
295  if (!precompute_matrix)
296  {
297  SG_FREE(precomputed_matrix);
298  precomputed_matrix=NULL;
299  }
300  }
301 
306  virtual int32_t get_num_vec_lhs()
307  {
308  return num_lhs;
309  }
310 
315  virtual int32_t get_num_vec_rhs()
316  {
317  return num_rhs;
318  }
319 
324  virtual bool has_features()
325  {
326  return lhs && rhs;
327  }
328 
333  inline bool lhs_equals_rhs()
334  {
335  return lhs==rhs;
336  }
337 
347  void run_distance_rhs(SGVector<float64_t>& result, const index_t idx_r_start, index_t idx_start, const index_t idx_stop, const index_t idx_a);
348 
358  void run_distance_lhs(SGVector<float64_t>& result, const index_t idx_r_start, index_t idx_start, const index_t idx_stop, const index_t idx_b);
359 
360  protected:
361 
363  static void* run_distance_thread(void* p);
364 
368  virtual float64_t compute(int32_t idx_a, int32_t idx_b)=0;
369 
371  void do_precompute_matrix();
372 
380  virtual bool check_compatibility(CFeatures* l, CFeatures* r);
381 
382  private:
383  void init();
384 
385  protected:
390 
395 
400 
402  int32_t num_lhs;
404  int32_t num_rhs;
405 
406 };
407 } // namespace shogun
408 #endif
virtual EFeatureClass get_feature_class()=0
void do_precompute_matrix()
matrix precomputation
Definition: Distance.cpp:231
virtual bool has_features()
Definition: Distance.h:324
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
int32_t index_t
Definition: common.h:72
CFeatures * get_lhs()
Definition: Distance.h:218
virtual int32_t get_num_vec_lhs()
Definition: Distance.h:306
virtual CFeatures * replace_lhs(CFeatures *lhs)
Definition: Distance.cpp:165
static T sq(T x)
Definition: Math.h:445
virtual void remove_lhs()
takes all necessary steps if the lhs is removed from distance matrix
Definition: Distance.cpp:132
int32_t num_rhs
Definition: Distance.h:404
virtual EDistanceType get_distance_type()=0
virtual ~CDistance()
Definition: Distance.cpp:47
CFeatures * get_rhs()
Definition: Distance.h:224
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:284
void run_distance_rhs(SGVector< float64_t > &result, const index_t idx_r_start, index_t idx_start, const index_t idx_stop, const index_t idx_a)
Definition: Distance.cpp:219
#define SG_REF(x)
Definition: SGObject.h:52
static float64_t floor(float64_t d)
Definition: Math.h:402
virtual bool check_compatibility(CFeatures *l, CFeatures *r)
Definition: Distance.cpp:78
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
bool lhs_equals_rhs()
Definition: Distance.h:333
virtual void set_precompute_matrix(bool flag)
Definition: Distance.h:291
EDistanceType
Definition: Distance.h:32
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:125
virtual void remove_lhs_and_rhs()
Definition: Distance.cpp:121
double float64_t
Definition: common.h:60
void save(CFile *writer)
Definition: Distance.cpp:115
A File access base class.
Definition: File.h:34
void load(CFile *loader)
Definition: Distance.cpp:109
void run_distance_lhs(SGVector< float64_t > &result, const index_t idx_r_start, index_t idx_start, const index_t idx_stop, const index_t idx_b)
Definition: Distance.cpp:225
virtual int32_t get_num_vec_rhs()
Definition: Distance.h:315
int32_t num_lhs
Definition: Distance.h:402
virtual CFeatures * replace_rhs(CFeatures *rhs)
Definition: Distance.cpp:147
float float32_t
Definition: common.h:59
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:183
bool precompute_matrix
Definition: Distance.h:394
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:397
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:399
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:389
static float32_t sqrt(float32_t x)
Definition: Math.h:454
virtual void remove_rhs()
takes all necessary steps if the rhs is removed from distance matrix
Definition: Distance.cpp:140
virtual float64_t compute(int32_t idx_a, int32_t idx_b)=0

SHOGUN Machine Learning Toolbox - Documentation