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

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

refs #448

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