|
SHOGUN v0.9.3
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2006-2009 Christian Gehl 00008 * Written (W) 2006-2009 Soeren Sonnenburg 00009 * Copyright (C) 2006-2009 Fraunhofer Institute FIRST and Max-Planck-Society 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 }; 00050 00051 00055 class CDistance : public CSGObject 00056 { 00057 public: 00059 CDistance(); 00060 00067 CDistance(CFeatures* lhs, CFeatures* rhs); 00068 virtual ~CDistance(); 00069 00077 inline float64_t distance(int32_t idx_a, int32_t idx_b) 00078 { 00079 if (idx_a < 0 || idx_b <0) 00080 return 0; 00081 00082 ASSERT(lhs); 00083 ASSERT(rhs); 00084 00085 if (lhs==rhs) 00086 { 00087 int32_t num_vectors = lhs->get_num_vectors(); 00088 00089 if (idx_a>=num_vectors) 00090 idx_a=2*num_vectors-1-idx_a; 00091 00092 if (idx_b>=num_vectors) 00093 idx_b=2*num_vectors-1-idx_b; 00094 } 00095 00096 if (precompute_matrix && (precomputed_matrix==NULL) && (lhs==rhs)) 00097 do_precompute_matrix() ; 00098 00099 if (precompute_matrix && (precomputed_matrix!=NULL)) 00100 { 00101 if (idx_a>=idx_b) 00102 return precomputed_matrix[idx_a*(idx_a+1)/2+idx_b] ; 00103 else 00104 return precomputed_matrix[idx_b*(idx_b+1)/2+idx_a] ; 00105 } 00106 00107 return compute(idx_a, idx_b); 00108 } 00109 00116 void get_distance_matrix(float64_t** dst,int32_t* m, int32_t* n); 00117 00125 virtual float64_t* get_distance_matrix_real( 00126 int32_t &m,int32_t &n, float64_t* target); 00127 00135 virtual float32_t* get_distance_matrix_shortreal( 00136 int32_t &m,int32_t &n,float32_t* target); 00137 00147 virtual bool init(CFeatures* lhs, CFeatures* rhs); 00148 00153 virtual void cleanup()=0; 00154 00159 void load(CFile* loader); 00160 00165 void save(CFile* writer); 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 } // namespace shogun 00314 #endif