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 Christian Gehl
00008  * Written (W) 2006-2008 Soeren Sonnenburg
00009  * Copyright (C) 2006-2008 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/Mathematics.h"
00019 #include "base/SGObject.h"
00020 #include "features/Features.h"
00021 
00022 
00023 enum EDistanceType
00024 {
00025     D_UNKNOWN = 0,
00026     D_MINKOWSKI = 10,
00027     D_MANHATTAN = 20,
00028     D_CANBERRA = 30,
00029     D_CHEBYSHEW = 40,
00030     D_GEODESIC = 50,
00031     D_JENSEN = 60,
00032     D_MANHATTANWORD = 70,
00033     D_HAMMINGWORD = 80 ,
00034     D_CANBERRAWORD = 90,
00035     D_SPARSEEUCLIDIAN = 100,
00036     D_EUCLIDIAN = 110,
00037     D_CHISQUARE = 120,
00038     D_TANIMOTO = 130,
00039     D_COSINE = 140,
00040     D_BRAYCURTIS =150
00041 };
00042 
00043 
00045 class CDistance : public CSGObject
00046 {
00047     public:
00049         CDistance();
00050 
00057         CDistance(CFeatures* lhs, CFeatures* rhs);
00058         virtual ~CDistance();
00059 
00067         inline float64_t distance(int32_t idx_a, int32_t idx_b)
00068         {
00069             if (idx_a < 0 || idx_b <0)
00070                 return 0;
00071 
00072             ASSERT(lhs);
00073             ASSERT(rhs);
00074 
00075             if (lhs==rhs)
00076             {
00077                 int32_t num_vectors = lhs->get_num_vectors();
00078 
00079                 if (idx_a>=num_vectors)
00080                     idx_a=2*num_vectors-1-idx_a;
00081 
00082                 if (idx_b>=num_vectors)
00083                     idx_b=2*num_vectors-1-idx_b;
00084             }
00085 
00086             if (precompute_matrix && (precomputed_matrix==NULL) && (lhs==rhs))
00087                 do_precompute_matrix() ;
00088 
00089             if (precompute_matrix && (precomputed_matrix!=NULL))
00090             {
00091                 if (idx_a>=idx_b)
00092                     return precomputed_matrix[idx_a*(idx_a+1)/2+idx_b] ;
00093                 else
00094                     return precomputed_matrix[idx_b*(idx_b+1)/2+idx_a] ;
00095             }
00096 
00097             return compute(idx_a, idx_b);
00098         }
00099 
00106         void get_distance_matrix(float64_t** dst,int32_t* m, int32_t* n);
00107 
00115         virtual float64_t* get_distance_matrix_real(
00116             int32_t &m,int32_t &n, float64_t* target);
00117 
00125         virtual float32_t* get_distance_matrix_shortreal(
00126             int32_t &m,int32_t &n,float32_t* target);
00127 
00137         virtual bool init(CFeatures* lhs, CFeatures* rhs);
00138 
00143         virtual void cleanup()=0;
00144 
00150         bool load(char* fname);
00151 
00157         bool save(char* fname);
00158 
00166         virtual bool load_init(FILE* src)=0;
00167 
00175         virtual bool save_init(FILE* dest)=0;
00176         
00181         inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; };
00182 
00187         inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; };
00188 
00190         virtual void remove_lhs();
00191 
00193         virtual void remove_rhs();
00194         
00201         virtual EDistanceType get_distance_type()=0 ;
00202 
00209         virtual EFeatureType get_feature_type()=0;
00210 
00217         virtual EFeatureClass get_feature_class()=0;
00218 
00225         virtual const char* get_name()=0 ;
00226 
00227 
00233         inline bool get_precompute_matrix() { return precompute_matrix ;  }
00234 
00240         inline virtual void set_precompute_matrix(bool flag)
00241         { 
00242             precompute_matrix=flag;
00243         
00244             if (!precompute_matrix)
00245             {
00246                 delete[] precomputed_matrix;
00247                 precomputed_matrix=NULL;
00248             }
00249         }
00250 
00251     protected:
00255         virtual float64_t compute(int32_t x, int32_t y)=0;
00256 
00258         void do_precompute_matrix();
00259 
00260     protected:
00264         float32_t * precomputed_matrix;
00265 
00269         bool precompute_matrix;
00270 
00272         CFeatures* lhs;
00274         CFeatures* rhs;
00275 
00276 };
00277 #endif

SHOGUN Machine Learning Toolbox - Documentation