SHOGUN  6.1.3
statistical_testing/internals/Kernel.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2016 - 2017 Soumyajit De
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
31 #include <shogun/lib/common.h>
32 #include <shogun/kernel/Kernel.h>
33 
34 #ifndef KERNEL_FUNCTOR_H__
35 #define KERNEL_FUNCTOR_H__
36 
37 namespace shogun
38 {
39 
40 class CKernel;
41 
42 namespace internal
43 {
44 #ifndef DOXYGEN_SHOULD_SKIP_THIS
45 class Kernel
46 {
47 public:
48  explicit Kernel(CKernel* kernel) : m_kernel(kernel)
49  {
50  }
51 
52  inline float32_t operator()(int32_t i, int32_t j) const
53  {
54  return m_kernel->kernel(i, j);
55  }
56 private:
57  CKernel* m_kernel;
58 };
59 
60 class SelfAdjointPrecomputedKernel
61 {
62 public:
63  SelfAdjointPrecomputedKernel() : m_num_feat_vec(0)
64  {
65  }
66  explicit SelfAdjointPrecomputedKernel(SGVector<float32_t> self_adjoint_kernel_matrix) : m_num_feat_vec(0)
67  {
68  REQUIRE(self_adjoint_kernel_matrix.size()>0, "Provided kernel matrix cannot be of size 0!\n");
69  m_self_adjoint_kernel_matrix=self_adjoint_kernel_matrix;
70  }
71  void precompute(CKernel* kernel)
72  {
73  REQUIRE(kernel, "Kernel instance cannot be NULL!\n");
74  REQUIRE(kernel->get_num_vec_lhs()==kernel->get_num_vec_rhs(),
75  "Kernel instance is not symmetric (%dx%d)!\n", kernel->get_num_vec_lhs(), kernel->get_num_vec_rhs());
76  m_num_feat_vec=kernel->get_num_vec_lhs();
77  auto size=m_num_feat_vec*(m_num_feat_vec+1)/2;
78  if (m_self_adjoint_kernel_matrix.size()==0 || m_self_adjoint_kernel_matrix.size()!=size)
79  m_self_adjoint_kernel_matrix=SGVector<float32_t>(size);
80  for (auto i=0; i<m_num_feat_vec; ++i)
81  {
82  for (auto j=i; j<m_num_feat_vec; ++j)
83  {
84  auto index=i*m_num_feat_vec-i*(i+1)/2+j;
85  m_self_adjoint_kernel_matrix[index]=kernel->kernel(i, j);
86  }
87  }
88  }
89  inline float32_t operator()(int32_t i, int32_t j) const
90  {
91  ASSERT(m_num_feat_vec);
92  ASSERT(i>=0 && i<m_num_feat_vec);
93  ASSERT(j>=0 && j<m_num_feat_vec);
94  if (i>j)
95  std::swap(i, j);
96  auto index=i*m_num_feat_vec-i*(i+1)/2+j;
97  return m_self_adjoint_kernel_matrix[index];
98  }
99 private:
100  SGVector<float32_t> m_self_adjoint_kernel_matrix;
101  index_t m_num_feat_vec;
102 };
103 #endif // DOXYGEN_SHOULD_SKIP_THIS
104 }
105 
106 }
107 #endif // KERNEL_FUNCTOR_H__
int32_t index_t
Definition: common.h:72
#define REQUIRE(x,...)
Definition: SGIO.h:181
#define ASSERT(x)
Definition: SGIO.h:176
float float32_t
Definition: common.h:59
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18

SHOGUN Machine Learning Toolbox - Documentation