LinearKernel.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-2008 Soeren Sonnenburg
00008  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #ifndef _LINEARKERNEL_H___
00012 #define _LINEARKERNEL_H___
00013 
00014 #include "lib/common.h"
00015 #include "kernel/SimpleKernel.h"
00016 #include "features/RealFeatures.h"
00017 
00023 class CLinearKernel: public CSimpleKernel<DREAL>
00024 {
00025     public:
00031         CLinearKernel(INT size, DREAL scale=1.0);
00032 
00040         CLinearKernel(CRealFeatures* l, CRealFeatures* r,
00041             DREAL scale=1.0, INT size=10);
00042 
00043         virtual ~CLinearKernel();
00044 
00051         virtual bool init(CFeatures* l, CFeatures* r);
00052 
00054         virtual void cleanup();
00055 
00061         virtual bool load_init(FILE* src);
00062 
00068         virtual bool save_init(FILE* dest);
00069 
00074         virtual EKernelType get_kernel_type() { return K_LINEAR; }
00075 
00080         virtual const CHAR* get_name() { return "Linear"; }
00081 
00090         virtual bool init_optimization(INT num_suppvec, INT* sv_idx, DREAL* alphas);
00091 
00096         virtual bool delete_optimization();
00097 
00103         virtual DREAL compute_optimized(INT idx);
00104 
00106         virtual void clear_normal();
00107 
00113         virtual void add_to_normal(INT idx, DREAL weight);
00114 
00120         inline const double* get_normal(INT& len)
00121         {
00122             if (lhs && normal)
00123             {
00124                 len = ((CRealFeatures*) lhs)->get_num_features();
00125                 return normal;
00126             }
00127             else
00128             {
00129                 len = 0;
00130                 return NULL;
00131             }
00132         }
00133 
00139         inline void get_w(DREAL** dst_w, INT* dst_dims)
00140         {
00141             ASSERT(lhs && normal);
00142             INT len = ((CRealFeatures*) lhs)->get_num_features();
00143             ASSERT(dst_w && dst_dims);
00144             *dst_dims=len;
00145             *dst_w=(DREAL*) malloc(sizeof(DREAL)*(*dst_dims));
00146             ASSERT(*dst_w);
00147             memcpy(*dst_w, normal, sizeof(DREAL) * (*dst_dims));
00148         }
00149 
00155         inline void set_w(DREAL* src_w, INT src_w_dim)
00156         {
00157             ASSERT(lhs && src_w_dim==((CRealFeatures*) lhs)->get_num_features());
00158             clear_normal();
00159             memcpy(normal, src_w, sizeof(DREAL) * src_w_dim);
00160         }
00161 
00162     protected:
00171         virtual DREAL compute(INT idx_a, INT idx_b);
00172 
00174         virtual void init_rescale();
00175 
00176     protected:
00178         double scale;
00180         bool initialized;
00182         DREAL* normal;
00184         INT normal_length;
00185 };
00186 
00187 #endif /* _LINEARKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation