DiscreteKernel.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Kernel on a finite, discrete space.
6  *
7  *
8  *
9  * \author T. Glasmachers
10  * \date 2012
11  *
12  *
13  * \par Copyright 1995-2017 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://shark-ml.org/>
18  *
19  * Shark is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU Lesser General Public License as published
21  * by the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * Shark is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 //===========================================================================
34 
35 #ifndef SHARK_MODELS_KERNELS_DISCRETEKERNEL_H
36 #define SHARK_MODELS_KERNELS_DISCRETEKERNEL_H
37 
38 
40 #include <shark/LinAlg/Base.h>
41 #include <vector>
42 
43 namespace shark {
44 
45 
46 ///
47 /// \brief Kernel on a finite, discrete space.
48 ///
49 /// \par
50 /// This class represents a kernel function on a finite
51 /// space with N elements. Wlog, this space is represented
52 /// by the integers 0, ..., N-1. The kernel function is
53 /// defined by a symmetric, positive semi-definite N x N
54 /// matrix.
55 ///
56 class DiscreteKernel : public AbstractKernelFunction<std::size_t>
57 {
58 public:
60 
61  /// \brief Construction of the kernel from a positive definite, symmetric matrix.
62  DiscreteKernel(RealMatrix const& matrix)
63  : m_matrix(matrix)
64  {
65  SHARK_RUNTIME_CHECK(matrix.size1() == matrix.size2(), "[DiscreteKernel::DiscreteKernel] kernel matrix must be square");
66 #ifdef DEBUG
67  for (std::size_t i=0; i<matrix.size1(); i++)
68  {
69  for (std::size_t j=0; j<i; j++)
70  {
71  SHARK_RUNTIME_CHECK(matrix(i, j) == matrix(j, i), "[DiscreteKernel::DiscreteKernel] kernel matrix must be symmetric");
72  }
73  }
74 #endif
75  }
76 
77  /// \brief From INameable: return the class name.
78  std::string name() const
79  { return "DiscreteKernel"; }
80 
81  RealVector parameterVector() const
82  {
83  return RealVector();
84  }
85 
86  void setParameterVector(RealVector const& newParameters)
87  {
88  SIZE_CHECK(newParameters.size() == 0);
89  }
90 
91  std::size_t numberOfParameters() const
92  {
93  return 0;
94  }
95 
96  /// \brief Cardinality of the discrete space.
97  std::size_t size() const
98  { return m_matrix.size1(); }
99 
100  ///\brief DiscreteKernels don't have a state so they return an EmptyState object
101  boost::shared_ptr<State> createState()const{
102  return boost::shared_ptr<State>(new EmptyState());
103  }
104 
105  /// \brief Evaluates the kernel function.
106  ///
107  /// The function returns the stored similarity value.
109  {
110  return m_matrix(x1, x2);
111  }
112 
113  /// \brief Evaluates the kernel function for every point combination of the two batches
114  ///
115  /// The function returns the stored similarity value.
116  void eval(ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix& result, State& state) const{
117  eval(batchX1,batchX2,result);
118  }
119  /// \brief Evaluates the kernel function for every point combination of the two batches
120  ///
121  /// The function returns the stored similarity value.
122  void eval(ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix& result) const{
123  std::size_t sizeX1 = batchSize(batchX1);
124  std::size_t sizeX2 = batchSize(batchX2);
125  ensure_size(result,sizeX1,sizeX2);
126  for(std::size_t i = 0; i != sizeX1; ++i)
127  for(std::size_t j = 0; j != sizeX2; ++j)
128  result(i,j)=m_matrix(i,j);
129  }
130 
131 
132  /// From ISerializable.
133  void read(InArchive& ar)
134  { ar >> m_matrix; }
135 
136  /// From ISerializable.
137  void write(OutArchive& ar) const
138  { ar << m_matrix; }
139 
140 protected:
141  /// kernel matrix
142  RealMatrix m_matrix;
143 };
144 
145 
146 }
147 #endif