CustomKernel.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) 1999-2009 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #ifndef _CUSTOMKERNEL_H___
00012 #define _CUSTOMKERNEL_H___
00013 
00014 #include "lib/Mathematics.h"
00015 #include "lib/common.h"
00016 #include "kernel/Kernel.h"
00017 #include "features/Features.h"
00018 
00027 class CCustomKernel: public CKernel
00028 {
00029     public:
00031         CCustomKernel();
00032 
00038         CCustomKernel(CKernel* k);
00039 
00040         virtual ~CCustomKernel();
00041 
00049         virtual float32_t* get_kernel_matrix_shortreal(
00050             int32_t &m, int32_t &n, float32_t* target=NULL);
00051 
00062         virtual bool dummy_init(int32_t rows, int32_t cols);
00063 
00070         virtual bool init(CFeatures* l, CFeatures* r);
00071 
00073         virtual void cleanup();
00074 
00080         virtual bool load_init(FILE* src);
00081 
00087         virtual bool save_init(FILE* dest);
00088 
00093         inline virtual EKernelType get_kernel_type() { return K_CUSTOM; }
00094 
00099         inline virtual EFeatureType get_feature_type() { return F_ANY; }
00100 
00105         inline virtual EFeatureClass get_feature_class() { return C_ANY; }
00106 
00111         virtual const char* get_name() const { return "Custom"; }
00112 
00121         bool set_triangle_kernel_matrix_from_triangle(
00122             const float64_t* km, int32_t len);
00123 
00132         bool set_triangle_kernel_matrix_from_full(
00133             const float64_t* km, int32_t rows, int32_t cols);
00134 
00142         bool set_full_kernel_matrix_from_full(
00143             const float64_t* km, int32_t rows, int32_t cols);
00144 
00149         virtual inline int32_t get_num_vec_lhs()
00150         {
00151             return num_rows;
00152         }
00153 
00158         virtual inline int32_t get_num_vec_rhs()
00159         {
00160             return num_cols;
00161         }
00162 
00167         virtual inline bool has_features()
00168         {
00169             return (num_rows>0) && (num_cols>0);
00170         }
00171 
00172     protected:
00179         inline virtual float64_t compute(int32_t row, int32_t col)
00180         {
00181             ASSERT(row<num_rows);
00182             ASSERT(col<num_cols);
00183             ASSERT(kmatrix);
00184 
00185             if (upper_diagonal)
00186             {
00187                 if (row <= col)
00188                     return kmatrix[row*num_cols - row*(row+1)/2 + col];
00189                 else
00190                     return kmatrix[col*num_cols - col*(col+1)/2 + row];
00191             }
00192             else
00193                 return kmatrix[row*num_cols+col];
00194         }
00195 
00196     private:
00198         void cleanup_custom();
00199 
00200     protected:
00202         float32_t* kmatrix;
00204         int32_t num_rows;
00206         int32_t num_cols;
00208         bool upper_diagonal;
00209 };
00210 #endif /* _CUSTOMKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation