SparseGaussianKernel.cpp
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 CSparseGaussianKernel::CSparseGaussianKernel(INT size, double w)
00018 : CSparseKernel<DREAL>(size), width(w), sq_lhs(NULL), sq_rhs(NULL)
00019 {
00020 }
00021
00022 CSparseGaussianKernel::CSparseGaussianKernel(
00023 CSparseFeatures<DREAL>* l, CSparseFeatures<DREAL>* r, double w)
00024 : CSparseKernel<DREAL>(10), width(w), sq_lhs(NULL), sq_rhs(NULL)
00025 {
00026 init(l, r);
00027 }
00028
00029 CSparseGaussianKernel::~CSparseGaussianKernel()
00030 {
00031 cleanup();
00032 }
00033
00034 bool CSparseGaussianKernel::init(CFeatures* l, CFeatures* r)
00035 {
00037 cleanup();
00038
00039 CSparseKernel<DREAL>::init(l, r);
00040
00041 sq_lhs=new DREAL[lhs->get_num_vectors()];
00042 sq_lhs=((CSparseFeatures<DREAL>*) lhs)->compute_squared(sq_lhs);
00043 if (lhs==rhs)
00044 sq_rhs=sq_lhs;
00045 else
00046 {
00047 sq_rhs=new DREAL[rhs->get_num_vectors()];
00048 sq_rhs=((CSparseFeatures<DREAL>*) rhs)->compute_squared(sq_rhs);
00049 }
00050
00051 return true;
00052 }
00053
00054 void CSparseGaussianKernel::cleanup()
00055 {
00056 if (sq_lhs != sq_rhs)
00057 delete[] sq_rhs;
00058 sq_rhs = NULL;
00059
00060 delete[] sq_lhs;
00061 sq_lhs = NULL;
00062
00063 CKernel::cleanup();
00064 }
00065
00066 bool CSparseGaussianKernel::load_init(FILE* src)
00067 {
00068 return false;
00069 }
00070
00071 bool CSparseGaussianKernel::save_init(FILE* dest)
00072 {
00073 return false;
00074 }
00075
00076 DREAL CSparseGaussianKernel::compute(INT idx_a, INT idx_b)
00077 {
00078
00079 DREAL result=((CSparseFeatures<DREAL>*) lhs)->compute_squared_norm((CSparseFeatures<DREAL>*) lhs, sq_lhs, idx_a, (CSparseFeatures<DREAL>*) rhs, sq_rhs, idx_b);
00080 return exp(-result/width);
00081 }