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

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

merge patch release 0.4.2 to trunk. Delta 0.4.2-0.4.1

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