Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
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 }