SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TensorProductPairKernel.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) 2009 Soeren Sonnenburg
8  * Copyright (C) 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), subkernel(NULL)
21 {
23 }
24 
26 : CDotKernel(size), subkernel(s)
27 {
30 }
31 
33 : CDotKernel(10), subkernel(s)
34 {
36  init(l, r);
38 }
39 
41 {
43  cleanup();
44 }
45 
46 bool CTensorProductPairKernel::init(CFeatures* l, CFeatures* r)
47 {
48  CDotKernel::init(l, r);
50  return true;
51 }
52 
53 float64_t CTensorProductPairKernel::compute(int32_t idx_a, int32_t idx_b)
54 {
55  int32_t alen, blen;
56  bool afree, bfree;
57 
58  int32_t* avec=((CSimpleFeatures<int32_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
59  int32_t* bvec=((CSimpleFeatures<int32_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
60 
61  ASSERT(alen==2);
62  ASSERT(blen==2);
63 
64  CKernel* k=subkernel;
65  ASSERT(k && k->has_features());
66 
67  int32_t a=avec[0];
68  int32_t b=avec[1];
69  int32_t c=bvec[0];
70  int32_t d=bvec[1];
71 
72  float64_t result = k->kernel(a,c)*k->kernel(b,d) + k->kernel(a,d)*k->kernel(b,c);
73 
74  ((CSimpleFeatures<int32_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
75  ((CSimpleFeatures<int32_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
76 
77  return result;
78 }
79 
81 {
82  m_parameters->add((CSGObject**)&subkernel, "subkernel", "the subkernel");
83 }

SHOGUN Machine Learning Toolbox - Documentation