iRoCS Toolbox  1.1.0
SVC_Q.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2004-2015 Olaf Ronneberger, Florian Pigorsch, Jörg Mechnich,
4  * Thorsten Falk
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title: SVC kernel
26 ** $RCSfile$
27 ** $Revision: 4820 $$Name$
28 ** $Date: 2011-11-08 10:57:01 +0100 (Tue, 08 Nov 2011) $
29 ** Copyright: LGPL $Author: tschmidt $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.2 2004/09/03 11:35:04 ronneber
38 ** - replaced Chi-Jen Lin's own min,max,swap with std::min,
39 ** std::max. std::swap to compile with programs, that "#include
40 ** <algorithm>" and do a "using namespace std;"
41 **
42 ** Revision 1.1 2004/08/26 08:36:59 ronneber
43 ** initital import
44 **
45 ** Revision 1.2 2002/05/06 12:25:29 ronneber
46 ** - removed Parameter struct dependencies (now passes chache size directly)
47 **
48 ** Revision 1.1 2002/03/26 12:44:02 ronneber
49 ** restructured for autoconf
50 **
51 ** Revision 1.5 2002/03/13 13:41:53 pigorsch
52 ** * made parameter param of constructor const
53 **
54 ** Revision 1.4 2002/01/21 13:46:18 pigorsch
55 ** * removed parameter "params" from constructor of CachedKernel
56 ** * child class Parameters of class Model is now separate class
57 **
58 ** Revision 1.3 2002/01/14 14:02:45 pigorsch
59 ** * cosmetic code fixes
60 **
61 ** Revision 1.2 2001/12/17 13:18:30 pigorsch
62 ** *** empty log message ***
63 **
64 ** Revision 1.1 2001/12/11 11:03:00 pigorsch
65 ** Initial Revision
66 **
67 **
68 **
69 **************************************************************************/
70 
71 #ifndef SVC_Q_HH
72 #define SVC_Q_HH
73 
74 #ifdef HAVE_CONFIG_H
75 #include <config.hh>
76 #endif
77 
78 #include <algorithm>
79 
80 #include "SVM_Problem.hh"
81 #include "Kernel.hh"
82 #include "Cache.hh"
83 
84 namespace svt
85 {
86  //
87  // Q matrices for various formulations
88  //
89  template< typename FV, typename KF>
90  class SVC_Q: public Kernel<FV,KF>
91  {
92  public:
93 
94  SVC_Q( const KF& kernel, const SVM_Problem<FV>& prob, float cacheSizeMB,
95  const schar *y_)
96  :Kernel<FV,KF>( kernel, prob.l, prob.x)
97  {
98  clone(y,y_,prob.l);
99  cache = new Cache(prob.l,(int)(cacheSizeMB*(1<<20)));
100  }
101 
102  private:
103  SVC_Q( const SVC_Q<FV,KF>&); // forbid copying
104  public:
105 
106 
107  Qfloat *get_Q(int i, int len) const
108  {
109  Qfloat *data;
110  int start;
111  if((start = static_cast<int>(cache->get_data(i,&data,len))) <
112  len)
113  {
114  for(int j = start; j < len; j++)
115  data[j] = static_cast<Qfloat>(
116  y[i] * y[j] * this->kernel_function(i,j));
117  }
118  return data;
119  }
120 
121  void swap_index(int i, int j) const
122  {
123  cache->swap_index(i,j);
125  std::swap(y[i],y[j]);
126  }
127 
129  {
130  delete[] y;
131  delete cache;
132  }
133 private:
134  schar *y;
135  Cache *cache;
136  };
137 }
138 
139 
140 
141 // old code from libsvmtl
142 //
143 
144 // class SVC_Q: public svt::CachedKernel<FV, KF>
145 // {
146 // public:
147 // template< typename FVIter, typename YIter>
148 // SVC_Q(
149 // const KF& kernel,
150 // int cacheSizeBytes,
151 // FVIter FV_begin, const FVIter& FV_end,
152 // YIter Y_begin, const YIter& Y_end)
153 // :svt::CachedKernel<FV, KF>(kernel, FV_begin, FV_end),
154 // p_y(Y_begin, Y_end),
155 // cache(p_x.size(), cacheSizeBytes)
156 // {};
157 //
158 // float*
159 // get_Q(
160 // int i,
161 // int len) const
162 // {
163 // float* data;
164 // int start;
165 // if ((start=cache.get_data(i, &data, len))<len)
166 // {
167 // for (int j=start; j<len; j++)
168 // {
169 // data[j]=(float)(p_y[i]*p_y[j]*(k_function)(i, j));
170 // }
171 // }
172 // return data;
173 // };
174 //
175 // void
176 // swap_index(
177 // int i,
178 // int j)
179 // {
180 // cache.swap_index(i, j);
181 // svt::CachedKernel<FV, KF>::swap_index(i, j);
182 // std::swap(p_y[i], p_y[j]);
183 // };
184 //
185 // private:
186 // std::vector<signed char> p_y;
187 // mutable svt::Cache cache;
188 // };
189 // }
190 //
191 #endif
SVC_Q(const KF &kernel, const SVM_Problem< FV > &prob, float cacheSizeMB, const schar *y_)
Definition: SVC_Q.hh:94
long get_data(const long index, Qfloat **data, long len)
void swap_index(long i, long j)
void swap_index(int i, int j) const
Definition: SVC_Q.hh:121
signed char schar
Definition: svm_defines.hh:67
~SVC_Q()
Definition: SVC_Q.hh:128
double kernel_function(int i, int j) const
Definition: Kernel.hh:92
Qfloat * get_Q(int i, int len) const
Definition: SVC_Q.hh:107
void clone(T *&dst, S *src, int n)
Definition: svm_defines.hh:80
float Qfloat
Definition: svm_defines.hh:66
virtual void swap_index(int i, int j) const
Definition: Kernel.hh:86