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 using namespace shogun;
00019 
00020 CPolyKernel::CPolyKernel(int32_t size, int32_t d, bool i)
00021 : CSimpleKernel<float64_t>(size), degree(d), inhomogene(i)
00022 {
00023     set_normalizer(new CSqrtDiagKernelNormalizer());
00024 }
00025 
00026 CPolyKernel::CPolyKernel(
00027     CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, int32_t d, bool i, int32_t size)
00028 : CSimpleKernel<float64_t>(size), degree(d), inhomogene(i)
00029 {
00030     set_normalizer(new CSqrtDiagKernelNormalizer());
00031     init(l,r);
00032 }
00033 
00034 CPolyKernel::~CPolyKernel()
00035 {
00036     cleanup();
00037 }
00038 
00039 bool CPolyKernel::init(CFeatures* l, CFeatures* r)
00040 {
00041     CSimpleKernel<float64_t>::init(l,r);
00042     return init_normalizer();
00043 }
00044 
00045 void CPolyKernel::cleanup()
00046 {
00047     CKernel::cleanup();
00048 }
00049 
00050 float64_t CPolyKernel::compute(int32_t idx_a, int32_t idx_b)
00051 {
00052   int32_t alen=0;
00053   int32_t blen=0;
00054   bool afree=false;
00055   bool bfree=false;
00056 
00057   float64_t* avec=
00058     ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00059   float64_t* bvec=
00060     ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00061   ASSERT(alen==blen);
00062 
00063   float64_t result=CMath::dot(avec, bvec, alen);
00064 
00065   if (inhomogene)
00066       result+=1;
00067 
00068   result=CMath::pow(result, degree);
00069 
00070   ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00071   ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00072 
00073   return result;
00074 }

SHOGUN Machine Learning Toolbox - Documentation