Distance.h
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/Mathematics.h"
00019 #include "base/SGObject.h"
00020 #include "features/Features.h"
00021
00022 namespace shogun
00023 {
00024 class CMath;
00025 class CFeatures;
00026 enum EFeatureType;
00027 enum EFeatureClass;
00028
00029 enum EDistanceType
00030 {
00031 D_UNKNOWN = 0,
00032 D_MINKOWSKI = 10,
00033 D_MANHATTAN = 20,
00034 D_CANBERRA = 30,
00035 D_CHEBYSHEW = 40,
00036 D_GEODESIC = 50,
00037 D_JENSEN = 60,
00038 D_MANHATTANWORD = 70,
00039 D_HAMMINGWORD = 80 ,
00040 D_CANBERRAWORD = 90,
00041 D_SPARSEEUCLIDIAN = 100,
00042 D_EUCLIDIAN = 110,
00043 D_CHISQUARE = 120,
00044 D_TANIMOTO = 130,
00045 D_COSINE = 140,
00046 D_BRAYCURTIS =150
00047 };
00048
00049
00053 class CDistance : public CSGObject
00054 {
00055 public:
00057 CDistance();
00058
00065 CDistance(CFeatures* lhs, CFeatures* rhs);
00066 virtual ~CDistance();
00067
00075 inline float64_t distance(int32_t idx_a, int32_t idx_b)
00076 {
00077 if (idx_a < 0 || idx_b <0)
00078 return 0;
00079
00080 ASSERT(lhs);
00081 ASSERT(rhs);
00082
00083 if (lhs==rhs)
00084 {
00085 int32_t num_vectors = lhs->get_num_vectors();
00086
00087 if (idx_a>=num_vectors)
00088 idx_a=2*num_vectors-1-idx_a;
00089
00090 if (idx_b>=num_vectors)
00091 idx_b=2*num_vectors-1-idx_b;
00092 }
00093
00094 if (precompute_matrix && (precomputed_matrix==NULL) && (lhs==rhs))
00095 do_precompute_matrix() ;
00096
00097 if (precompute_matrix && (precomputed_matrix!=NULL))
00098 {
00099 if (idx_a>=idx_b)
00100 return precomputed_matrix[idx_a*(idx_a+1)/2+idx_b] ;
00101 else
00102 return precomputed_matrix[idx_b*(idx_b+1)/2+idx_a] ;
00103 }
00104
00105 return compute(idx_a, idx_b);
00106 }
00107
00114 void get_distance_matrix(float64_t** dst,int32_t* m, int32_t* n);
00115
00123 virtual float64_t* get_distance_matrix_real(
00124 int32_t &m,int32_t &n, float64_t* target);
00125
00133 virtual float32_t* get_distance_matrix_shortreal(
00134 int32_t &m,int32_t &n,float32_t* target);
00135
00145 virtual bool init(CFeatures* lhs, CFeatures* rhs);
00146
00151 virtual void cleanup()=0;
00152
00158 bool load(char* fname);
00159
00165 bool save(char* fname);
00166
00171 inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; };
00172
00177 inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; };
00178
00187 CFeatures* replace_rhs(CFeatures* rhs);
00188
00190 virtual void remove_lhs_and_rhs();
00191
00193 virtual void remove_lhs();
00194
00196 virtual void remove_rhs();
00197
00204 virtual EDistanceType get_distance_type()=0 ;
00205
00212 virtual EFeatureType get_feature_type()=0;
00213
00220 virtual EFeatureClass get_feature_class()=0;
00221
00227 inline bool get_precompute_matrix() { return precompute_matrix ; }
00228
00234 inline virtual void set_precompute_matrix(bool flag)
00235 {
00236 precompute_matrix=flag;
00237
00238 if (!precompute_matrix)
00239 {
00240 delete[] precomputed_matrix;
00241 precomputed_matrix=NULL;
00242 }
00243 }
00244
00249 inline int32_t get_num_vec_lhs()
00250 {
00251 if (!lhs)
00252 return 0;
00253 else
00254 return lhs->get_num_vectors();
00255 }
00256
00261 inline int32_t get_num_vec_rhs()
00262 {
00263 if (!rhs)
00264 return 0;
00265 else
00266 return rhs->get_num_vectors();
00267 }
00268
00273 inline bool has_features()
00274 {
00275 return lhs && rhs;
00276 }
00277
00282 inline bool lhs_equals_rhs()
00283 {
00284 return lhs==rhs;
00285 }
00286
00287 protected:
00291 virtual float64_t compute(int32_t x, int32_t y)=0;
00292
00294 void do_precompute_matrix();
00295
00296 protected:
00300 float32_t * precomputed_matrix;
00301
00305 bool precompute_matrix;
00306
00308 CFeatures* lhs;
00310 CFeatures* rhs;
00311
00312 };
00313 }
00314 #endif