Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "lib/common.h"
00012 #include "lib/io.h"
00013 #include "kernel/SparseGaussianKernel.h"
00014 #include "features/Features.h"
00015 #include "features/SparseFeatures.h"
00016
00017 using namespace shogun;
00018
00019 CSparseGaussianKernel::CSparseGaussianKernel(int32_t size, float64_t w)
00020 : CSparseKernel<float64_t>(size), width(w), sq_lhs(NULL), sq_rhs(NULL)
00021 {
00022 }
00023
00024 CSparseGaussianKernel::CSparseGaussianKernel(
00025 CSparseFeatures<float64_t>* l, CSparseFeatures<float64_t>* r, float64_t w)
00026 : CSparseKernel<float64_t>(10), width(w), sq_lhs(NULL), sq_rhs(NULL)
00027 {
00028 init(l, r);
00029 }
00030
00031 CSparseGaussianKernel::~CSparseGaussianKernel()
00032 {
00033 cleanup();
00034 }
00035
00036 bool CSparseGaussianKernel::init(CFeatures* l, CFeatures* r)
00037 {
00039 cleanup();
00040
00041 CSparseKernel<float64_t>::init(l, r);
00042
00043 sq_lhs=new float64_t[lhs->get_num_vectors()];
00044 sq_lhs=((CSparseFeatures<float64_t>*) lhs)->compute_squared(sq_lhs);
00045 if (lhs==rhs)
00046 sq_rhs=sq_lhs;
00047 else
00048 {
00049 sq_rhs=new float64_t[rhs->get_num_vectors()];
00050 sq_rhs=((CSparseFeatures<float64_t>*) rhs)->compute_squared(sq_rhs);
00051 }
00052
00053 return init_normalizer();
00054 }
00055
00056 void CSparseGaussianKernel::cleanup()
00057 {
00058 if (sq_lhs != sq_rhs)
00059 delete[] sq_rhs;
00060 sq_rhs = NULL;
00061
00062 delete[] sq_lhs;
00063 sq_lhs = NULL;
00064
00065 CKernel::cleanup();
00066 }
00067
00068 float64_t CSparseGaussianKernel::compute(int32_t idx_a, int32_t idx_b)
00069 {
00070
00071 float64_t result=((CSparseFeatures<float64_t>*) lhs)->compute_squared_norm(
00072 (CSparseFeatures<float64_t>*) lhs, sq_lhs, idx_a,
00073 (CSparseFeatures<float64_t>*) rhs, sq_rhs, idx_b);
00074 return exp(-result/width);
00075 }