|
SHOGUN v0.9.3
|
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/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 //float64_t result = sq_lhs[idx_a] + sq_rhs[idx_b]; 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 }