SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GaussianShortRealKernel.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2008-2009 Soeren Sonnenburg
8  * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/common.h>
14 #include <shogun/io/SGIO.h>
15 
16 using namespace shogun;
17 
19 : CDotKernel(0), width(0.0)
20 {
22 }
23 
25 : CDotKernel(size), width(w)
26 {
28 }
29 
32 : CDotKernel(size), width(w)
33 {
34  init(l,r);
36 }
37 
39 {
40 }
41 
42 bool CGaussianShortRealKernel::init(CFeatures* l, CFeatures* r)
43 {
44  CDotKernel::init(l, r);
45  return init_normalizer();
46 }
47 
48 float64_t CGaussianShortRealKernel::compute(int32_t idx_a, int32_t idx_b)
49 {
50  int32_t alen, blen;
51  bool afree, bfree;
52 
53  float32_t* avec=((CDenseFeatures<float32_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
54  float32_t* bvec=((CDenseFeatures<float32_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
55  ASSERT(alen==blen)
56 
57  float64_t result=0;
58  for (int32_t i=0; i<alen; i++)
59  result+=CMath::sq(avec[i]-bvec[i]);
60 
61  result=exp(-result/width);
62 
63  ((CDenseFeatures<float32_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
64  ((CDenseFeatures<float32_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
65 
66  return result;
67 }
68 
70 {
71  SG_ADD(&width, "width", "kernel width", MS_AVAILABLE);
72 }

SHOGUN Machine Learning Toolbox - Documentation