Machine Learning Library
CKernel.h
Go to the documentation of this file.
1 /*
2  COPYRIGHT (C) 2003 APPLIED NEUROINFORMATIC GROUP - UNIVERSITY OF BIELEFELD.
3 
4  ALL RIGHTS RESERVED.
5 
6  REDISTRIBUTION AND USE IN SOURCE AND BINARY FORM, WITH OR WITHOUT
7  MODIFICATION, REQUIRE THE PERMISSION OF THE COPYRIGHT HOLDERS.
8 
9  COMMERCIAL USE WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT HOLDERS
10  IS FORBIDDEN
11 
12  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
13  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 
25 
26 #ifndef CKERNEL_H
27 #define CKERNEL_H
28 
29 #include <cmath>
30 #include <stdio.h>
31 #include <assert.h>
32 #include "CDenseVector.h"
33 #include "CVector.h"
34 #include <string>
35 #include "CObject.h"
36 #include "CArchiv.h"
37 
45 template<class Type>
46 class CKernel : public CObject<Type>
47 {
48 private:
49 
50  double m_dBandwidth;
51 
52 public:
53  CKernel(Type tBandwidth=-1.0);
54  virtual ~CKernel(){};
55  //CKernel<Type>& operator=(const CKernel<Type> rtKernel);
56 
57  inline Type bandwidth() const { return m_dBandwidth;};
58  void setBandwidth(Type tB) { m_dBandwidth=tB;};
59  virtual Type execute(const CVector<Type>& rtVector1, const CVector<Type>& rtVector2) const=0;
60  virtual Type execute(const CVector<Type>& rtVector1, const Type* ptVector2) const=0;
61  virtual Type execute(const Type* ptVector1, const Type* ptVector2, int iDim) const=0;
62  virtual Type execute(Type tValue) const=0;
63  virtual CKernel<Type>* clone()const=0 ;
64  virtual string className() const { return string("CKernel");};
65  virtual bool isA(const char* acClass) const { if(string(acClass) == this->className()) return true; else return CObject<Type>::isA(acClass);};
66  virtual string factoryObject()=0;
67  virtual bool serialize(fstream& stream, IO_MODE mode );
68  virtual bool serialize2(CArchiv& tA);
69 };
70 
71 
78 template<class Type>
79 class CGaussianKernel : public CKernel<Type>
80 {
81 
82 public:
83  CGaussianKernel(Type tBandwidth = 1.0);
87  inline Type execute( const CVector<Type>& rtVector1, const CVector<Type>& rtVector2) const;
88  inline Type execute( const CVector<Type>& rtVector1, const Type* ptVector2) const;
89  inline Type execute( const Type* ptVector1, const Type* ptVector2, int iDim) const;
90  inline Type execute(Type tValue) const;
91  virtual string className() const { return string("CGaussianKernel");};
92  virtual bool isA(const char* acClass) const { if(string(acClass) == this->className()) return true; else return CKernel<Type>::isA(acClass);};
93  string factoryObject() { return string("CGaussianKernelFactory.so");};
94  Type gradientBandwidth( const CVector<Type>& rtVector1, const CVector<Type>& rtVector2, Type tDiagConst = 0);
96 
97 
98 
99 
100 };
101 
102 
103 
104 
111 template<class Type>
112 class CPlummerKernel : public CKernel<Type>
113 {
114 protected:
115  double m_dEpsilon;
116 public:
117  CPlummerKernel(Type tBandwidth = 1);
118  ~CPlummerKernel();
121  inline Type execute( const CVector<Type>& rtVector1, const CVector<Type>& rtVector2) const;
122  inline Type execute( const CVector<Type>& rtVector1, const Type* ptVector2) const ;
123  inline Type execute( const Type* ptVector1, const Type* ptVector2, int iDim) const ;
124  inline Type execute(Type tValue) const;
125  virtual string className() const { return string("CPlummerKernel");};
126  virtual bool isA(const char* acClass) const { if(string(acClass) == this->className()) return true; else return CKernel<Type>::isA(acClass);};
127 
128 
129  virtual void setEpsilon(Type tE);
130 
131  string factoryObject() { return string("CPlummerFactory.so");};
132  CPlummerKernel<Type>* clone() const;
133  bool serialize(fstream& stream, IO_MODE mode );
134  virtual bool serialize2(CArchiv& tA);
135 };
136 
137 
144 template<class Type>
145 class CTriangularKernel : public CKernel<Type>
146 {
147 
148 public:
149  CTriangularKernel(Type tBandwidth = 1);
153  inline Type execute( const CVector<Type>& rtVector1, const CVector<Type>& rtVector2) const;
154  inline Type execute( const CVector<Type>& rtVector1, const Type* ptVector2) const;
155  inline Type execute( const Type* ptVector1, const Type* ptVector2, int iDim) const ;
156  inline Type execute(Type tValue) const;
157  virtual string className() const { return string("CTriangularKernel");};
158  virtual bool isA(const char* acClass) const { if(string(acClass) == this->className()) return true; else return CKernel<Type>::isA(acClass);};
159 
160  string factoryObject() { return string("CTriangularKernelFactory.so");};
162 
163 
164 };
165 
166 
167 template<class Type>
169 {
170 
171 protected:
173 public:
178  inline Type execute( const CVector<Type>& rtVector1, const CVector<Type>& rtVector2) const;
179  inline Type execute( const CVector<Type>& rtVector1, const Type* ptVector2) const;
180  inline Type execute( const Type* ptVector1, const Type* ptVector2, int iDim) const;
181  inline Type execute(Type tValue) const;
182  void setBandwidth(Type tBw);
183  virtual string className() const { return string("CMultiVariateGaussianKernel");};
184  virtual bool isA(const char* acClass) const { if(string(acClass) == this->className()) return true; else return CKernel<Type>::isA(acClass);};
185  void setBandwidth(CDenseVector<Type>& tBw);
186 
188 
189 
190  virtual string factoryObject() {return string("");};
191 
192 };
193 
194 
201 template<class Type>
202 class CPolynomialKernel : public CKernel<Type>
203 {
204 public:
205  double dR;
206  int iDegree;
207 
208 public:
209  CPolynomialKernel(Type tBandwidth = 1, int iDegree = 2, Type tR = 0);
213  void setDegree(int iDegree);
214  void setR(Type tR);
215  virtual string className() const { return string("CPolynomialKernel");};
216  virtual bool isA(const char* acClass) const { if(string(acClass) == this->className()) return true; else return CKernel<Type>::isA(acClass);};
218  inline Type execute( const CVector<Type>& rtVector1, const CVector<Type>& rtVector2) const;
219  inline Type execute( const CVector<Type>& rtVector1, const Type* ptVector2) const;
220  inline Type execute( const Type* ptVector1, const Type* ptVector2, int iDim) const;
221  inline Type execute(Type tValue) const;
222  string factoryObject() { return string("CPolynomialKernelFactory.so");};
223  bool serialize(fstream& stream, IO_MODE mode );
224  virtual bool serialize2(CArchiv& tA);
225 };
226 
227 
228 
229 
236 template<class Type>
237 class CLinearKernel : public CKernel<Type>
238 {
239 public:
240  CLinearKernel();
241  CLinearKernel<Type>(const CLinearKernel<Type>& rtKernel);
243  ~CLinearKernel();
244  virtual string className() const { return string("CLinearKernel");};
245  virtual bool isA(const char* acClass) const { if(string(acClass) == this->className()) return true; else return CKernel<Type>::isA(acClass);};
246  CLinearKernel<Type>* clone() const;
247  inline Type execute(const CVector<Type>& rtVector1, const CVector<Type>& rtVector2) const;
248  inline Type execute( const CVector<Type>& rtVector1, const Type* ptVector2) const;
249  inline Type execute( const Type* ptVector1, const Type* ptVector2, int iDim) const;
250  inline Type execute(Type tValue) const;
251  string factoryObject() { return string("CLinearKernelFactory.so");};
252 
253 };
254 
255 
256 
257 
258 
267 template<class Type>
268 class CDoubleExponentialKernel : public CKernel<Type>
269 {
270 
271 public:
272  CDoubleExponentialKernel(Type tBandwidth = 1) ;
277  inline Type execute( const CVector<Type>& rtVector1, const CVector<Type>& rtVector2) const;
278  inline Type execute( const CVector<Type>& rtVector1, const Type* ptVector2) const;
279  inline Type execute( const Type* ptVector1, const Type* ptVector2, int iDim) const;
280  inline Type execute(Type tValue) const;
281  string factoryObject() { return string("CDoubleExponentialKernelFactory.so");};
282  virtual string className() const { return string("CDoubleExponentialKernel");};
283  virtual bool isA(const char* acClass) const { if(string(acClass) == this->className()) return true; else return CKernel<Type>::isA(acClass);};
284 
285 };
286 
287 
296 template<class Type>
297 class CAnovaKernel : public CKernel<Type>
298 {
299 protected:
300  int iDegree;
301 
302 public:
303  CAnovaKernel(Type tBandwidth=1,int iDegree = 2);
304  ~CAnovaKernel();
306  CAnovaKernel<Type>(const CAnovaKernel<Type>& rtKernel);
307  CAnovaKernel<Type>* clone() const ;
308  inline void setDegree(int iDegree);
309  inline Type execute( const CVector<Type>& rtVector1, const CVector<Type>& rtVector2) const;
310  inline Type execute( const CVector<Type>& rtVector1, const Type* ptVector2) const;
311  inline Type execute( const Type* ptVector1, const Type* ptVector2, int iDim) const;
312  inline Type execute(Type tValue) const;
313  virtual string className() const { return string("CAnoveKernel");};
314  virtual bool isA(const char* acClass) const { if(string(acClass) == this->className()) return true; else return CKernel<Type>::isA(acClass);};
315  string factoryObject() { return string("CAnovaKernelFactory.so");};
316  bool serialize(fstream& stream, IO_MODE mode );
317  virtual bool serialize2(CArchiv& tA);
318 };
319 
320 
321 
328 template<class Type>
329 class CChiSquaredKernel : public CKernel<Type>
330 {
331 
332 public:
333  CChiSquaredKernel<Type>(Type tBandwidth = 1);
338  inline Type execute( const CVector<Type>& rtVector1, const CVector<Type>& rtVector2) const;
339  inline Type execute( const CVector<Type>& rtVector1, const Type* ptVector2) const;
340  inline Type execute( const Type* ptVector1, const Type* ptVector2, int iDim) const;
341  inline Type execute(Type tValue) const;
342 
343  virtual string className() const { return string("CChiSquaredKernelFactoryKernel");};
344  virtual bool isA(const char* acClass) const { if(string(acClass) == this->className()) return true; else return CKernel<Type>::isA(acClass);};
345  string factoryObject() { return string("CChiSquaredKernelFactory.so");};
346 
347 };
348 
354 template<class Type>
355 class CEpanechnikovKernel : public CKernel<Type>
356 {
357 
358 public:
359  CEpanechnikovKernel<Type>(Type tBandwidth = 1);
364  inline Type execute( const CVector<Type>& rtVector1, const CVector<Type>& rtVector2) const;
365  inline Type execute( const CVector<Type>& rtVector1, const Type* ptVector2) const;
366  inline Type execute( const Type* ptVector1, const Type* ptVector2, int iDim) const;
367  inline Type execute(Type tValue) const;
368  string factoryObject() { return string("CEpanechnikovKernelFactory.so");};
369 
370 };
371 
372 extern "C"{
377 }
378 #endif
Definition: CKernel.h:329
bool serialize(fstream &stream, IO_MODE mode)
Read/write from binary stream.
virtual string className() const
Returns the class name.
Definition: CKernel.h:183
CDenseVector< Type > tVecBandwidth
Definition: CKernel.h:172
Definition: CKernel.h:355
CGaussianKernel(Type tBandwidth=1.0)
CTriangularKernel< Type > * clone() const
virtual string className() const
Returns the class name.
Definition: CKernel.h:244
Type execute(const CVector< Type > &rtVector1, const CVector< Type > &rtVector2) const
string factoryObject()
Definition: CKernel.h:345
void setDegree(int iDegree)
void setR(Type tR)
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CKernel.h:126
CAnovaKernel< Type > & operator=(const CAnovaKernel< Type > &rtKernel)
IO_MODE
Definition: CObject.h:38
Definition: CKernel.h:168
double m_dEpsilon
Definition: CKernel.h:115
CTriangularKernel & operator=(const CTriangularKernel< Type > &rtKernel)
Type execute(const CVector< Type > &rtVector1, const CVector< Type > &rtVector2) const
string factoryObject()
Definition: CKernel.h:93
CKernel< double > * _create_double_kernel()
Definition: CKernel.h:373
string factoryObject()
Definition: CKernel.h:315
int iDegree
Definition: CKernel.h:206
CMultiVariateGaussianKernel(CDenseVector< Type > tTmp=CDenseVector< Type >())
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CKernel.h:65
virtual void setEpsilon(Type tE)
CPlummerKernel & operator=(const CPlummerKernel< Type > &rtKernel)
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CKernel.h:158
CChiSquaredKernel< Type > * clone() const
virtual string factoryObject()=0
Triangular kernel function.
Definition: CKernel.h:145
Type execute(const CVector< Type > &rtVector1, const CVector< Type > &rtVector2) const
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CKernel.h:245
CPolynomialKernel< Type > * clone() const
Type execute(const CVector< Type > &rtVector1, const CVector< Type > &rtVector2) const
Plummer kernel function.
Definition: CKernel.h:112
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CKernel.h:216
CEpanechnikovKernel< Type > * clone() const
virtual CKernel< Type > * clone() const =0
virtual bool serialize(fstream &stream, IO_MODE mode)
Read/write from binary stream.
bool serialize(fstream &stream, IO_MODE mode)
Read/write from binary stream.
CAnovaKernel< Type > * clone() const
void _destroy_double_kernel(CKernel< double > *)
Definition: CKernel.h:374
string factoryObject()
Definition: CKernel.h:160
CAnovaKernel(Type tBandwidth=1, int iDegree=2)
virtual string className() const
Returns the class name.
Definition: CKernel.h:282
void setDegree(int iDegree)
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CKernel.h:314
CLinearKernel< Type > * clone() const
Type gradientBandwidth(const CVector< Type > &rtVector1, const CVector< Type > &rtVector2, Type tDiagConst=0)
CKernel(Type tBandwidth=-1.0)
virtual string className() const
Returns the class name.
Definition: CKernel.h:343
CLinearKernel< Type > & operator=(const CLinearKernel< Type > &rtKernel)
CPlummerKernel< Type > * clone() const
CDoubleExponentialKernel< Type > * clone() const
string factoryObject()
Definition: CKernel.h:368
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CKernel.h:184
CPolynomialKernel(Type tBandwidth=1, int iDegree=2, Type tR=0)
CTriangularKernel(Type tBandwidth=1)
virtual ~CKernel()
Definition: CKernel.h:54
string factoryObject()
Definition: CKernel.h:222
virtual string className() const
Returns the class name.
Definition: CKernel.h:125
virtual Type execute(const CVector< Type > &rtVector1, const CVector< Type > &rtVector2) const =0
virtual bool serialize2(CArchiv &tA)
virtual bool serialize2(CArchiv &tA)
virtual string className() const
Returns the class name.
Definition: CKernel.h:91
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CKernel.h:283
Type bandwidth() const
Definition: CKernel.h:57
virtual string factoryObject()
Definition: CKernel.h:190
virtual bool serialize2(CArchiv &tA)
CGaussianKernel< Type > * clone() const
virtual string className() const
Returns the class name.
Definition: CKernel.h:157
CEpanechnikovKernel< Type > & operator=(const CEpanechnikovKernel< Type > &rtKernel)
Base class of object serialization.
Definition: CArchiv.h:19
Type execute(const CVector< Type > &rtVector1, const CVector< Type > &rtVector2) const
Base class for kernel functions Base class for kernel function used for example by the support vector...
Definition: CKernel.h:46
double dR
Definition: CKernel.h:205
Gaussian kernel function.
Definition: CKernel.h:79
Template object for vectors of single and double precision and integer.
Definition: CDenseVector.h:37
Double-Exponential kernel function.
Definition: CKernel.h:268
CMultiVariateGaussianKernel< Type > * clone() const
templatized vector for numerical applications
Definition: CMatrix.h:39
CPolynomialKernel< Type > & operator=(const CPolynomialKernel< Type > &rtKernel)
Type execute(const CVector< Type > &rtVector1, const CVector< Type > &rtVector2) const
Base class for all object.
Definition: CObject.h:51
virtual bool serialize2(CArchiv &tA)
virtual string className() const
Returns the class name.
Definition: CKernel.h:64
Type execute(const CVector< Type > &rtVector1, const CVector< Type > &rtVector2) const
string factoryObject()
Definition: CKernel.h:251
void _destroy_float_kernel(CKernel< float > *)
Definition: CKernel.h:376
CGaussianKernel & operator=(const CGaussianKernel< Type > &rtKernel)
bool serialize(fstream &stream, IO_MODE mode)
Read/write from binary stream.
string factoryObject()
Definition: CKernel.h:131
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CKernel.h:344
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CObject.h:93
Polynomial kernel function.
Definition: CKernel.h:202
CDoubleExponentialKernel(Type tBandwidth=1)
void setBandwidth(Type tB)
Definition: CKernel.h:58
CDoubleExponentialKernel< Type > & operator=(const CDoubleExponentialKernel< Type > &rtKernel)
virtual bool isA(const char *acClass) const
Check if the object is an instance of the class with given name.
Definition: CKernel.h:92
int iDegree
Definition: CKernel.h:300
CKernel< float > * _create_float_kernel()
Definition: CKernel.h:375
virtual string className() const
Returns the class name.
Definition: CKernel.h:313
Type execute(const CVector< Type > &rtVector1, const CVector< Type > &rtVector2) const
virtual string className() const
Returns the class name.
Definition: CKernel.h:215
CChiSquaredKernel< Type > & operator=(const CChiSquaredKernel< Type > &rtKernel)
Type execute(const CVector< Type > &rtVector1, const CVector< Type > &rtVector2) const
Type execute(const CVector< Type > &rtVector1, const CVector< Type > &rtVector2) const
CMultiVariateGaussianKernel & operator=(const CMultiVariateGaussianKernel< Type > &rtKernel)
string factoryObject()
Definition: CKernel.h:281
ANOVA kernel function.
Definition: CKernel.h:297
CPlummerKernel(Type tBandwidth=1)
Linear kernel function.
Definition: CKernel.h:237