SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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_EUCLIDIAN = 110,
45  D_CHISQUARE = 120,
46  D_TANIMOTO = 130,
47  D_COSINE = 140,
48  D_BRAYCURTIS = 150,
49  D_CUSTOM = 160,
51 };
52 
53 
77 class CDistance : public CSGObject
78 {
79  public:
81  CDistance();
82 
90  virtual ~CDistance();
91 
99  inline float64_t distance(int32_t idx_a, int32_t idx_b)
100  {
101  if (idx_a < 0 || idx_b <0)
102  return 0;
103 
104  ASSERT(lhs);
105  ASSERT(rhs);
106 
107  if (lhs==rhs)
108  {
109  int32_t num_vectors = lhs->get_num_vectors();
110 
111  if (idx_a>=num_vectors)
112  idx_a=2*num_vectors-1-idx_a;
113 
114  if (idx_b>=num_vectors)
115  idx_b=2*num_vectors-1-idx_b;
116  }
117 
118  ASSERT(idx_a<lhs->get_num_vectors());
119  ASSERT(idx_b<rhs->get_num_vectors());
120 
121  if (precompute_matrix && (precomputed_matrix==NULL) && (lhs==rhs))
123 
124  if (precompute_matrix && (precomputed_matrix!=NULL))
125  {
126  if (idx_a>=idx_b)
127  return precomputed_matrix[idx_a*(idx_a+1)/2+idx_b] ;
128  else
129  return precomputed_matrix[idx_b*(idx_b+1)/2+idx_a] ;
130  }
131 
132  return compute(idx_a, idx_b);
133  }
134 
140 
149  int32_t &m,int32_t &n, float64_t* target);
150 
159  int32_t &m,int32_t &n,float32_t* target);
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 
213  virtual void remove_lhs_and_rhs();
214 
216  virtual void remove_lhs();
217 
219  virtual void remove_rhs();
220 
227  virtual EDistanceType get_distance_type()=0 ;
228 
235  virtual EFeatureType get_feature_type()=0;
236 
243  virtual EFeatureClass get_feature_class()=0;
244 
250  inline bool get_precompute_matrix() { return precompute_matrix ; }
251 
257  inline virtual void set_precompute_matrix(bool flag)
258  {
259  precompute_matrix=flag;
260 
261  if (!precompute_matrix)
262  {
264  precomputed_matrix=NULL;
265  }
266  }
267 
272  inline int32_t get_num_vec_lhs()
273  {
274  if (!lhs)
275  return 0;
276  else
277  return lhs->get_num_vectors();
278  }
279 
284  inline int32_t get_num_vec_rhs()
285  {
286  if (!rhs)
287  return 0;
288  else
289  return rhs->get_num_vectors();
290  }
291 
296  inline bool has_features()
297  {
298  return lhs && rhs;
299  }
300 
305  inline bool lhs_equals_rhs()
306  {
307  return lhs==rhs;
308  }
309 
310  protected:
311 
313  static void* run_distance_thread(void* p);
314 
318  virtual float64_t compute(int32_t x, int32_t y)=0;
319 
321  void do_precompute_matrix();
322 
323  private:
324  void init();
325 
326  protected:
331 
336 
341 
342 };
343 } // namespace shogun
344 #endif

SHOGUN Machine Learning Toolbox - Documentation