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 "lib/common.h"
00016 #include "lib/Mathematics.h"
00017 #include "base/SGObject.h"
00018 #include "features/Features.h"
00019 
00020 #include <stdio.h>
00021 
00023 class CDistance : public CSGObject
00024 {
00025     public:
00027         CDistance();
00028 
00035         CDistance(CFeatures* lhs, CFeatures* rhs);
00036         virtual ~CDistance();
00037 
00045         inline DREAL distance(INT idx_a, INT idx_b)
00046         {
00047             if (idx_a < 0 || idx_b <0)
00048                 return 0;
00049 
00050             ASSERT(lhs);
00051             ASSERT(rhs);
00052 
00053             if (lhs==rhs)
00054             {
00055                 INT num_vectors = lhs->get_num_vectors();
00056 
00057                 if (idx_a>=num_vectors)
00058                     idx_a=2*num_vectors-1-idx_a;
00059 
00060                 if (idx_b>=num_vectors)
00061                     idx_b=2*num_vectors-1-idx_b;
00062             }
00063 
00064             if (precompute_matrix && (precomputed_matrix==NULL) && (lhs==rhs))
00065                 do_precompute_matrix() ;
00066 
00067             if (precompute_matrix && (precomputed_matrix!=NULL))
00068             {
00069                 if (idx_a>=idx_b)
00070                     return precomputed_matrix[idx_a*(idx_a+1)/2+idx_b] ;
00071                 else
00072                     return precomputed_matrix[idx_b*(idx_b+1)/2+idx_a] ;
00073             }
00074 
00075             return compute(idx_a, idx_b);
00076         }
00077 
00084         void get_distance_matrix(DREAL** dst,INT* m, INT* n);
00085 
00093         virtual DREAL* get_distance_matrix_real(int &m,int &n, DREAL* target);
00094 
00102         virtual SHORTREAL* get_distance_matrix_shortreal(int &m,int &n,SHORTREAL* target);
00103 
00113         virtual bool init(CFeatures* lhs, CFeatures* rhs);
00114 
00119         virtual void cleanup()=0;
00120 
00126         bool load(CHAR* fname);
00127 
00133         bool save(CHAR* fname);
00134 
00142         virtual bool load_init(FILE* src)=0;
00143 
00151         virtual bool save_init(FILE* dest)=0;
00152         
00157         inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; };
00158 
00163         inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; };
00164 
00166         virtual void remove_lhs();
00167 
00169         virtual void remove_rhs();
00170         
00177         virtual EDistanceType get_distance_type()=0 ;
00178 
00185         virtual EFeatureType get_feature_type()=0;
00186 
00193         virtual EFeatureClass get_feature_class()=0;
00194 
00201         virtual const CHAR* get_name()=0 ;
00202 
00203 
00209         inline bool get_precompute_matrix() { return precompute_matrix ;  }
00210 
00216         inline virtual void set_precompute_matrix(bool flag)
00217         { 
00218             precompute_matrix=flag;
00219         
00220             if (!precompute_matrix)
00221             {
00222                 delete[] precomputed_matrix;
00223                 precomputed_matrix=NULL;
00224             }
00225         }
00226 
00227     protected:
00231         virtual DREAL compute(INT x, INT y)=0;
00232 
00234         void do_precompute_matrix();
00235 
00236     protected:
00240         SHORTREAL * precomputed_matrix;
00241 
00245         bool precompute_matrix;
00246 
00248         CFeatures* lhs;
00250         CFeatures* rhs;
00251 
00252 };
00253 #endif

SHOGUN Machine Learning Toolbox - Documentation