GaussianKernel.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 "kernel/GaussianKernel.h"
00013 #include "features/Features.h"
00014 #include "features/SimpleFeatures.h"
00015 #include "lib/io.h"
00016 
00017 #ifdef HAVE_BOOST_SERIALIZATION
00018 #include <boost/serialization/export.hpp>
00019 BOOST_CLASS_EXPORT(shogun::CGaussianKernel);
00020 #endif //HAVE_BOOST_SERIALIZATION
00021 
00022 using namespace shogun;
00023 
00024 CGaussianKernel::CGaussianKernel()
00025 : CSimpleKernel<float64_t>(), width(1)
00026 {
00027 }
00028 
00029 
00030 CGaussianKernel::CGaussianKernel(int32_t size, float64_t w)
00031 : CSimpleKernel<float64_t>(size), width(w)
00032 {
00033 }
00034 
00035 CGaussianKernel::CGaussianKernel(
00036     CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, float64_t w, int32_t size)
00037 : CSimpleKernel<float64_t>(size), width(w)
00038 {
00039     init(l,r);
00040 }
00041 
00042 CGaussianKernel::~CGaussianKernel()
00043 {
00044 }
00045 
00046 bool CGaussianKernel::init(CFeatures* l, CFeatures* r)
00047 {
00048     CSimpleKernel<float64_t>::init(l, r);
00049     return init_normalizer();
00050 }
00051 
00052 float64_t CGaussianKernel::compute(int32_t idx_a, int32_t idx_b)
00053 {
00054     int32_t alen, blen;
00055     bool afree, bfree;
00056 
00057     float64_t* avec=((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00058     float64_t* bvec=((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00059     ASSERT(alen==blen);
00060 
00061     float64_t result=0;
00062     for (int32_t i=0; i<alen; i++)
00063         result+=CMath::sq(avec[i]-bvec[i]);
00064 
00065     result=exp(-result/width);
00066 
00067     ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00068     ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00069 
00070     return result;
00071 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation