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-2009 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 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/SimpleFeatures.h"
00017 
00018 namespace shogun
00019 {
00028 class CLinearKernel: public CSimpleKernel<float64_t>
00029 {
00030     public:
00033         CLinearKernel();
00034 
00040         CLinearKernel(CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r);
00041 
00042         virtual ~CLinearKernel();
00043 
00050         virtual bool init(CFeatures* l, CFeatures* r);
00051 
00053         virtual void cleanup();
00054 
00059         virtual EKernelType get_kernel_type() { return K_LINEAR; }
00060 
00065         virtual const char* get_name() const { return "Linear"; }
00066 
00075         virtual bool init_optimization(
00076             int32_t num_suppvec, int32_t* sv_idx, float64_t* alphas);
00077 
00082         virtual bool delete_optimization();
00083 
00089         virtual float64_t compute_optimized(int32_t idx);
00090 
00092         virtual void clear_normal();
00093 
00099         virtual void add_to_normal(int32_t idx, float64_t weight);
00100 
00106         inline const float64_t* get_normal(int32_t& len)
00107         {
00108             if (lhs && normal)
00109             {
00110                 len = ((CSimpleFeatures<float64_t>*) lhs)->get_num_features();
00111                 return normal;
00112             }
00113             else
00114             {
00115                 len = 0;
00116                 return NULL;
00117             }
00118         }
00119 
00125         inline void get_w(float64_t** dst_w, int32_t* dst_dims)
00126         {
00127             ASSERT(lhs && normal);
00128             int32_t len = ((CSimpleFeatures<float64_t>*) lhs)->get_num_features();
00129             ASSERT(dst_w && dst_dims);
00130             *dst_dims=len;
00131             *dst_w=(float64_t*) malloc(sizeof(float64_t)*(*dst_dims));
00132             ASSERT(*dst_w);
00133             memcpy(*dst_w, normal, sizeof(float64_t) * (*dst_dims));
00134         }
00135 
00141         inline void set_w(float64_t* src_w, int32_t src_w_dim)
00142         {
00143             ASSERT(lhs && src_w_dim==((CSimpleFeatures<float64_t>*) lhs)->get_num_features());
00144             clear_normal();
00145             memcpy(normal, src_w, sizeof(float64_t) * src_w_dim);
00146         }
00147 
00148     protected:
00157         virtual float64_t compute(int32_t idx_a, int32_t idx_b);
00158 
00159     protected:
00161         float64_t* normal;
00163         int32_t normal_length;
00164 };
00165 }
00166 #endif /* _LINEARKERNEL_H__ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation