SHOGUN
v2.0.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_SPARSEEUCLIDEAN = 100, 00044 D_EUCLIDEAN = 110, 00045 D_CHISQUARE = 120, 00046 D_TANIMOTO = 130, 00047 D_COSINE = 140, 00048 D_BRAYCURTIS = 150, 00049 D_CUSTOM = 160, 00050 D_ATTENUATEDEUCLIDEAN = 170, 00051 D_MAHALANOBIS = 180, 00052 D_DIRECTOR = 190 00053 }; 00054 00055 00079 class CDistance : public CSGObject 00080 { 00081 public: 00083 CDistance(); 00084 00091 CDistance(CFeatures* lhs, CFeatures* rhs); 00092 virtual ~CDistance(); 00093 00101 virtual float64_t distance(int32_t idx_a, int32_t idx_b) 00102 { 00103 if (idx_a < 0 || idx_b <0) 00104 return 0; 00105 00106 ASSERT(lhs); 00107 ASSERT(rhs); 00108 00109 if (lhs==rhs) 00110 { 00111 int32_t num_vectors = lhs->get_num_vectors(); 00112 00113 if (idx_a>=num_vectors) 00114 idx_a=2*num_vectors-1-idx_a; 00115 00116 if (idx_b>=num_vectors) 00117 idx_b=2*num_vectors-1-idx_b; 00118 } 00119 00120 ASSERT(idx_a<lhs->get_num_vectors()); 00121 ASSERT(idx_b<rhs->get_num_vectors()); 00122 00123 if (precompute_matrix && (precomputed_matrix==NULL) && (lhs==rhs)) 00124 do_precompute_matrix() ; 00125 00126 if (precompute_matrix && (precomputed_matrix!=NULL)) 00127 { 00128 if (idx_a>=idx_b) 00129 return precomputed_matrix[idx_a*(idx_a+1)/2+idx_b] ; 00130 else 00131 return precomputed_matrix[idx_b*(idx_b+1)/2+idx_a] ; 00132 } 00133 00134 return compute(idx_a, idx_b); 00135 } 00136 00150 virtual float64_t distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound) 00151 { 00152 return distance(idx_a, idx_b); 00153 } 00154 00155 00160 SGMatrix<float64_t> get_distance_matrix(); 00161 00169 virtual float64_t* get_distance_matrix_real( 00170 int32_t &m,int32_t &n, float64_t* target); 00171 00179 virtual float32_t* get_distance_matrix_shortreal( 00180 int32_t &m,int32_t &n,float32_t* target); 00181 00191 virtual bool init(CFeatures* lhs, CFeatures* rhs); 00192 00197 virtual void cleanup()=0; 00198 00203 void load(CFile* loader); 00204 00209 void save(CFile* writer); 00210 00215 inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; }; 00216 00221 inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; }; 00222 00231 CFeatures* replace_rhs(CFeatures* rhs); 00232 00241 CFeatures* replace_lhs(CFeatures* lhs); 00242 00244 virtual void remove_lhs_and_rhs(); 00245 00247 virtual void remove_lhs(); 00248 00250 virtual void remove_rhs(); 00251 00258 virtual EDistanceType get_distance_type()=0 ; 00259 00266 virtual EFeatureType get_feature_type()=0; 00267 00274 virtual EFeatureClass get_feature_class()=0; 00275 00281 inline bool get_precompute_matrix() { return precompute_matrix ; } 00282 00288 inline virtual void set_precompute_matrix(bool flag) 00289 { 00290 precompute_matrix=flag; 00291 00292 if (!precompute_matrix) 00293 { 00294 SG_FREE(precomputed_matrix); 00295 precomputed_matrix=NULL; 00296 } 00297 } 00298 00303 virtual int32_t get_num_vec_lhs() 00304 { 00305 return num_lhs; 00306 } 00307 00312 virtual int32_t get_num_vec_rhs() 00313 { 00314 return num_rhs; 00315 } 00316 00321 virtual bool has_features() 00322 { 00323 return lhs && rhs; 00324 } 00325 00330 inline bool lhs_equals_rhs() 00331 { 00332 return lhs==rhs; 00333 } 00334 00335 protected: 00336 00338 static void* run_distance_thread(void* p); 00339 00343 virtual float64_t compute(int32_t x, int32_t y)=0; 00344 00346 void do_precompute_matrix(); 00347 00348 private: 00349 void init(); 00350 00351 protected: 00355 float32_t * precomputed_matrix; 00356 00360 bool precompute_matrix; 00361 00363 CFeatures* lhs; 00365 CFeatures* rhs; 00366 00368 int32_t num_lhs; 00370 int32_t num_rhs; 00371 00372 }; 00373 } // namespace shogun 00374 #endif