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