SHOGUN  v3.0.0
 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 <stdio.h>
16 
17 #include <shogun/lib/common.h>
18 #include <shogun/io/File.h>
20 #include <shogun/base/SGObject.h>
23 
24 namespace shogun
25 {
26 class CFile;
27 class CMath;
28 class CFeatures;
29 
32 {
33  D_UNKNOWN = 0,
36  D_CANBERRA = 30,
38  D_GEODESIC = 50,
39  D_JENSEN = 60,
44  D_EUCLIDEAN = 110,
45  D_CHISQUARE = 120,
46  D_TANIMOTO = 130,
47  D_COSINE = 140,
48  D_BRAYCURTIS = 150,
49  D_CUSTOM = 160,
52  D_DIRECTOR = 190,
54 };
55 
56 
80 class CDistance : public CSGObject
81 {
82  public:
84  CDistance();
85 
93  virtual ~CDistance();
94 
102  virtual float64_t distance(int32_t idx_a, int32_t idx_b);
103 
117  virtual float64_t distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound)
118  {
119  return distance(idx_a, idx_b);
120  }
121 
127  {
128  return get_distance_matrix<float64_t>();
129  }
130 
135  template <class T> SGMatrix<T> get_distance_matrix();
136 
143  int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
144  {
145  int32_t i_start;
146 
147  if (symmetric)
148  i_start=(int32_t) CMath::floor(n-CMath::sqrt(CMath::sq((float64_t) n)-offs));
149  else
150  i_start=(int32_t) (offs/int64_t(n));
151 
152  return i_start;
153  }
154 
159  template <class T> static void* get_distance_matrix_helper(void* p);
160 
170  virtual bool init(CFeatures* lhs, CFeatures* rhs);
171 
176  virtual void cleanup()=0;
177 
182  void load(CFile* loader);
183 
188  void save(CFile* writer);
189 
194  inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; };
195 
200  inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; };
201 
211 
221 
223  virtual void remove_lhs_and_rhs();
224 
226  virtual void remove_lhs();
227 
229  virtual void remove_rhs();
230 
237  virtual EDistanceType get_distance_type()=0 ;
238 
245  virtual EFeatureType get_feature_type()=0;
246 
253  virtual EFeatureClass get_feature_class()=0;
254 
260  inline bool get_precompute_matrix() { return precompute_matrix ; }
261 
267  virtual void set_precompute_matrix(bool flag)
268  {
269  precompute_matrix=flag;
270 
271  if (!precompute_matrix)
272  {
273  SG_FREE(precomputed_matrix);
274  precomputed_matrix=NULL;
275  }
276  }
277 
282  virtual int32_t get_num_vec_lhs()
283  {
284  return num_lhs;
285  }
286 
291  virtual int32_t get_num_vec_rhs()
292  {
293  return num_rhs;
294  }
295 
300  virtual bool has_features()
301  {
302  return lhs && rhs;
303  }
304 
309  inline bool lhs_equals_rhs()
310  {
311  return lhs==rhs;
312  }
313 
314  protected:
315 
317  static void* run_distance_thread(void* p);
318 
322  virtual float64_t compute(int32_t idx_a, int32_t idx_b)=0;
323 
325  void do_precompute_matrix();
326 
327  private:
328  void init();
329 
330  protected:
335 
340 
345 
347  int32_t num_lhs;
349  int32_t num_rhs;
350 
351 };
352 } // namespace shogun
353 #endif

SHOGUN Machine Learning Toolbox - Documentation