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

Last change on this file since 1797 was 1797, checked in by Peter, 12 years ago

updating copyright statements

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