source: trunk/yat/utility/MatrixWeighted.h @ 1890

Last change on this file since 1890 was 1890, checked in by Peter, 12 years ago

fixes #454 - throw exception when detecting funky dimensions

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1#ifndef _theplu_yat_utility_matrix_weighted_
2#define _theplu_yat_utility_matrix_weighted_
3
4// $Id: MatrixWeighted.h 1890 2009-04-02 21:43:42Z peter $
5
6/*
7  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
8  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
10  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
11  Copyright (C) 2009 Peter Johansson
12
13  This file is part of the yat library, http://dev.thep.lu.se/yat
14
15  The yat library is free software; you can redistribute it and/or
16  modify it under the terms of the GNU General Public License as
17  published by the Free Software Foundation; either version 3 of the
18  License, or (at your option) any later version.
19
20  The yat library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  General Public License for more details.
24
25  You should have received a copy of the GNU General Public License
26  along with yat. If not, see <http://www.gnu.org/licenses/>.
27*/
28
29#include "DataWeight.h"
30#include "StrideIterator.h"
31
32#include <vector>
33
34namespace theplu {
35namespace yat {
36namespace utility {
37
38  class Matrix;
39
40  /**
41     \brief Weighted Matrix
42
43     \since New in yat 0.5
44  */
45  class MatrixWeighted
46  {
47  public:
48    /**
49       value_type is DataWeight
50     */
51    typedef DataWeight value_type;
52
53    /**
54       reference type is DataWeight&
55     */
56    typedef DataWeight& reference;
57
58    /**
59       const_reference type is const DataWeight&
60     */
61    typedef const DataWeight& const_reference;
62
63    /**
64       Mutable iterator that iterates over all elements
65     */
66    typedef std::vector<DataWeight>::iterator iterator;
67
68    /**
69       Read-only iterator that iterates over all elements
70     */
71    typedef std::vector<DataWeight>::const_iterator const_iterator;
72
73    /**
74       Mutable iterator that iterates over one column
75     */
76    typedef StrideIterator<std::vector<DataWeight>::iterator> column_iterator;
77
78    /**
79       Read-only iterator that iterates over one column
80     */
81    typedef StrideIterator<std::vector<DataWeight>::const_iterator> 
82    const_column_iterator;
83
84    /**
85       Mutable iterator that iterates over one row
86     */
87    typedef column_iterator row_iterator;
88
89    /**
90       Read-only iterator that iterates over one row
91     */
92    typedef const_column_iterator const_row_iterator;
93
94    /**
95       @brief The default constructor.
96   
97       Creates a zero size MatrixWeighted
98    */
99    MatrixWeighted(void);
100
101    /**
102       \brief Create a \a r times \a c MatrixWeighted
103
104       If \a r is zero \a c must be zero and vice versa.
105
106       Constructor allocating memory space for \a r times \a c
107       elements, and sets all elements to
108       DataWeight(\a init_value, \a init_weight)
109    */
110    MatrixWeighted(size_t r, size_t c, double init_value=0,
111                   double init_weight=1.0);
112
113    /**
114       \brief The copy constructor.
115    */
116    MatrixWeighted(const MatrixWeighted&);
117
118   
119    /**
120       \brief Constructor.
121
122       Data is copied from \a other and weights are calculated using
123       the binary_weight function, i.e., a NaN gets a zero weight and
124       other data values results in a unity weight (1.0).
125    */
126    explicit MatrixWeighted(const Matrix& other);
127
128    /**
129       \brief The istream constructor.
130
131       This constructor first creates a Matrix using the istream
132       constructor, and then constructs MatrixWeighted using
133       MatrixWeighted(const Matrix&).
134
135       \see Matrix(std::istream &, char)
136    */
137    explicit MatrixWeighted(std::istream &, char sep='\0'); 
138
139    /**
140       Iterator iterates along a row. When end of row is reached it
141       jumps to beginning of next row.
142
143       \return iterator pointing to upper-left element.
144     */
145    iterator begin(void);
146
147    /**
148       Iterator iterates along a row. When end of row is reached it
149       jumps to beginning of next row.
150
151       \return const_iterator pointing to upper-left element.
152     */
153    const_iterator begin(void) const;
154
155    /**
156       Iterator iterates along a column.
157
158       \return iterator pointing to first element of column \a i.
159     */
160    column_iterator begin_column(size_t i);
161
162    /**
163       Iterator iterates along a column.
164
165       \return const_iterator pointing to first element of column \a i.
166     */
167    const_column_iterator begin_column(size_t i) const;
168
169    /**
170       Iterator iterates along a row.
171
172       \return iterator pointing to first element of row \a i.
173     */
174    row_iterator begin_row(size_t i);
175
176    /**
177       Iterator iterates along a row.
178
179       \return const_iterator pointing to first element of row \a i.
180     */
181    const_row_iterator begin_row(size_t i) const;
182
183    /**
184       \return The number of columns in the matrix.
185    */
186    size_t columns(void) const;
187
188    /**
189       \return iterator pointing to end of matrix
190     */
191    iterator end(void);
192
193    /**
194       \return const_iterator pointing to end of matrix
195     */
196    const_iterator end(void) const;
197
198    /**
199       \return iterator pointing to end of column \a i
200     */
201    column_iterator end_column(size_t i);
202
203    /**
204       \return const_iterator pointing to end of column \a i
205     */
206    const_column_iterator end_column(size_t i) const;
207
208    /**
209       \return iterator pointing to end of row \a i
210     */
211    row_iterator end_row(size_t i);
212
213    /**
214       \return const_iterator pointing to end of row \a i
215     */
216    const_row_iterator end_row(size_t i) const;
217
218    /**
219       \brief Resize MatrixWeighted
220
221       Elements in MatrixWeighted are undefined after a resize.
222
223       If \a rows is zero \a columns must be zero and vice versa.
224
225       \note this function may invalidate iterators.
226    */
227    void resize(size_t rows, size_t columns);
228
229    /**
230       \return The number of rows in the matrix.
231    */
232    size_t rows(void) const;
233
234    /**
235       \brief swap objects
236
237       Takes constant time. Invalidates iterators.
238       There is no requirement on the size of \a other.
239     */
240    void swap(MatrixWeighted& other);
241
242    /**
243       \brief Element access operator.
244
245       \return Reference to the element position (\a row, \a column).
246    */
247    DataWeight& operator()(size_t row,size_t column);
248
249    /**
250       \brief Element access operator.
251
252       \return Const reference to the element position (\a row, \a
253       column).
254    */
255    const DataWeight& operator()(size_t row,size_t column) const;
256
257
258    /**
259       \brief The assignment operator.
260
261       \return A const reference to the resulting Matrix.
262    */
263    const MatrixWeighted& operator=(const MatrixWeighted& other);
264
265  private:
266    void copy(const Matrix&);
267
268    std::vector<DataWeight> vec_;
269    size_t columns_;
270
271  };
272
273  /**
274     \brief Exchange all elements.
275
276     Takes constant time.
277
278     \see MatrixWeighted::swap(MatrixWeighted&)
279
280     \relates MatrixWeighted
281  */
282  void swap(MatrixWeighted&, MatrixWeighted&);
283
284}}} // of namespace utility, yat, and theplu
285
286#endif
Note: See TracBrowser for help on using the repository browser.