SHOGUN
v1.1.0
|
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 <shogun/lib/common.h> 00018 #include <shogun/io/File.h> 00019 #include <shogun/mathematics/Math.h> 00020 #include <shogun/base/SGObject.h> 00021 #include <shogun/features/FeatureTypes.h> 00022 #include <shogun/features/Features.h> 00023 00024 namespace shogun 00025 { 00026 class CFile; 00027 class CMath; 00028 class CFeatures; 00029 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 D_ATTENUATEDEUCLIDIAN = 170 00051 }; 00052 00053 00077 class CDistance : public CSGObject 00078 { 00079 public: 00081 CDistance(); 00082 00089 CDistance(CFeatures* lhs, CFeatures* rhs); 00090 virtual ~CDistance(); 00091 00099 inline float64_t distance(int32_t idx_a, int32_t idx_b) 00100 { 00101 if (idx_a < 0 || idx_b <0) 00102 return 0; 00103 00104 ASSERT(lhs); 00105 ASSERT(rhs); 00106 00107 if (lhs==rhs) 00108 { 00109 int32_t num_vectors = lhs->get_num_vectors(); 00110 00111 if (idx_a>=num_vectors) 00112 idx_a=2*num_vectors-1-idx_a; 00113 00114 if (idx_b>=num_vectors) 00115 idx_b=2*num_vectors-1-idx_b; 00116 } 00117 00118 ASSERT(idx_a<lhs->get_num_vectors()); 00119 ASSERT(idx_b<rhs->get_num_vectors()); 00120 00121 if (precompute_matrix && (precomputed_matrix==NULL) && (lhs==rhs)) 00122 do_precompute_matrix() ; 00123 00124 if (precompute_matrix && (precomputed_matrix!=NULL)) 00125 { 00126 if (idx_a>=idx_b) 00127 return precomputed_matrix[idx_a*(idx_a+1)/2+idx_b] ; 00128 else 00129 return precomputed_matrix[idx_b*(idx_b+1)/2+idx_a] ; 00130 } 00131 00132 return compute(idx_a, idx_b); 00133 } 00134 00139 SGMatrix<float64_t> get_distance_matrix(); 00140 00148 virtual float64_t* get_distance_matrix_real( 00149 int32_t &m,int32_t &n, float64_t* target); 00150 00158 virtual float32_t* get_distance_matrix_shortreal( 00159 int32_t &m,int32_t &n,float32_t* target); 00160 00170 virtual bool init(CFeatures* lhs, CFeatures* rhs); 00171 00176 virtual void cleanup()=0; 00177 00182 void load(CFile* loader); 00183 00188 void save(CFile* writer); 00189 00194 inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; }; 00195 00200 inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; }; 00201 00210 CFeatures* replace_rhs(CFeatures* rhs); 00211 00213 virtual void remove_lhs_and_rhs(); 00214 00216 virtual void remove_lhs(); 00217 00219 virtual void remove_rhs(); 00220 00227 virtual EDistanceType get_distance_type()=0 ; 00228 00235 virtual EFeatureType get_feature_type()=0; 00236 00243 virtual EFeatureClass get_feature_class()=0; 00244 00250 inline bool get_precompute_matrix() { return precompute_matrix ; } 00251 00257 inline virtual void set_precompute_matrix(bool flag) 00258 { 00259 precompute_matrix=flag; 00260 00261 if (!precompute_matrix) 00262 { 00263 SG_FREE(precomputed_matrix); 00264 precomputed_matrix=NULL; 00265 } 00266 } 00267 00272 inline int32_t get_num_vec_lhs() 00273 { 00274 if (!lhs) 00275 return 0; 00276 else 00277 return lhs->get_num_vectors(); 00278 } 00279 00284 inline int32_t get_num_vec_rhs() 00285 { 00286 if (!rhs) 00287 return 0; 00288 else 00289 return rhs->get_num_vectors(); 00290 } 00291 00296 inline bool has_features() 00297 { 00298 return lhs && rhs; 00299 } 00300 00305 inline bool lhs_equals_rhs() 00306 { 00307 return lhs==rhs; 00308 } 00309 00310 protected: 00311 00313 static void* run_distance_thread(void* p); 00314 00318 virtual float64_t compute(int32_t x, int32_t y)=0; 00319 00321 void do_precompute_matrix(); 00322 00323 private: 00324 void init(); 00325 00326 protected: 00330 float32_t * precomputed_matrix; 00331 00335 bool precompute_matrix; 00336 00338 CFeatures* lhs; 00340 CFeatures* rhs; 00341 00342 }; 00343 } // namespace shogun 00344 #endif