SHOGUN  3.2.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
81 class CDistance : public CSGObject
82 {
83  public:
85  CDistance();
86 
94  virtual ~CDistance();
95 
103  virtual float64_t distance(int32_t idx_a, int32_t idx_b);
104 
118  virtual float64_t distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound)
119  {
120  return distance(idx_a, idx_b);
121  }
122 
128  {
129  return get_distance_matrix<float64_t>();
130  }
131 
136  template <class T> SGMatrix<T> get_distance_matrix();
137 
144  int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
145  {
146  int32_t i_start;
147 
148  if (symmetric)
149  i_start=(int32_t) CMath::floor(n-CMath::sqrt(CMath::sq((float64_t) n)-offs));
150  else
151  i_start=(int32_t) (offs/int64_t(n));
152 
153  return i_start;
154  }
155 
160  template <class T> static void* get_distance_matrix_helper(void* p);
161 
171  virtual bool init(CFeatures* lhs, CFeatures* rhs);
172 
177  virtual void cleanup()=0;
178 
183  void load(CFile* loader);
184 
189  void save(CFile* writer);
190 
195  inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; };
196 
201  inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; };
202 
212 
222 
224  virtual void remove_lhs_and_rhs();
225 
227  virtual void remove_lhs();
228 
230  virtual void remove_rhs();
231 
238  virtual EDistanceType get_distance_type()=0 ;
239 
246  virtual EFeatureType get_feature_type()=0;
247 
254  virtual EFeatureClass get_feature_class()=0;
255 
261  inline bool get_precompute_matrix() { return precompute_matrix ; }
262 
268  virtual void set_precompute_matrix(bool flag)
269  {
270  precompute_matrix=flag;
271 
272  if (!precompute_matrix)
273  {
274  SG_FREE(precomputed_matrix);
275  precomputed_matrix=NULL;
276  }
277  }
278 
283  virtual int32_t get_num_vec_lhs()
284  {
285  return num_lhs;
286  }
287 
292  virtual int32_t get_num_vec_rhs()
293  {
294  return num_rhs;
295  }
296 
301  virtual bool has_features()
302  {
303  return lhs && rhs;
304  }
305 
310  inline bool lhs_equals_rhs()
311  {
312  return lhs==rhs;
313  }
314 
315  protected:
316 
318  static void* run_distance_thread(void* p);
319 
323  virtual float64_t compute(int32_t idx_a, int32_t idx_b)=0;
324 
326  void do_precompute_matrix();
327 
328  private:
329  void init();
330 
331  protected:
336 
341 
346 
348  int32_t num_lhs;
350  int32_t num_rhs;
351 
352 };
353 } // namespace shogun
354 #endif

SHOGUN Machine Learning Toolbox - Documentation