PolyKernel.cpp

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 #include "lib/config.h"
00012 #include "lib/common.h"
00013 #include "lib/io.h"
00014 #include "kernel/PolyKernel.h"
00015 #include "kernel/SqrtDiagKernelNormalizer.h"
00016 #include "features/SimpleFeatures.h"
00017 
00018 CPolyKernel::CPolyKernel(int32_t size, int32_t d, bool i)
00019 : CSimpleKernel<float64_t>(size), degree(d), inhomogene(i)
00020 {
00021     set_normalizer(new CSqrtDiagKernelNormalizer());
00022 }
00023 
00024 CPolyKernel::CPolyKernel(
00025     CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, int32_t d, bool i, int32_t size)
00026 : CSimpleKernel<float64_t>(size), degree(d), inhomogene(i)
00027 {
00028     set_normalizer(new CSqrtDiagKernelNormalizer());
00029     init(l,r);
00030 }
00031 
00032 CPolyKernel::~CPolyKernel()
00033 {
00034     cleanup();
00035 }
00036 
00037 bool CPolyKernel::init(CFeatures* l, CFeatures* r)
00038 {
00039     CSimpleKernel<float64_t>::init(l,r);
00040     return init_normalizer();
00041 }
00042 
00043 void CPolyKernel::cleanup()
00044 {
00045     CKernel::cleanup();
00046 }
00047 
00048 bool CPolyKernel::load_init(FILE* src)
00049 {
00050     return false;
00051 }
00052 
00053 bool CPolyKernel::save_init(FILE* dest)
00054 {
00055     return false;
00056 }
00057 
00058 float64_t CPolyKernel::compute(int32_t idx_a, int32_t idx_b)
00059 {
00060   int32_t alen=0;
00061   int32_t blen=0;
00062   bool afree=false;
00063   bool bfree=false;
00064 
00065   float64_t* avec=
00066     ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00067   float64_t* bvec=
00068     ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00069   ASSERT(alen==blen);
00070 
00071   float64_t result=CMath::dot(avec, bvec, alen);
00072 
00073   if (inhomogene)
00074       result+=1;
00075 
00076   result=CMath::pow(result, degree);
00077 
00078   ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00079   ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00080 
00081   return result;
00082 }

SHOGUN Machine Learning Toolbox - Documentation