SHOGUN  v3.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 
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 CDynamicObjectArray;
27 
39 class CProductKernel : public CKernel
40 {
41  public:
46  CProductKernel(int32_t size=10);
47 
48  virtual ~CProductKernel();
49 
56  virtual bool init(CFeatures* lhs, CFeatures* rhs);
57 
59  virtual void cleanup();
60 
65  virtual EKernelType get_kernel_type() { return K_PRODUCT; }
66 
71  virtual EFeatureType get_feature_type() { return F_UNKNOWN; }
72 
78 
83  virtual const char* get_name() const { return "ProductKernel"; }
84 
86  void list_kernels();
87 
93  inline CKernel* get_kernel(int32_t idx)
94  {
95  return (CKernel*) kernel_array->get_element(idx);
96  }
97 
105  inline bool insert_kernel(CKernel* k, int32_t idx)
106  {
107  ASSERT(k)
109 
110  if (!(k->has_property(KP_LINADD)))
112 
113  return kernel_array->insert_element(k, idx);
114  }
115 
121  inline bool append_kernel(CKernel* k)
122  {
123  ASSERT(k)
125 
126  if (!(k->has_property(KP_LINADD)))
128 
129  int32_t n = get_num_subkernels();
131  return n+1==get_num_subkernels();
132  }
133 
139  inline bool delete_kernel(int32_t idx)
140  {
141  return kernel_array->delete_element(idx);
142  }
143 
148  inline int32_t get_num_subkernels()
149  {
150  return kernel_array->get_num_elements();
151  }
152 
157  virtual bool has_features()
158  {
159  return initialized;
160  }
161 
163  virtual void remove_lhs();
164 
166  virtual void remove_rhs();
167 
169  virtual void remove_lhs_and_rhs();
170 
172  bool precompute_subkernels();
173 
178  {
179  return dynamic_cast<CProductKernel*>(n);
180  }
181 
190  index_t index=-1);
191 
197  {
199  return kernel_array;
200  }
201 
202  protected:
209  virtual float64_t compute(int32_t x, int32_t y);
210 
217  {
218  ASSERT(k)
219 
220  if (k->get_num_vec_lhs())
221  {
222  if (num_lhs)
225 
226  if (!get_num_subkernels())
227  {
228  initialized=true;
229 #ifdef USE_SVMLIGHT
230  cache_reset();
231 #endif //USE_SVMLIGHT
232  }
233  }
234  else
235  initialized=false;
236 
237  if (k->get_num_vec_rhs())
238  {
239  if (num_rhs)
242 
243  if (!get_num_subkernels())
244  {
245  initialized=true;
246 #ifdef USE_SVMLIGHT
247  cache_reset();
248 #endif //USE_SVMLIGHT
249  }
250  }
251  else
252  initialized=false;
253  }
254 
255  private:
256  void init();
257 
258  protected:
263 };
264 }
265 #endif /* _PRODUCTKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation