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 "lib/common.h"
00018 #include "lib/Mathematics.h"
00019 #include "base/SGObject.h"
00020 #include "features/Features.h"
00021 
00022 enum EDistanceType
00023 {
00024     D_UNKNOWN = 0,
00025     D_MINKOWSKI = 10,
00026     D_MANHATTAN = 20,
00027     D_CANBERRA = 30,
00028     D_CHEBYSHEW = 40,
00029     D_GEODESIC = 50,
00030     D_JENSEN = 60,
00031     D_MANHATTANWORD = 70,
00032     D_HAMMINGWORD = 80 ,
00033     D_CANBERRAWORD = 90,
00034     D_SPARSEEUCLIDIAN = 100,
00035     D_EUCLIDIAN = 110,
00036     D_CHISQUARE = 120,
00037     D_TANIMOTO = 130,
00038     D_COSINE = 140,
00039     D_BRAYCURTIS =150
00040 };
00041 
00042 
00046 class CDistance : public CSGObject
00047 {
00048     public:
00050         CDistance();
00051 
00058         CDistance(CFeatures* lhs, CFeatures* rhs);
00059         virtual ~CDistance();
00060 
00068         inline float64_t distance(int32_t idx_a, int32_t idx_b)
00069         {
00070             if (idx_a < 0 || idx_b <0)
00071                 return 0;
00072 
00073             ASSERT(lhs);
00074             ASSERT(rhs);
00075 
00076             if (lhs==rhs)
00077             {
00078                 int32_t num_vectors = lhs->get_num_vectors();
00079 
00080                 if (idx_a>=num_vectors)
00081                     idx_a=2*num_vectors-1-idx_a;
00082 
00083                 if (idx_b>=num_vectors)
00084                     idx_b=2*num_vectors-1-idx_b;
00085             }
00086 
00087             if (precompute_matrix && (precomputed_matrix==NULL) && (lhs==rhs))
00088                 do_precompute_matrix() ;
00089 
00090             if (precompute_matrix && (precomputed_matrix!=NULL))
00091             {
00092                 if (idx_a>=idx_b)
00093                     return precomputed_matrix[idx_a*(idx_a+1)/2+idx_b] ;
00094                 else
00095                     return precomputed_matrix[idx_b*(idx_b+1)/2+idx_a] ;
00096             }
00097 
00098             return compute(idx_a, idx_b);
00099         }
00100 
00107         void get_distance_matrix(float64_t** dst,int32_t* m, int32_t* n);
00108 
00116         virtual float64_t* get_distance_matrix_real(
00117             int32_t &m,int32_t &n, float64_t* target);
00118 
00126         virtual float32_t* get_distance_matrix_shortreal(
00127             int32_t &m,int32_t &n,float32_t* target);
00128 
00138         virtual bool init(CFeatures* lhs, CFeatures* rhs);
00139 
00144         virtual void cleanup()=0;
00145 
00151         bool load(char* fname);
00152 
00158         bool save(char* fname);
00159 
00167         virtual bool load_init(FILE* src)=0;
00168 
00176         virtual bool save_init(FILE* dest)=0;
00177         
00182         inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; };
00183 
00188         inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; };
00189 
00191         virtual void remove_lhs_and_rhs();
00192 
00194         virtual void remove_lhs();
00195 
00197         virtual void remove_rhs();
00198         
00205         virtual EDistanceType get_distance_type()=0 ;
00206 
00213         virtual EFeatureType get_feature_type()=0;
00214 
00221         virtual EFeatureClass get_feature_class()=0;
00222 
00228         inline bool get_precompute_matrix() { return precompute_matrix ;  }
00229 
00235         inline virtual void set_precompute_matrix(bool flag)
00236         { 
00237             precompute_matrix=flag;
00238         
00239             if (!precompute_matrix)
00240             {
00241                 delete[] precomputed_matrix;
00242                 precomputed_matrix=NULL;
00243             }
00244         }
00245 
00250         inline int32_t get_num_vec_lhs()
00251         {
00252             if (!lhs)
00253                 return 0;
00254             else
00255                 return lhs->get_num_vectors();
00256         }
00257 
00262         inline int32_t get_num_vec_rhs()
00263         {
00264             if (!rhs)
00265                 return 0;
00266             else
00267                 return rhs->get_num_vectors();
00268         }
00269 
00274         inline bool has_features()
00275         {
00276             return lhs && rhs;
00277         }
00278 
00283         inline bool lhs_equals_rhs()
00284         {
00285             return lhs==rhs;
00286         }
00287 
00288     protected:
00292         virtual float64_t compute(int32_t x, int32_t y)=0;
00293 
00295         void do_precompute_matrix();
00296 
00297     protected:
00301         float32_t * precomputed_matrix;
00302 
00306         bool precompute_matrix;
00307 
00309         CFeatures* lhs;
00311         CFeatures* rhs;
00312 
00313 };
00314 #endif

SHOGUN Machine Learning Toolbox - Documentation