source: trunk/yat/classifier/SVindex.cc @ 1274

Last change on this file since 1274 was 1167, checked in by Peter, 13 years ago

refs #342 in KernelLookup? and DataLookup2D - nothing changed in MatrixLookups?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1// $Id: SVindex.cc 1167 2008-02-26 20:02:28Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of the yat library, http://trac.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library 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 GNU
16  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
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "SVindex.h"
25#include "yat/random/random.h"
26#include "yat/statistics/Averager.h"
27#include "yat/utility/Vector.h"
28
29#include <algorithm>
30#include <cassert>
31#include <cctype>
32#include <cmath>
33#include <limits>
34#include <utility>
35#include <vector>
36
37namespace theplu {
38namespace yat {
39namespace classifier { 
40
41  SVindex::SVindex(void)
42    : nof_sv_(0), vec_(std::vector<size_t>(0))
43  {
44  }
45
46  SVindex::SVindex(const size_t n)
47    : nof_sv_(0), vec_(std::vector<size_t>(n))
48  {
49    for (size_t i=0; i<vec_.size(); i++)
50      vec_[i]=i;
51  }
52
53  size_t SVindex::index_first(void) const
54  {
55    assert(index_first_<size());
56    return index_first_;
57  }
58
59  size_t SVindex::index_second(void) const
60  {
61    assert(index_second_<size());
62    return index_second_;
63  }
64
65  void SVindex::init(const utility::Vector& alpha, const double tol)
66  {
67    nof_sv_=0;
68    size_t nof_nsv=0;
69    vec_.resize(alpha.size());
70    for (size_t i=0; i<alpha.size(); i++) 
71      if (alpha(i)<tol){
72        nof_nsv++;
73        vec_[vec_.size()-nof_nsv]=i;
74      }
75      else{
76        vec_[nof_sv_]=i;
77        nof_sv_++;
78      }
79    assert(nof_sv_+nof_nsv==vec_.size());
80
81  }
82
83  size_t SVindex::nof_sv(void) const
84  {
85    return nof_sv_;
86  }
87
88  void SVindex::nsv_first(void)
89  {
90    // if already nsv, do nothing
91    if ( !(index_first_<nof_sv()) )
92      return;
93   
94    if(index_second_==nof_sv_-1)
95      index_second_=index_first_;
96    vec_[index_first_]=vec_[nof_sv_-1];
97    vec_[nof_sv_-1]=value_first_;
98    index_first_=nof_sv_-1;
99   
100    nof_sv_--;
101  }
102
103  void SVindex::nsv_second(void)
104  {
105    // if already nsv, do nothing
106    if ( !(index_second_<nof_sv()) )
107      return;
108
109    if(index_first_==nof_sv_-1)
110      index_first_=index_second_;
111    vec_[index_second_]=vec_[nof_sv_-1];
112    vec_[nof_sv_-1]=value_second_;
113    index_second_ = nof_sv_-1;
114   
115    nof_sv_--;
116  }
117
118
119  void SVindex::shuffle(void)
120  {
121    random::random_shuffle(vec_.begin()+nof_sv_, vec_.end()); 
122  }
123
124  size_t SVindex::size(void) const
125  {
126    return vec_.size();
127  }
128
129  void SVindex::sv_first(void)
130  {
131    // if already sv, do nothing
132    if (index_first_<nof_sv())
133      return;
134
135    // swap elements
136    if(index_second_==nof_sv_){
137      index_second_=index_first_;
138    }
139    vec_[index_first_]=vec_[nof_sv_];
140    vec_[nof_sv_]=value_first_;
141    index_first_ = nof_sv_;
142
143    nof_sv_++;
144
145  }
146
147  void SVindex::sv_second(void)
148  {
149    // if already sv, do nothing
150    if (index_second_<nof_sv())
151      return;
152
153    // swap elements
154    if(index_first_==nof_sv_){
155      index_first_=index_second_;
156    }
157
158    vec_[index_second_]=vec_[nof_sv_];
159    vec_[nof_sv_]=value_second_;
160    index_second_=nof_sv_;
161
162    nof_sv_++;
163  }
164
165  void SVindex::update_first(const size_t i)
166  {
167    assert(i<size());
168    index_first_=i;
169    value_first_=vec_[i];
170  }
171
172  void SVindex::update_second(const size_t i)
173  {
174    assert(i<size());
175    index_second_=i;
176    value_second_=vec_[i];
177  }
178
179  size_t SVindex::value_first(void) const
180  {
181    assert(value_first_<size());
182    return value_first_;
183  }
184
185  size_t SVindex::value_second(void) const
186  {
187    assert(value_second_<size());
188    return value_second_;
189  }
190
191  size_t SVindex::operator()(size_t i) const
192  {
193    assert(i<size());
194    assert(vec_[i]<size());
195    return vec_[i];
196  }
197
198}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.