source: trunk/yat/utility/MatrixWeighted.cc

Last change on this file was 4060, checked in by Peter, 3 months ago

prefer throw_with_nested, when info in the caught expression makes sense to expose

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
  • Property svndigest:ignore set to 1379
File size: 6.0 KB
Line 
1// $Id: MatrixWeighted.cc 4060 2021-05-10 02:57:01Z 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  Copyright (C) 2009, 2010, 2012, 2017 Peter Johansson
9
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <config.h>
27
28#include "MatrixWeighted.h"
29
30#include "DataIterator.h"
31#include "Exception.h"
32#include "Matrix.h"
33#include "utility.h"
34#include "WeightIterator.h"
35
36#include <cassert>
37#include <sstream>
38#include <utility>
39#include <vector>
40
41namespace theplu {
42namespace yat {
43namespace utility {
44
45
46  MatrixWeighted::MatrixWeighted(void)
47    : columns_(0)
48  {
49  }
50
51
52  MatrixWeighted::MatrixWeighted(size_t r, size_t c, double x, double w)
53    : vec_(std::vector<DataWeight>(r*c, DataWeight(x,w))), columns_(c)
54  {}
55
56
57  MatrixWeighted::MatrixWeighted(const MatrixWeighted& other)
58    : vec_(other.vec_), columns_(other.columns_)
59  {}
60
61
62  MatrixWeighted::MatrixWeighted(MatrixWeighted&& other)
63    : vec_(std::move(other.vec_)), columns_(other.columns_)
64  {}
65
66
67  MatrixWeighted::MatrixWeighted(const Matrix& data)
68  {
69    copy(data);
70  }
71
72
73  MatrixWeighted::MatrixWeighted(std::istream& is, char sep)
74  {
75    if (!is.good())
76      throw utility::IO_error("MatrixWeighted: istream is not good");
77
78    // read the data file and store in stl vectors (dynamically
79    // expandable)
80    std::vector<std::vector<double> > data_matrix;
81    try {
82      load(is, data_matrix, sep, '\n', true);
83    }
84    catch (utility::IO_error& e) {
85      std::stringstream ss(e.what());
86      ss << "\nMatrixWeighted(std::istream&): invalid dimensions\n";
87      std::throw_with_nested(IO_error(ss.str()));
88    }
89    catch (runtime_error& e) {
90      std::stringstream ss(e.what());
91      ss << "\nMatrixWeighted(std::istream&): invalid matrix element\n";
92      std::throw_with_nested(IO_error(ss.str()));
93    }
94
95    unsigned int nof_rows = data_matrix.size();
96    // if stream was empty, create nothing
97    if (!nof_rows)
98      return;
99
100    unsigned int nof_columns=data_matrix[0].size();
101
102    resize(nof_rows, nof_columns);
103    for (size_t i=0; i<rows(); ++i) {
104      std::copy(data_matrix[i].begin(), data_matrix[i].end(), 
105                data_iterator(begin_row(i)));
106      binary_weight(data_matrix[i].begin(), data_matrix[i].end(), 
107                    weight_iterator(begin_row(i)));
108    }
109  }
110
111 
112  MatrixWeighted::iterator MatrixWeighted::begin(void)
113  {
114    return iterator(vec_.begin());
115  }
116
117
118  MatrixWeighted::const_iterator MatrixWeighted::begin(void) const
119  {
120    return const_iterator(vec_.begin());
121  }
122
123
124  MatrixWeighted::column_iterator MatrixWeighted::begin_column(size_t i)
125  {
126    return column_iterator(vec_.begin()+i, columns_);
127  }
128
129
130  MatrixWeighted::const_column_iterator
131  MatrixWeighted::begin_column(size_t i) const
132  {
133    return const_column_iterator(vec_.begin()+i, columns_);
134  }
135
136
137  MatrixWeighted::row_iterator MatrixWeighted::begin_row(size_t i)
138  {
139    return row_iterator(vec_.begin()+columns_*i);
140  }
141
142
143  MatrixWeighted::const_row_iterator MatrixWeighted::begin_row(size_t i) const
144  {
145    return const_row_iterator(vec_.begin()+columns_*i);
146  }
147
148
149  size_t MatrixWeighted::columns(void) const
150  {
151    return columns_;
152  }
153
154
155  void MatrixWeighted::copy(const Matrix& data)
156  {
157    resize(data.rows(), data.columns());
158    assert(rows()==data.rows());
159    assert(columns()==data.columns());
160    std::copy(data.begin(), data.end(), data_iterator(vec_.begin()));
161    binary_weight(data.begin(), data.end(), weight_iterator(vec_.begin()));
162  }
163
164
165  MatrixWeighted::iterator MatrixWeighted::end(void)
166  {
167    return iterator(vec_.end());
168  }
169
170
171  MatrixWeighted::const_iterator MatrixWeighted::end(void) const
172  {
173    return const_iterator(vec_.end());
174  }
175
176
177  MatrixWeighted::column_iterator MatrixWeighted::end_column(size_t i)
178  {
179    return column_iterator(vec_.begin()+i+vec_.size(), columns_);
180  }
181
182
183  MatrixWeighted::const_column_iterator
184  MatrixWeighted::end_column(size_t i) const
185  {
186    return const_column_iterator(vec_.begin()+i+vec_.size(), columns_);
187  }
188
189
190  MatrixWeighted::row_iterator MatrixWeighted::end_row(size_t i)
191  {
192    return row_iterator(vec_.begin()+columns_*(i+1));
193  }
194
195
196  MatrixWeighted::const_row_iterator MatrixWeighted::end_row(size_t i) const
197  {
198    return const_row_iterator(vec_.begin()+columns_*(i+1));
199  }
200
201
202  void MatrixWeighted::swap(MatrixWeighted& other)
203  {
204    std::swap(vec_, other.vec_);
205    std::swap(columns_, other.columns_);
206  }
207
208
209  void MatrixWeighted::resize(size_t rows, size_t columns)
210  {
211    if (!rows ^ !columns) { // logical xor
212      std::stringstream msg;
213      msg << "MatrixWeighted::resize(" << rows << ", " << columns
214          << "): incorrect dimensions\n";
215      throw runtime_error(msg.str());
216    }
217    columns_ = columns;
218    vec_.resize(rows*columns);
219    assert(this->rows()==rows);
220    assert(this->columns()==columns);
221  }
222
223 
224  size_t MatrixWeighted::rows(void) const
225  {
226    if (vec_.size())
227      return vec_.size()/columns_;
228    return 0;
229  }
230
231
232  MatrixWeighted& MatrixWeighted::operator=(MatrixWeighted&& other)
233  {
234    MatrixWeighted tmp(std::move(other));
235    swap(tmp);
236    return *this;
237  }
238
239
240  DataWeight& MatrixWeighted::operator()(size_t row, size_t column)
241  {
242    assert(column<columns_);
243    assert(row*column < vec_.size());
244    return vec_[row*columns_ + column];
245  }
246
247
248  const DataWeight& MatrixWeighted::operator()(size_t row, size_t column) const
249  {
250    assert(column<columns_);
251    assert(row*column < vec_.size());
252    return vec_[row*columns_ + column];
253  }
254
255
256  void swap(MatrixWeighted& lhs, MatrixWeighted& rhs)
257  {
258    lhs.swap(rhs);
259  }
260
261}}} // of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.