source: trunk/yat/classifier/MatrixLookupWeighted.cc

Last change on this file was 2881, checked in by Peter, 10 years ago

Define PP variables in config.h rather than in CPPFLAGS. Include
config.h into all source files. Only ammend CXXFLAGS with '-Wall
-pedantic' when --enable-debug. In default mode we respect CXXFLAGS
value set by user, or set to default value '-O3'.

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