SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ProductKernel.h
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  * Copyright (C) 2012 Jacob Walker
8  *
9  * Code adapted from CCombinedKernel
10  */
11 
12 #ifndef _PRODUCTKERNEL_H___
13 #define _PRODUCTKERNEL_H___
14 
15 #include <shogun/lib/List.h>
16 #include <shogun/io/SGIO.h>
17 #include <shogun/kernel/Kernel.h>
18 
21 
22 namespace shogun
23 {
24 class CFeatures;
25 class CCombinedFeatures;
26 class CList;
27 class CListElement;
42 class CProductKernel : public CKernel
43 {
44  public:
50  CProductKernel(int32_t size=10);
51 
52  virtual ~CProductKernel();
53 
60  virtual bool init(CFeatures* lhs, CFeatures* rhs);
61 
63  virtual void cleanup();
64 
70  {
71  return K_PRODUCT;
72  }
73 
79  {
80  return F_UNKNOWN;
81  }
82 
88  {
89  return C_COMBINED;
90  }
91 
96  virtual const char* get_name() const { return "ProductKernel"; }
97 
99  void list_kernels();
100 
106  {
108  }
109 
116  {
117  return (CKernel*) kernel_list->get_first_element(current);
118  }
119 
125  inline CKernel* get_kernel(int32_t idx)
126  {
127  CKernel * k = get_first_kernel();
128  for (int32_t i=0; i<idx; i++)
129  {
130  SG_UNREF(k);
131  k = get_next_kernel();
132  }
133  return k;
134  }
135 
141  {
142  return (CKernel*) kernel_list->get_last_element();
143  }
144 
150  {
151  return (CKernel*) kernel_list->get_next_element();
152  }
153 
160  {
161  return (CKernel*) kernel_list->get_next_element(current);
162  }
163 
169  inline bool insert_kernel(CKernel* k)
170  {
171  ASSERT(k);
173 
174  if (!(k->has_property(KP_LINADD)))
176 
177  return kernel_list->insert_element(k);
178  }
179 
185  inline bool append_kernel(CKernel* k)
186  {
187  ASSERT(k);
189 
190  if (!(k->has_property(KP_LINADD)))
192 
193  return kernel_list->append_element(k);
194  }
195 
196 
201  inline bool delete_kernel()
202  {
204  SG_UNREF(k);
205 
206  if (!k)
207  {
208  num_lhs=0;
209  num_rhs=0;
210  }
211 
212  return (k!=NULL);
213  }
214 
215 
220  inline int32_t get_num_subkernels()
221  {
222  return kernel_list->get_num_elements();
223  }
224 
229  virtual inline bool has_features()
230  {
231  return initialized;
232  }
233 
235  virtual void remove_lhs();
236 
238  virtual void remove_rhs();
239 
241  virtual void remove_lhs_and_rhs();
242 
244  bool precompute_subkernels();
245 
250  {
251  return dynamic_cast<CProductKernel*>(n);
252  }
253 
263  CSGObject* obj, index_t index);
264 
270 
271  protected:
278  virtual float64_t compute(int32_t x, int32_t y);
279 
286  {
287  ASSERT(k);
288 
289  if (k->get_num_vec_lhs())
290  {
291  if (num_lhs)
294 
295  if (!get_num_subkernels())
296  {
297  initialized=true;
298 #ifdef USE_SVMLIGHT
299  cache_reset();
300 #endif //USE_SVMLIGHT
301  }
302  }
303  else
304  initialized=false;
305 
306  if (k->get_num_vec_rhs())
307  {
308  if (num_rhs)
311 
312  if (!get_num_subkernels())
313  {
314  initialized=true;
315 #ifdef USE_SVMLIGHT
316  cache_reset();
317 #endif //USE_SVMLIGHT
318  }
319  }
320  else
321  initialized=false;
322  }
323 
324  private:
325  void init();
326 
327  protected:
332 };
333 }
334 #endif /* _PRODUCTKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation