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 }
static T sq(T x)
x^2
Definition: Math.h:277
Parameter * m_parameters
Definition: SGObject.h:297
Template class DotKernel is the base class for kernels working on DotFeatures.
Definition: DotKernel.h:29
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:23
#define ASSERT(x)
Definition: SGIO.h:102
double float64_t
Definition: common.h:56
virtual bool init(CFeatures *l, CFeatures *r)
virtual bool init_normalizer()
Definition: Kernel.cpp:133
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
float float32_t
Definition: common.h:55
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:810
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:808
The class Features is the base class of all feature objects.
Definition: Features.h:56
The class SimpleFeatures implements dense feature matrices.
Definition: LDA.h:24

SHOGUN Machine Learning Toolbox - Documentation