|
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) 2008-2009 Soeren Sonnenburg 00008 * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #include "lib/common.h" 00012 #include "kernel/GaussianShortRealKernel.h" 00013 #include "features/Features.h" 00014 #include "features/SimpleFeatures.h" 00015 #include "lib/io.h" 00016 00017 using namespace shogun; 00018 00019 CGaussianShortRealKernel::CGaussianShortRealKernel(int32_t size, float64_t w) 00020 : CSimpleKernel<float32_t>(size), width(w) 00021 { 00022 } 00023 00024 CGaussianShortRealKernel::CGaussianShortRealKernel( 00025 CSimpleFeatures<float32_t>* l, CSimpleFeatures<float32_t>* r, float64_t w, int32_t size) 00026 : CSimpleKernel<float32_t>(size), width(w) 00027 { 00028 init(l,r); 00029 } 00030 00031 CGaussianShortRealKernel::~CGaussianShortRealKernel() 00032 { 00033 } 00034 00035 bool CGaussianShortRealKernel::init(CFeatures* l, CFeatures* r) 00036 { 00037 CSimpleKernel<float32_t>::init(l, r); 00038 return init_normalizer(); 00039 } 00040 00041 float64_t CGaussianShortRealKernel::compute(int32_t idx_a, int32_t idx_b) 00042 { 00043 int32_t alen, blen; 00044 bool afree, bfree; 00045 00046 float32_t* avec=((CSimpleFeatures<float32_t>*) lhs)->get_feature_vector(idx_a, alen, afree); 00047 float32_t* bvec=((CSimpleFeatures<float32_t>*) rhs)->get_feature_vector(idx_b, blen, bfree); 00048 ASSERT(alen==blen); 00049 00050 float64_t result=0; 00051 for (int32_t i=0; i<alen; i++) 00052 result+=CMath::sq(avec[i]-bvec[i]); 00053 00054 result=exp(-result/width); 00055 00056 ((CSimpleFeatures<float32_t>*) lhs)->free_feature_vector(avec, idx_a, afree); 00057 ((CSimpleFeatures<float32_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree); 00058 00059 return result; 00060 }