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-2010 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  * Copyright (C) 2010 Berlin Institute of Technology
00010  */
00011 
00012 #ifndef _LINEARKERNEL_H___
00013 #define _LINEARKERNEL_H___
00014 
00015 #include <shogun/lib/common.h>
00016 #include <shogun/kernel/DotKernel.h>
00017 #include <shogun/features/DotFeatures.h>
00018 #include <shogun/machine/KernelMachine.h>
00019 
00020 namespace shogun
00021 {
00022     class CKernelMachine;
00023     class CDotFeatures;
00024 
00033 class CLinearKernel: public CDotKernel
00034 {
00035     public:
00038         CLinearKernel();
00039 
00045         CLinearKernel(CDotFeatures* l, CDotFeatures* r);
00046 
00047         virtual ~CLinearKernel();
00048 
00055         virtual bool init(CFeatures* l, CFeatures* r);
00056 
00058         virtual void cleanup();
00059 
00064         virtual EKernelType get_kernel_type() { return K_LINEAR; }
00065 
00070         virtual const char* get_name() const { return "LinearKernel"; }
00071 
00080         virtual bool init_optimization(
00081             int32_t num_suppvec, int32_t* sv_idx, float64_t* alphas);
00082 
00086         virtual bool init_optimization(CKernelMachine* km);
00087 
00092         virtual bool delete_optimization();
00093 
00099         virtual float64_t compute_optimized(int32_t idx);
00100 
00102         virtual void clear_normal();
00103 
00109         virtual void add_to_normal(int32_t idx, float64_t weight);
00110 
00116         inline const float64_t* get_normal(int32_t& len)
00117         {
00118             if (lhs && normal)
00119             {
00120                 len = ((CDotFeatures*) lhs)->get_dim_feature_space();
00121                 return normal;
00122             }
00123             else
00124             {
00125                 len = 0;
00126                 return NULL;
00127             }
00128         }
00129 
00135         inline void get_w(float64_t** dst_w, int32_t* dst_dims)
00136         {
00137             ASSERT(lhs && normal);
00138             int32_t len = ((CDotFeatures*) lhs)->get_dim_feature_space();
00139             ASSERT(dst_w && dst_dims);
00140             *dst_dims=len;
00141             *dst_w=SG_MALLOC(float64_t, *dst_dims);
00142             ASSERT(*dst_w);
00143             memcpy(*dst_w, normal, sizeof(float64_t) * (*dst_dims));
00144         }
00145 
00151         inline void set_w(float64_t* src_w, int32_t src_w_dim)
00152         {
00153             ASSERT(lhs && src_w_dim==((CDotFeatures*) lhs)->get_dim_feature_space());
00154             clear_normal();
00155             memcpy(normal, src_w, sizeof(float64_t) * src_w_dim);
00156         }
00157 
00158     protected:
00160         float64_t* normal;
00162         int32_t normal_length;
00163 };
00164 }
00165 #endif /* _LINEARKERNEL_H__ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation