SHOGUN  v2.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
53 };
54 
55 
79 class CDistance : public CSGObject
80 {
81  public:
83  CDistance();
84 
92  virtual ~CDistance();
93 
101  virtual float64_t distance(int32_t idx_a, int32_t idx_b)
102  {
103  if (idx_a < 0 || idx_b <0)
104  return 0;
105 
106  ASSERT(lhs);
107  ASSERT(rhs);
108 
109  if (lhs==rhs)
110  {
111  int32_t num_vectors = lhs->get_num_vectors();
112 
113  if (idx_a>=num_vectors)
114  idx_a=2*num_vectors-1-idx_a;
115 
116  if (idx_b>=num_vectors)
117  idx_b=2*num_vectors-1-idx_b;
118  }
119 
120  ASSERT(idx_a<lhs->get_num_vectors());
121  ASSERT(idx_b<rhs->get_num_vectors());
122 
123  if (precompute_matrix && (precomputed_matrix==NULL) && (lhs==rhs))
125 
126  if (precompute_matrix && (precomputed_matrix!=NULL))
127  {
128  if (idx_a>=idx_b)
129  return precomputed_matrix[idx_a*(idx_a+1)/2+idx_b] ;
130  else
131  return precomputed_matrix[idx_b*(idx_b+1)/2+idx_a] ;
132  }
133 
134  return compute(idx_a, idx_b);
135  }
136 
150  virtual float64_t distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound)
151  {
152  return distance(idx_a, idx_b);
153  }
154 
155 
161 
170  int32_t &m,int32_t &n, float64_t* target);
171 
180  int32_t &m,int32_t &n,float32_t* target);
181 
191  virtual bool init(CFeatures* lhs, CFeatures* rhs);
192 
197  virtual void cleanup()=0;
198 
203  void load(CFile* loader);
204 
209  void save(CFile* writer);
210 
215  inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; };
216 
221  inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; };
222 
232 
242 
244  virtual void remove_lhs_and_rhs();
245 
247  virtual void remove_lhs();
248 
250  virtual void remove_rhs();
251 
258  virtual EDistanceType get_distance_type()=0 ;
259 
266  virtual EFeatureType get_feature_type()=0;
267 
274  virtual EFeatureClass get_feature_class()=0;
275 
281  inline bool get_precompute_matrix() { return precompute_matrix ; }
282 
288  inline virtual void set_precompute_matrix(bool flag)
289  {
290  precompute_matrix=flag;
291 
292  if (!precompute_matrix)
293  {
295  precomputed_matrix=NULL;
296  }
297  }
298 
303  virtual int32_t get_num_vec_lhs()
304  {
305  return num_lhs;
306  }
307 
312  virtual int32_t get_num_vec_rhs()
313  {
314  return num_rhs;
315  }
316 
321  virtual bool has_features()
322  {
323  return lhs && rhs;
324  }
325 
330  inline bool lhs_equals_rhs()
331  {
332  return lhs==rhs;
333  }
334 
335  protected:
336 
338  static void* run_distance_thread(void* p);
339 
343  virtual float64_t compute(int32_t x, int32_t y)=0;
344 
346  void do_precompute_matrix();
347 
348  private:
349  void init();
350 
351  protected:
356 
361 
366 
368  int32_t num_lhs;
370  int32_t num_rhs;
371 
372 };
373 } // namespace shogun
374 #endif

SHOGUN Machine Learning Toolbox - Documentation