SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 }
The class DenseFeatures implements dense feature matrices.
Definition: LDA.h:41
static T sq(T x)
Definition: Math.h:450
Template class DotKernel is the base class for kernels working on DotFeatures.
Definition: DotKernel.h:31
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
virtual bool init(CFeatures *l, CFeatures *r)
virtual bool init_normalizer()
Definition: Kernel.cpp:168
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
float float32_t
Definition: common.h:49
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1061
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1059
The class Features is the base class of all feature objects.
Definition: Features.h:68
#define SG_ADD(...)
Definition: SGObject.h:81

SHOGUN Machine Learning Toolbox - Documentation