Distance.h

Go to the documentation of this file.
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 enum EFeatureType;
00030 enum EFeatureClass;
00031 
00033 enum EDistanceType
00034 {
00035     D_UNKNOWN = 0,
00036     D_MINKOWSKI = 10,
00037     D_MANHATTAN = 20,
00038     D_CANBERRA = 30,
00039     D_CHEBYSHEW = 40,
00040     D_GEODESIC = 50,
00041     D_JENSEN = 60,
00042     D_MANHATTANWORD = 70,
00043     D_HAMMINGWORD = 80 ,
00044     D_CANBERRAWORD = 90,
00045     D_SPARSEEUCLIDIAN = 100,
00046     D_EUCLIDIAN = 110,
00047     D_CHISQUARE = 120,
00048     D_TANIMOTO = 130,
00049     D_COSINE = 140,
00050     D_BRAYCURTIS = 150,
00051     D_CUSTOM = 160,
00052     D_ATTENUATEDEUCLIDIAN = 170
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         inline 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 
00141         SGMatrix<float64_t> get_distance_matrix();
00142 
00150         virtual float64_t* get_distance_matrix_real(
00151             int32_t &m,int32_t &n, float64_t* target);
00152 
00160         virtual float32_t* get_distance_matrix_shortreal(
00161             int32_t &m,int32_t &n,float32_t* target);
00162 
00172         virtual bool init(CFeatures* lhs, CFeatures* rhs);
00173 
00178         virtual void cleanup()=0;
00179 
00184         void load(CFile* loader);
00185 
00190         void save(CFile* writer);
00191 
00196         inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; };
00197 
00202         inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; };
00203     
00212         CFeatures* replace_rhs(CFeatures* rhs);
00213             
00215         virtual void remove_lhs_and_rhs();
00216 
00218         virtual void remove_lhs();
00219 
00221         virtual void remove_rhs();
00222         
00229         virtual EDistanceType get_distance_type()=0 ;
00230 
00237         virtual EFeatureType get_feature_type()=0;
00238 
00245         virtual EFeatureClass get_feature_class()=0;
00246 
00252         inline bool get_precompute_matrix() { return precompute_matrix ;  }
00253 
00259         inline virtual void set_precompute_matrix(bool flag)
00260         { 
00261             precompute_matrix=flag;
00262         
00263             if (!precompute_matrix)
00264             {
00265                 SG_FREE(precomputed_matrix);
00266                 precomputed_matrix=NULL;
00267             }
00268         }
00269 
00274         inline int32_t get_num_vec_lhs()
00275         {
00276             if (!lhs)
00277                 return 0;
00278             else
00279                 return lhs->get_num_vectors();
00280         }
00281 
00286         inline int32_t get_num_vec_rhs()
00287         {
00288             if (!rhs)
00289                 return 0;
00290             else
00291                 return rhs->get_num_vectors();
00292         }
00293 
00298         inline bool has_features()
00299         {
00300             return lhs && rhs;
00301         }
00302 
00307         inline bool lhs_equals_rhs()
00308         {
00309             return lhs==rhs;
00310         }
00311 
00312     protected:
00313 
00315         static void* run_distance_thread(void* p);      
00316 
00320         virtual float64_t compute(int32_t x, int32_t y)=0;
00321 
00323         void do_precompute_matrix();
00324 
00325     private:
00326         void init();
00327 
00328     protected:
00332         float32_t * precomputed_matrix;
00333 
00337         bool precompute_matrix;
00338 
00340         CFeatures* lhs;
00342         CFeatures* rhs;
00343 
00344 };
00345 } // namespace shogun
00346 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation