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

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

working on #363

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1#ifndef _theplu_yat_utility_matrix_weighted_
2#define _theplu_yat_utility_matrix_weighted_
3
4// $Id: MatrixWeighted.h 1380 2008-07-16 22:36:23Z 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://trac.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 2 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 this program; if not, write to the Free Software
26  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27  02111-1307, USA.
28*/
29
30#include "DataWeight.h"
31#include "StrideIterator.h"
32
33#include <vector>
34
35namespace theplu {
36namespace yat {
37namespace utility {
38
39  ///
40  /// @brief Weighted Matrix
41  ///
42  class MatrixWeighted
43  {
44  public:
45    /**
46       Mutable iterator that iterates over all elements
47     */
48    typedef StrideIterator<std::vector<DataWeight>::iterator> iterator;
49
50    /**
51       Read-only iterator that iterates over all elements
52     */
53    typedef StrideIterator<std::vector<DataWeight>::const_iterator> 
54    const_iterator;
55
56    /**
57       Mutable iterator that iterates over one column
58     */
59    typedef StrideIterator<std::vector<DataWeight>::iterator> column_iterator;
60
61    /**
62       Read-only iterator that iterates over one column
63     */
64    typedef StrideIterator<std::vector<DataWeight>::const_iterator> 
65    const_column_iterator;
66
67    /**
68       Mutable iterator that iterates over one row
69     */
70    typedef StrideIterator<std::vector<DataWeight>::iterator> row_iterator;
71
72    /**
73       Read-only iterator that iterates over one row
74     */
75    typedef StrideIterator<std::vector<DataWeight>::const_iterator> 
76    const_row_iterator;
77
78    /**
79       @brief The default constructor.
80   
81       This contructor does not initialize underlying (essential)
82       structures.
83    */
84    MatrixWeighted(void);
85
86    /**
87       \brief Constructor allocating memory space for \a r times \a c
88       elements, and sets all elements to
89       DataWeight(\a init_value, \a init_weight)
90    */
91    MatrixWeighted(const size_t& r, const size_t& c, double init_value=0,
92                 double init_weight=1.0);
93
94    /**
95       \brief The copy constructor.
96    */
97    MatrixWeighted(const MatrixWeighted&);
98
99    /**
100       \brief The istream constructor.
101
102       Constructor is reading data values using corresponding
103       constructor in Matrix class. The weights are calculate dusing
104       the nan function.
105    */
106    explicit MatrixWeighted(std::istream &, char sep='\0'); 
107
108    /**
109       Iterator iterates along a row. When end of row is reached it
110       jumps to beginning of next row.
111
112       \return iterator pointing to upper-left element.
113     */
114    iterator begin(void);
115
116    /**
117       Iterator iterates along a row. When end of row is reached it
118       jumps to beginning of next row.
119
120       \return const_iterator pointing to upper-left element.
121     */
122    const_iterator begin(void) const;
123
124    /**
125       Iterator iterates along a column.
126
127       \return iterator pointing to first element of column \a i.
128     */
129    iterator begin_column(size_t i);
130
131    /**
132       Iterator iterates along a column.
133
134       \return const_iterator pointing to first element of column \a i.
135     */
136    const_iterator begin_column(size_t i) const;
137
138    /**
139       Iterator iterates along a row.
140
141       \return iterator pointing to first element of row \a i.
142     */
143    iterator begin_row(size_t i);
144
145    /**
146       Iterator iterates along a row.
147
148       \return const_iterator pointing to first element of row \a i.
149     */
150    const_iterator begin_row(size_t i) const;
151
152    /**
153       \return The number of columns in the matrix.
154    */
155    size_t columns(void) const;
156
157    /**
158       \return iterator pointing to end of matrix
159     */
160    iterator end(void);
161
162    /**
163       \return const_iterator pointing to end of matrix
164     */
165    const_iterator end(void) const;
166
167    /**
168       \return iterator pointing to end of column \a i
169     */
170    iterator end_column(size_t i);
171
172    /**
173       \return const_iterator pointing to end of column \a i
174     */
175    const_iterator end_column(size_t i) const;
176
177    /**
178       \return iterator pointing to end of row \a i
179     */
180    iterator end_row(size_t i);
181
182    /**
183       \return const_iterator pointing to end of row \a i
184     */
185    const_iterator end_row(size_t i) const;
186
187    /**
188       \brief Resize Matrix
189
190       \note this function may invalidate iterators.
191    */
192    void resize(size_t, size_t);
193
194    /**
195       \return The number of rows in the matrix.
196    */
197    size_t rows(void) const;
198
199    /**
200       \brief Transpose the matrix.
201
202       \note This function may invalidate iterators.
203    */
204    void transpose(void);
205
206    /**
207       \brief Element access operator.
208
209       \return Reference to the element position (\a row, \a column).
210    */
211    DataWeight& operator()(size_t row,size_t column);
212
213    /**
214       \brief Element access operator.
215
216       \return Const reference to the element position (\a row, \a
217       column).
218    */
219    const DataWeight& operator()(size_t row,size_t column) const;
220
221
222    /**
223       \brief The assignment operator.
224
225       \return A const reference to the resulting Matrix.
226    */
227    const MatrixWeighted& operator=(const MatrixWeighted& other);
228
229  private:
230    std::vector<DataWeight> vec_;
231    size_t columns_;
232
233  };
234
235  /**
236     \brief Exchange all elements.
237
238     Takes constant time.
239
240     \throw std::runtime_error if the two matrices disagree in size.
241  */
242  void swap(MatrixWeighted&, MatrixWeighted&);
243
244}}} // of namespace utility, yat, and theplu
245
246#endif
Note: See TracBrowser for help on using the repository browser.