SHOGUN
v3.2.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 D_CUSTOMMAHALANOBIS = 200 00054 }; 00055 00056 00080 class CDistance : public CSGObject 00081 { 00082 public: 00084 CDistance(); 00085 00092 CDistance(CFeatures* lhs, CFeatures* rhs); 00093 virtual ~CDistance(); 00094 00102 virtual float64_t distance(int32_t idx_a, int32_t idx_b); 00103 00117 virtual float64_t distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound) 00118 { 00119 return distance(idx_a, idx_b); 00120 } 00121 00126 SGMatrix<float64_t> get_distance_matrix() 00127 { 00128 return get_distance_matrix<float64_t>(); 00129 } 00130 00135 template <class T> SGMatrix<T> get_distance_matrix(); 00136 00143 int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric) 00144 { 00145 int32_t i_start; 00146 00147 if (symmetric) 00148 i_start=(int32_t) CMath::floor(n-CMath::sqrt(CMath::sq((float64_t) n)-offs)); 00149 else 00150 i_start=(int32_t) (offs/int64_t(n)); 00151 00152 return i_start; 00153 } 00154 00159 template <class T> static void* get_distance_matrix_helper(void* p); 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 00220 CFeatures* replace_lhs(CFeatures* lhs); 00221 00223 virtual void remove_lhs_and_rhs(); 00224 00226 virtual void remove_lhs(); 00227 00229 virtual void remove_rhs(); 00230 00237 virtual EDistanceType get_distance_type()=0 ; 00238 00245 virtual EFeatureType get_feature_type()=0; 00246 00253 virtual EFeatureClass get_feature_class()=0; 00254 00260 inline bool get_precompute_matrix() { return precompute_matrix ; } 00261 00267 virtual void set_precompute_matrix(bool flag) 00268 { 00269 precompute_matrix=flag; 00270 00271 if (!precompute_matrix) 00272 { 00273 SG_FREE(precomputed_matrix); 00274 precomputed_matrix=NULL; 00275 } 00276 } 00277 00282 virtual int32_t get_num_vec_lhs() 00283 { 00284 return num_lhs; 00285 } 00286 00291 virtual int32_t get_num_vec_rhs() 00292 { 00293 return num_rhs; 00294 } 00295 00300 virtual bool has_features() 00301 { 00302 return lhs && rhs; 00303 } 00304 00309 inline bool lhs_equals_rhs() 00310 { 00311 return lhs==rhs; 00312 } 00313 00314 protected: 00315 00317 static void* run_distance_thread(void* p); 00318 00322 virtual float64_t compute(int32_t idx_a, int32_t idx_b)=0; 00323 00325 void do_precompute_matrix(); 00326 00327 private: 00328 void init(); 00329 00330 protected: 00334 float32_t * precomputed_matrix; 00335 00339 bool precompute_matrix; 00340 00342 CFeatures* lhs; 00344 CFeatures* rhs; 00345 00347 int32_t num_lhs; 00349 int32_t num_rhs; 00350 00351 }; 00352 } // namespace shogun 00353 #endif