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 
00019 namespace shogun
00020 {
00029 class CCustomKernel: public CKernel
00030 {
00031     public:
00033         CCustomKernel();
00034 
00040         CCustomKernel(CKernel* k);
00041 
00051         CCustomKernel(
00052             const float64_t* km, int32_t rows, int32_t cols);
00053 
00054         virtual ~CCustomKernel();
00055 
00066         virtual bool dummy_init(int32_t rows, int32_t cols);
00067 
00074         virtual bool init(CFeatures* l, CFeatures* r);
00075 
00077         virtual void cleanup();
00078 
00083         inline virtual EKernelType get_kernel_type() { return K_CUSTOM; }
00084 
00089         inline virtual EFeatureType get_feature_type() { return F_ANY; }
00090 
00095         inline virtual EFeatureClass get_feature_class() { return C_ANY; }
00096 
00101         virtual const char* get_name() const { return "Custom"; }
00102 
00111         bool set_triangle_kernel_matrix_from_triangle(
00112             const float64_t* km, int32_t len);
00113 
00122         bool set_triangle_kernel_matrix_from_full(
00123             const float64_t* km, int32_t rows, int32_t cols);
00124 
00132         bool set_full_kernel_matrix_from_full(
00133             const float64_t* km, int32_t rows, int32_t cols);
00134 
00139         virtual inline int32_t get_num_vec_lhs()
00140         {
00141             return num_rows;
00142         }
00143 
00148         virtual inline int32_t get_num_vec_rhs()
00149         {
00150             return num_cols;
00151         }
00152 
00157         virtual inline bool has_features()
00158         {
00159             return (num_rows>0) && (num_cols>0);
00160         }
00161 
00162     protected:
00169         inline virtual float64_t compute(int32_t row, int32_t col)
00170         {
00171             ASSERT(kmatrix);
00172 
00173             if (upper_diagonal)
00174             {
00175                 if (row <= col)
00176                     return kmatrix[row*num_cols - row*(row+1)/2 + col];
00177                 else
00178                     return kmatrix[col*num_cols - col*(col+1)/2 + row];
00179             }
00180             else
00181                 return kmatrix[row*num_cols+col];
00182         }
00183 
00184     private:
00186         void cleanup_custom();
00187 
00188     protected:
00190         float32_t* kmatrix;
00192         int32_t num_rows;
00194         int32_t num_cols;
00196         bool upper_diagonal;
00197 };
00198 }
00199 #endif /* _CUSTOMKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation