SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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>
15 #include <shogun/io/SGIO.h>
16 
17 using namespace shogun;
18 
20 : CDotKernel(0), width(0.0)
21 {
23 }
24 
26 : CDotKernel(size), width(w)
27 {
29 }
30 
33 : CDotKernel(size), width(w)
34 {
35  init(l,r);
37 }
38 
40 {
41 }
42 
43 bool CGaussianShortRealKernel::init(CFeatures* l, CFeatures* r)
44 {
45  CDotKernel::init(l, r);
46  return init_normalizer();
47 }
48 
49 float64_t CGaussianShortRealKernel::compute(int32_t idx_a, int32_t idx_b)
50 {
51  int32_t alen, blen;
52  bool afree, bfree;
53 
54  float32_t* avec=((CSimpleFeatures<float32_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
55  float32_t* bvec=((CSimpleFeatures<float32_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
56  ASSERT(alen==blen);
57 
58  float64_t result=0;
59  for (int32_t i=0; i<alen; i++)
60  result+=CMath::sq(avec[i]-bvec[i]);
61 
62  result=exp(-result/width);
63 
64  ((CSimpleFeatures<float32_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
65  ((CSimpleFeatures<float32_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
66 
67  return result;
68 }
69 
71 {
72  m_parameters->add(&width, "width", "kernel width");
73 }

SHOGUN Machine Learning Toolbox - Documentation