SparsePolyKernel.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/common.h"
00012 #include "lib/io.h"
00013 #include "kernel/SparsePolyKernel.h"
00014 #include "kernel/SqrtDiagKernelNormalizer.h"
00015 #include "features/SparseFeatures.h"
00016 
00017 using namespace shogun;
00018 
00019 CSparsePolyKernel::CSparsePolyKernel(int32_t size, int32_t d, bool i)
00020 : CSparseKernel<float64_t>(size), degree(d), inhomogene(i)
00021 {
00022     set_normalizer(new CSqrtDiagKernelNormalizer());
00023 }
00024 
00025 CSparsePolyKernel::CSparsePolyKernel(
00026     CSparseFeatures<float64_t>* l, CSparseFeatures<float64_t>* r,
00027     int32_t size, int32_t d, bool i)
00028 : CSparseKernel<float64_t>(size),degree(d),inhomogene(i)
00029 {
00030     set_normalizer(new CSqrtDiagKernelNormalizer());
00031     init(l,r);
00032 }
00033 
00034 CSparsePolyKernel::~CSparsePolyKernel()
00035 {
00036     cleanup();
00037 }
00038 
00039 bool CSparsePolyKernel::init(CFeatures* l, CFeatures* r)
00040 {
00041     CSparseKernel<float64_t>::init(l,r);
00042     return init_normalizer();
00043 }
00044   
00045 void CSparsePolyKernel::cleanup()
00046 {
00047     CKernel::cleanup();
00048 }
00049 
00050 float64_t CSparsePolyKernel::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   TSparseEntry<float64_t>* avec=((CSparseFeatures<float64_t>*) lhs)->
00058     get_sparse_feature_vector(idx_a, alen, afree);
00059   TSparseEntry<float64_t>* bvec=((CSparseFeatures<float64_t>*) rhs)->
00060     get_sparse_feature_vector(idx_b, blen, bfree);
00061 
00062   float64_t result=((CSparseFeatures<float64_t>*) lhs)->sparse_dot(1.0,avec, alen, bvec, blen);
00063 
00064   if (inhomogene)
00065       result+=1;
00066 
00067   result=CMath::pow(result, degree);
00068 
00069   ((CSparseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00070   ((CSparseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00071 
00072   return result;
00073 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation