source: trunk/yat/utility/MatrixWeighted.cc @ 1615

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

fixes #444

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1// $Id: MatrixWeighted.cc 1589 2008-10-17 16:10:43Z peter $
2
3/*
4  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
5  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
7  Copyright (C) 2007, 2008 Jari Häkkinen, 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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "MatrixWeighted.h"
26
27#include "DataIterator.h"
28#include "Matrix.h"
29#include "WeightIterator.h"
30
31#include <cassert>
32#include <vector>
33
34namespace theplu {
35namespace yat {
36namespace utility {
37
38
39  MatrixWeighted::MatrixWeighted(void)
40    : columns_(0)
41  {
42  }
43
44
45  MatrixWeighted::MatrixWeighted(size_t r, size_t c, double x, double w)
46    : vec_(std::vector<DataWeight>(r*c, DataWeight(x,w))), columns_(c)
47  {}
48
49 
50  MatrixWeighted::MatrixWeighted(const MatrixWeighted& other)
51    : vec_(other.vec_), columns_(other.columns_)
52  {}
53
54
55  MatrixWeighted::MatrixWeighted(const Matrix& data)
56  {
57    copy(data);
58  }
59
60 
61  MatrixWeighted::MatrixWeighted(std::istream& is, char sep)
62  {
63    Matrix data(is, sep);
64    copy(data);
65  }
66
67 
68  MatrixWeighted::iterator MatrixWeighted::begin(void)
69  {
70    return iterator(vec_.begin());
71  }
72
73
74  MatrixWeighted::const_iterator MatrixWeighted::begin(void) const
75  {
76    return const_iterator(vec_.begin());
77  }
78
79
80  MatrixWeighted::column_iterator MatrixWeighted::begin_column(size_t i)
81  {
82    return column_iterator(vec_.begin()+i, columns_);
83  }
84
85
86  MatrixWeighted::const_column_iterator
87  MatrixWeighted::begin_column(size_t i) const
88  {
89    return const_column_iterator(vec_.begin()+i, columns_);
90  }
91
92
93  MatrixWeighted::row_iterator MatrixWeighted::begin_row(size_t i)
94  {
95    return row_iterator(vec_.begin()+columns_*i);
96  }
97
98
99  MatrixWeighted::const_row_iterator MatrixWeighted::begin_row(size_t i) const
100  {
101    return const_row_iterator(vec_.begin()+columns_*i);
102  }
103
104
105  size_t MatrixWeighted::columns(void) const
106  {
107    return columns_;
108  }
109
110
111  void MatrixWeighted::copy(const Matrix& data)
112  {
113    Matrix weight;
114    nan(data, weight);
115    copy(data, weight);
116  }
117
118
119  void MatrixWeighted::copy(const Matrix& data, const Matrix& weight)
120  {
121    assert(data.rows()==weight.rows());
122    assert(data.columns()==weight.columns());
123    columns_ = data.columns();
124    resize(data.rows(), data.columns());
125    assert(rows()==data.rows());
126    assert(columns()==data.columns());
127    std::copy(data.begin(), data.end(), data_iterator(vec_.begin()));
128    std::copy(weight.begin(), weight.end(), weight_iterator(vec_.begin()));
129  }
130
131
132  MatrixWeighted::iterator MatrixWeighted::end(void)
133  {
134    return iterator(vec_.end());
135  }
136
137
138  MatrixWeighted::const_iterator MatrixWeighted::end(void) const
139  {
140    return const_iterator(vec_.end());
141  }
142
143
144  MatrixWeighted::column_iterator MatrixWeighted::end_column(size_t i)
145  {
146    return column_iterator(vec_.begin()+i+vec_.size(), columns_);
147  }
148
149
150  MatrixWeighted::const_column_iterator
151  MatrixWeighted::end_column(size_t i) const
152  {
153    return const_column_iterator(vec_.begin()+i+vec_.size(), columns_);
154  }
155
156
157  MatrixWeighted::row_iterator MatrixWeighted::end_row(size_t i)
158  {
159    return row_iterator(vec_.begin()+columns_*(i+1));
160  }
161
162
163  MatrixWeighted::const_row_iterator MatrixWeighted::end_row(size_t i) const
164  {
165    return const_row_iterator(vec_.begin()+columns_*(i+1));
166  }
167
168
169  void MatrixWeighted::resize(size_t rows, size_t columns)
170  {
171    columns_ = columns;
172    vec_.resize(rows*columns);
173    assert(this->rows()==rows);
174    assert(this->columns()==columns);
175  }
176
177 
178  size_t MatrixWeighted::rows(void) const
179  {
180    if (vec_.size())
181      return vec_.size()/columns_;
182    return 0;
183  }
184
185
186  DataWeight& MatrixWeighted::operator()(size_t row, size_t column)
187  {
188    assert(column<columns_);
189    assert(row*column < vec_.size());
190    return vec_[row*columns_ + column];
191  }
192
193
194  const DataWeight& MatrixWeighted::operator()(size_t row, size_t column) const
195  {
196    assert(column<columns_);
197    assert(row*column < vec_.size());
198    return vec_[row*columns_ + column];
199  }
200
201}}} // of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.