SHOGUN  3.2.1
 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 <stdio.h>
18 
19 #include <shogun/lib/common.h>
20 #include <shogun/io/File.h>
22 #include <shogun/base/SGObject.h>
25 #include <shogun/lib/SGMatrix.h>
26 
27 namespace shogun
28 {
29 class CFile;
30 class CMath;
31 class CFeatures;
32 
35 {
36  D_UNKNOWN = 0,
39  D_CANBERRA = 30,
41  D_GEODESIC = 50,
42  D_JENSEN = 60,
47  D_EUCLIDEAN = 110,
48  D_CHISQUARE = 120,
49  D_TANIMOTO = 130,
50  D_COSINE = 140,
51  D_BRAYCURTIS = 150,
52  D_CUSTOM = 160,
55  D_DIRECTOR = 190,
57 };
58 
59 
83 class CDistance : public CSGObject
84 {
85  public:
87  CDistance();
88 
96  virtual ~CDistance();
97 
105  virtual float64_t distance(int32_t idx_a, int32_t idx_b);
106 
120  virtual float64_t distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound)
121  {
122  return distance(idx_a, idx_b);
123  }
124 
130  {
131  return get_distance_matrix<float64_t>();
132  }
133 
138  template <class T> SGMatrix<T> get_distance_matrix();
139 
146  int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
147  {
148  int32_t i_start;
149 
150  if (symmetric)
151  i_start=(int32_t) CMath::floor(n-CMath::sqrt(CMath::sq((float64_t) n)-offs));
152  else
153  i_start=(int32_t) (offs/int64_t(n));
154 
155  return i_start;
156  }
157 
162  template <class T> static void* get_distance_matrix_helper(void* p);
163 
173  virtual bool init(CFeatures* lhs, CFeatures* rhs);
174 
179  virtual void cleanup()=0;
180 
185  void load(CFile* loader);
186 
191  void save(CFile* writer);
192 
197  inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; };
198 
203  inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; };
204 
214 
224 
226  virtual void remove_lhs_and_rhs();
227 
229  virtual void remove_lhs();
230 
232  virtual void remove_rhs();
233 
240  virtual EDistanceType get_distance_type()=0 ;
241 
248  virtual EFeatureType get_feature_type()=0;
249 
256  virtual EFeatureClass get_feature_class()=0;
257 
263  inline bool get_precompute_matrix() { return precompute_matrix ; }
264 
270  virtual void set_precompute_matrix(bool flag)
271  {
272  precompute_matrix=flag;
273 
274  if (!precompute_matrix)
275  {
276  SG_FREE(precomputed_matrix);
277  precomputed_matrix=NULL;
278  }
279  }
280 
285  virtual int32_t get_num_vec_lhs()
286  {
287  return num_lhs;
288  }
289 
294  virtual int32_t get_num_vec_rhs()
295  {
296  return num_rhs;
297  }
298 
303  virtual bool has_features()
304  {
305  return lhs && rhs;
306  }
307 
312  inline bool lhs_equals_rhs()
313  {
314  return lhs==rhs;
315  }
316 
317  protected:
318 
320  static void* run_distance_thread(void* p);
321 
325  virtual float64_t compute(int32_t idx_a, int32_t idx_b)=0;
326 
328  void do_precompute_matrix();
329 
330  private:
331  void init();
332 
333  protected:
338 
343 
348 
350  int32_t num_lhs;
352  int32_t num_rhs;
353 
354 };
355 } // namespace shogun
356 #endif

SHOGUN Machine Learning Toolbox - Documentation