Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef _DISTANCE_H___
00013 #define _DISTANCE_H___
00014
00015 #include <stdio.h>
00016
00017 #include "lib/common.h"
00018 #include "lib/File.h"
00019 #include "lib/Mathematics.h"
00020 #include "base/SGObject.h"
00021 #include "features/Features.h"
00022
00023 namespace shogun
00024 {
00025 class CFile;
00026 class CMath;
00027 class CFeatures;
00028 enum EFeatureType;
00029 enum EFeatureClass;
00030
00031 enum EDistanceType
00032 {
00033 D_UNKNOWN = 0,
00034 D_MINKOWSKI = 10,
00035 D_MANHATTAN = 20,
00036 D_CANBERRA = 30,
00037 D_CHEBYSHEW = 40,
00038 D_GEODESIC = 50,
00039 D_JENSEN = 60,
00040 D_MANHATTANWORD = 70,
00041 D_HAMMINGWORD = 80 ,
00042 D_CANBERRAWORD = 90,
00043 D_SPARSEEUCLIDIAN = 100,
00044 D_EUCLIDIAN = 110,
00045 D_CHISQUARE = 120,
00046 D_TANIMOTO = 130,
00047 D_COSINE = 140,
00048 D_BRAYCURTIS =150,
00049 D_CUSTOM = 160
00050 };
00051
00052
00056 class CDistance : public CSGObject
00057 {
00058 public:
00060 CDistance();
00061
00068 CDistance(CFeatures* lhs, CFeatures* rhs);
00069 virtual ~CDistance();
00070
00078 inline float64_t distance(int32_t idx_a, int32_t idx_b)
00079 {
00080 if (idx_a < 0 || idx_b <0)
00081 return 0;
00082
00083 ASSERT(lhs);
00084 ASSERT(rhs);
00085
00086 if (lhs==rhs)
00087 {
00088 int32_t num_vectors = lhs->get_num_vectors();
00089
00090 if (idx_a>=num_vectors)
00091 idx_a=2*num_vectors-1-idx_a;
00092
00093 if (idx_b>=num_vectors)
00094 idx_b=2*num_vectors-1-idx_b;
00095 }
00096
00097 if (precompute_matrix && (precomputed_matrix==NULL) && (lhs==rhs))
00098 do_precompute_matrix() ;
00099
00100 if (precompute_matrix && (precomputed_matrix!=NULL))
00101 {
00102 if (idx_a>=idx_b)
00103 return precomputed_matrix[idx_a*(idx_a+1)/2+idx_b] ;
00104 else
00105 return precomputed_matrix[idx_b*(idx_b+1)/2+idx_a] ;
00106 }
00107
00108 return compute(idx_a, idx_b);
00109 }
00110
00117 void get_distance_matrix(float64_t** dst,int32_t* m, int32_t* n);
00118
00126 virtual float64_t* get_distance_matrix_real(
00127 int32_t &m,int32_t &n, float64_t* target);
00128
00136 virtual float32_t* get_distance_matrix_shortreal(
00137 int32_t &m,int32_t &n,float32_t* target);
00138
00148 virtual bool init(CFeatures* lhs, CFeatures* rhs);
00149
00154 virtual void cleanup()=0;
00155
00160 void load(CFile* loader);
00161
00166 void save(CFile* writer);
00167
00172 inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; };
00173
00178 inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; };
00179
00188 CFeatures* replace_rhs(CFeatures* rhs);
00189
00191 virtual void remove_lhs_and_rhs();
00192
00194 virtual void remove_lhs();
00195
00197 virtual void remove_rhs();
00198
00205 virtual EDistanceType get_distance_type()=0 ;
00206
00213 virtual EFeatureType get_feature_type()=0;
00214
00221 virtual EFeatureClass get_feature_class()=0;
00222
00228 inline bool get_precompute_matrix() { return precompute_matrix ; }
00229
00235 inline virtual void set_precompute_matrix(bool flag)
00236 {
00237 precompute_matrix=flag;
00238
00239 if (!precompute_matrix)
00240 {
00241 delete[] precomputed_matrix;
00242 precomputed_matrix=NULL;
00243 }
00244 }
00245
00250 inline int32_t get_num_vec_lhs()
00251 {
00252 if (!lhs)
00253 return 0;
00254 else
00255 return lhs->get_num_vectors();
00256 }
00257
00262 inline int32_t get_num_vec_rhs()
00263 {
00264 if (!rhs)
00265 return 0;
00266 else
00267 return rhs->get_num_vectors();
00268 }
00269
00274 inline bool has_features()
00275 {
00276 return lhs && rhs;
00277 }
00278
00283 inline bool lhs_equals_rhs()
00284 {
00285 return lhs==rhs;
00286 }
00287
00288 protected:
00292 virtual float64_t compute(int32_t x, int32_t y)=0;
00293
00295 void do_precompute_matrix();
00296
00297 private:
00298 void init();
00299
00300 protected:
00304 float32_t * precomputed_matrix;
00305
00309 bool precompute_matrix;
00310
00312 CFeatures* lhs;
00314 CFeatures* rhs;
00315
00316 };
00317 }
00318 #endif