source: branches/0.4-stable/yat/classifier/MatrixLookup.cc @ 1392

Last change on this file since 1392 was 1392, checked in by Peter, 15 years ago

trac has moved

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 6.2 KB
Line 
1// $Id$
2
3/*
4  Copyright (C) 2005 Peter Johansson, Markus Ringnér
5  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
6  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
7  Copyright (C) 2008 Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "MatrixLookup.h"
28#include "yat/utility/Matrix.h"
29
30#include <algorithm>
31#include <cassert>
32#include <fstream>
33#include <vector>
34
35namespace theplu {
36namespace yat {
37namespace classifier {
38
39  MatrixLookup::MatrixLookup(const utility::Matrix& data, const bool own)
40    : data_(MatrixP(&data, own))
41  {
42    column_index_ = utility::Index(data.columns());
43    row_index_ = utility::Index(data.rows());
44    assert(rows()==data.rows());
45    assert(columns()==data.columns());
46    assert(validate());
47  }
48 
49
50
51  MatrixLookup::MatrixLookup(const utility::Matrix& data, 
52                             const utility::Index& row, 
53                             const utility::Index& col)
54    : column_index_(col),
55      data_(MatrixP(&data, false)), 
56      row_index_(row)
57
58  {
59    assert(rows()==row.size());
60    assert(columns()==col.size());
61    assert(validate());
62  }
63 
64
65
66  MatrixLookup::MatrixLookup(const utility::Matrix& data, 
67                             const utility::Index& index, 
68                             const bool row)
69    : data_(MatrixP(&data, false))
70  {
71    if (row){
72      row_index_=index;
73      column_index_ = utility::Index(data.columns());
74    }
75    else{
76      column_index_=index;
77      row_index_ = utility::Index(data.rows());
78    }
79    assert(row || rows()==data.rows());
80    assert(row || columns()==index.size());
81    assert(!row || rows()==index.size());
82    assert(!row || columns()==data.columns());
83    assert(validate());
84  }
85 
86
87
88  MatrixLookup::MatrixLookup(const MatrixLookup& other)
89    : column_index_(other.column_index_), 
90      data_(other.data_),
91      row_index_(other.row_index_)
92  {
93    assert(validate());
94  }
95
96
97
98  MatrixLookup::MatrixLookup(const MatrixLookup& other, 
99                             const utility::Index& row, 
100                             const utility::Index& col)
101    : column_index_(utility::Index(other.column_index_, col)), 
102      data_(other.data_), row_index_(utility::Index(other.row_index_, row)) 
103  {
104    assert(rows()==row.size());
105    assert(columns()==col.size());
106    assert(validate());
107  }
108 
109
110
111  MatrixLookup::MatrixLookup(const MatrixLookup& other, 
112                             const utility::Index& index, bool row)
113    : data_(other.data_)
114  {
115    if (row){
116      row_index_ = utility::Index(other.row_index_, index);
117      column_index_= other.column_index_;
118    }
119    else{
120      column_index_ = utility::Index(other.column_index_, index);
121      row_index_= other.row_index_;
122    }
123    assert(validate());
124  }
125 
126
127
128  MatrixLookup::MatrixLookup(const size_t rows, const size_t columns, 
129                             const double value)
130    : data_(MatrixP(new utility::Matrix(1,1,value)))
131  {   
132    column_index_ = utility::Index(std::vector<size_t>(columns, 0));
133    row_index_ = utility::Index(std::vector<size_t>(rows,0));
134    assert(validate());
135  }
136
137
138  MatrixLookup::MatrixLookup(std::istream& is, char sep)
139    : data_(MatrixP(new utility::Matrix(is,sep)))
140  {
141    column_index_ = utility::Index(data_->columns());
142    row_index_ = utility::Index(data_->rows());
143    assert(validate());
144  }
145
146
147  MatrixLookup::~MatrixLookup(void)
148  {
149  }
150
151
152  MatrixLookup::const_iterator MatrixLookup::begin(void) const
153  {
154    return const_iterator(const_iterator::iterator_type(*this, 0, 0), 1);
155  }
156
157
158  MatrixLookup::const_column_iterator MatrixLookup::begin_column(size_t i) const
159  {
160    return const_column_iterator(const_column_iterator::iterator_type(*this, 0,
161                                                                      i),
162                                 columns());
163  }
164
165
166  MatrixLookup::const_row_iterator MatrixLookup::begin_row(size_t i) const
167  {
168    return const_row_iterator(const_row_iterator::iterator_type(*this,i,0), 1);
169  }
170
171
172  size_t MatrixLookup::columns(void) const
173  {
174    return column_index_.size();
175  }
176
177
178  MatrixLookup::const_iterator MatrixLookup::end(void) const
179  {
180    return const_iterator(const_iterator::iterator_type(*this, rows(), 0), 1);
181  }
182
183
184  MatrixLookup::const_column_iterator MatrixLookup::end_column(size_t i) const
185  {
186    return 
187      const_column_iterator(const_column_iterator::iterator_type(*this,rows(),
188                                                                      i), 
189                            columns());
190  }
191
192
193  MatrixLookup::const_row_iterator MatrixLookup::end_row(size_t i) const
194  {
195    return const_row_iterator(const_row_iterator::iterator_type(*this,i+1,0),1);
196  }
197
198
199  size_t MatrixLookup::rows(void) const
200  {
201    return row_index_.size();
202  }
203
204
205  bool MatrixLookup::validate(void) const
206  {
207    for (size_t i=0; i<row_index_.size(); ++i)
208      if (row_index_[i]>=data_->rows())
209        return false;
210    for (size_t i=0; i<column_index_.size(); ++i)
211      if (column_index_[i]>=data_->columns())
212        return false;
213    return true;
214  }
215
216
217  bool MatrixLookup::weighted(void) const 
218  {
219    return false;
220  }
221
222
223
224  double MatrixLookup::operator()(const size_t row, const size_t column) const
225  {
226    assert(row<rows());
227    assert(column<columns());
228    assert(row_index_[row]<data_->rows());
229    assert(column_index_[column]<data_->columns());
230    return (*data_)(row_index_[row], column_index_[column]);
231  }
232
233
234
235  const MatrixLookup& MatrixLookup::operator=(const MatrixLookup& other)
236  {
237    if (this!=&other){
238      row_index_ = other.row_index_;
239      column_index_ = other.column_index_;
240      data_ = other.data_;
241    }
242    assert(validate());
243    return *this;
244  }
245
246
247  std::ostream& operator<<(std::ostream& s, const MatrixLookup& m)
248  {
249    s.setf(std::ios::dec);
250    s.precision(12);
251    for(size_t i=0, j=0; i<m.rows(); i++)
252      for (j=0; j<m.columns(); j++) {
253        s << m(i,j);
254        if (j<m.columns()-1)
255          s << s.fill();
256        else if (i<m.rows()-1)
257          s << "\n";
258      }
259    return s;
260  }
261
262
263
264}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.