All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Matrix.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 
34 /**
35  \file Matrix.hpp
36  \brief Header of ...
37 
38  \author Jean-Christophe FABRE <fabrejc@supagro.inra.fr>
39  */
40 
41 
42 #ifndef __MATRIX_HPP___
43 #define __MATRIX_HPP___
44 
45 
46 
47 #include <iostream>
48 #include <openfluid/dllexport.hpp>
50 #include <boost/multi_array.hpp>
51 
52 namespace openfluid { namespace core {
53 
54 
55 /**
56  Template class for matrix data
57 */
58 template <class T>
60 {
61  protected :
62 
63  boost::multi_array<T, 2> m_Data;
64 
65  unsigned long m_ColsNbr;
66 
67  unsigned long m_RowsNbr;
68 
69 
70  public :
71 
72  /**
73  Default constructor, creates an empty Matrix
74  */
75  Matrix();
76 
77  /**
78  Copy constructor
79  */
80  Matrix(const Matrix &Matrix);
81 
82  /**
83  Constructor, creates a Matrix containing Size elements
84  */
85  Matrix(unsigned long ColsNbr,unsigned long RowsNbr);
86 
87  /**
88  Constructor, creates a Matrix containing Size elements, initialized with value InitValue
89  */
90  Matrix(unsigned long ColsNbr,unsigned long RowsNbr, T InitValue);
91 
92  /**
93  Destructor
94  */
95  virtual ~Matrix() {};
96 
97  /**
98  Returns the number of columns of the Matrix
99  @return number of columns
100  */
101  inline unsigned long getColsNbr() const { return m_ColsNbr; };
102 
103  /**
104  Returns the number of rows of the Matrix
105  @return number of rows
106  */
107  inline unsigned long getRowsNbr() const { return m_RowsNbr; };
108 
109  /**
110  Returns the full size of the Matrix (number of columns x number of rows)
111  @return size of the Matrix
112  */
113  inline unsigned long getSize() const { return (m_ColsNbr * m_RowsNbr); };
114 
115  /**
116  Returns the full size of the Matrix
117  */
118  inline unsigned long size() const { return (m_ColsNbr * m_RowsNbr); };
119 
120 
121  /**
122  Returns a pointer to the content of the Matrix (like C arrays)
123  */
124  T* getData() const { return (T*)(m_Data.data()); };
125 
126  /**
127  Sets data from a pointer to a content (like C arrays)
128  */
129  void setData(T* Data);
130 
131  /**
132  Returns the element of the Matrix for index Index
133  */
134  T getElement(unsigned long ColIndex, unsigned long RowIndex) const;
135 
136  /**
137  Returns the element of the Matrix for index Index
138  */
139  inline T at(unsigned long ColIndex, unsigned long RowIndex) const { return getElement(ColIndex,RowIndex); };
140 
141  /**
142  Returns the element of the Matrix for index Index
143  */
144  inline T get(unsigned long ColIndex, unsigned long RowIndex) const { return getElement(ColIndex,RowIndex); };
145 
146 
147  /**
148  Sets a new value for element at the given index
149  */
150  void setElement(unsigned long ColIndex, unsigned long RowIndex, T Element);
151 
152  /**
153  Sets a new value for element at the given index
154  */
155  inline void set(unsigned long ColIndex, unsigned long RowIndex, T Element) { setElement(ColIndex,RowIndex,Element); };
156 
157  /**
158  Allocation operator
159  */
160  Matrix<T>& operator = (const Matrix &A);
161 
162  /**
163  Fills the Matrix with given value
164  */
165  void fill(const T& Val);
166 
167  /**
168  Clears the Matrix (empty and size is 0)
169  */
170  void clear();
171 
172 };
173 
174 // =====================================================================
175 // =====================================================================
176 
177 template <class T>
179  m_Data(boost::extents[0][0],boost::fortran_storage_order()),
180  m_ColsNbr(0),m_RowsNbr(0)
181 {
182 }
183 
184 
185 // =====================================================================
186 // =====================================================================
187 
188 template <class T>
190  m_Data(boost::extents[A.m_ColsNbr][A.m_RowsNbr],boost::fortran_storage_order()),
191  m_ColsNbr(A.m_ColsNbr),m_RowsNbr(A.m_RowsNbr)
192 {
193  m_Data = A.m_Data;
194 }
195 
196 
197 // =====================================================================
198 // =====================================================================
199 
200 template <class T>
201 Matrix<T>::Matrix(unsigned long ColsNbr, unsigned long RowsNbr) :
202  m_Data(boost::extents[ColsNbr][RowsNbr],boost::fortran_storage_order()),
203  m_ColsNbr(ColsNbr),m_RowsNbr(RowsNbr)
204 {
205  fill(0);
206 }
207 
208 
209 // =====================================================================
210 // =====================================================================
211 
212 
213 template <class T>
214 Matrix<T>::Matrix(unsigned long ColsNbr, unsigned long RowsNbr, T InitValue) :
215  m_Data(boost::extents[ColsNbr][RowsNbr],boost::fortran_storage_order()),
216  m_ColsNbr(ColsNbr),m_RowsNbr(RowsNbr)
217 {
218  fill(InitValue);
219 }
220 
221 
222 // =====================================================================
223 // =====================================================================
224 
225 
226 template <class T>
227 void Matrix<T>::setData(T* Data)
228 {
229  for (unsigned long j=0; j < m_RowsNbr;j++)
230  {
231  for (unsigned long i=0; i < m_ColsNbr;i++)
232  {
233  m_Data[i][j] = Data[i+(j*m_ColsNbr)];
234  }
235  }
236 }
237 
238 
239 // =====================================================================
240 // =====================================================================
241 
242 
243 template <class T>
244 T Matrix<T>::getElement(unsigned long ColIndex, unsigned long RowIndex) const
245 {
246  if (ColIndex >= m_ColsNbr || RowIndex >= m_RowsNbr) throw openfluid::base::FrameworkException("Matrix::getElement","element access range error");
247  return m_Data[ColIndex][RowIndex];
248 }
249 
250 
251 // =====================================================================
252 // =====================================================================
253 
254 
255 template <class T>
256 void Matrix<T>::setElement(unsigned long ColIndex, unsigned long RowIndex, T Element)
257 {
258  if (ColIndex >= m_ColsNbr || RowIndex >= m_RowsNbr) throw openfluid::base::FrameworkException("Matrix::setElement","element access range error");
259  m_Data[ColIndex][RowIndex] = Element;
260 }
261 
262 
263 
264 // =====================================================================
265 // =====================================================================
266 
267 template <class T>
269 {
270 
271  if (this == &A) return *this; // in case somebody tries assign array to itself
272 
273  m_Data.resize(boost::extents[A.m_ColsNbr][A.m_RowsNbr]);
274  m_Data = A.m_Data;
275  m_ColsNbr = A.m_ColsNbr;
276  m_RowsNbr = A.m_RowsNbr;
277 
278  return *this;
279 }
280 
281 
282 // =====================================================================
283 // =====================================================================
284 
285 
286 template <class T>
287 void Matrix<T>::fill(const T& Val)
288 {
289  for (unsigned long i=0;i<m_ColsNbr;i++)
290  for (unsigned long j=0;j<m_RowsNbr;j++)
291  m_Data[i][j] = Val;
292 }
293 
294 
295 // =====================================================================
296 // =====================================================================
297 
298 template <class T>
300 {
301  m_Data.resize(boost::extents[0][0]);
302  m_ColsNbr=0;
303  m_RowsNbr=0;
304 }
305 
306 
307 
308 } }
309 
310 #endif /* __MATRIX_HPP___ */
Definition: Matrix.hpp:59
boost::multi_array< T, 2 > m_Data
Definition: Matrix.hpp:63
T at(unsigned long ColIndex, unsigned long RowIndex) const
Definition: Matrix.hpp:139
Matrix()
Definition: Matrix.hpp:178
void clear()
Definition: Matrix.hpp:299
T getElement(unsigned long ColIndex, unsigned long RowIndex) const
Definition: Matrix.hpp:244
Definition: FrameworkException.hpp:49
void set(unsigned long ColIndex, unsigned long RowIndex, T Element)
Definition: Matrix.hpp:155
unsigned long m_ColsNbr
Definition: Matrix.hpp:65
unsigned long m_RowsNbr
Definition: Matrix.hpp:67
unsigned long getSize() const
Definition: Matrix.hpp:113
void fill(const T &Val)
Definition: Matrix.hpp:287
unsigned long size() const
Definition: Matrix.hpp:118
void setData(T *Data)
Definition: Matrix.hpp:227
Matrix< T > & operator=(const Matrix &A)
Definition: Matrix.hpp:268
virtual ~Matrix()
Definition: Matrix.hpp:95
void setElement(unsigned long ColIndex, unsigned long RowIndex, T Element)
Definition: Matrix.hpp:256
unsigned long getColsNbr() const
Definition: Matrix.hpp:101
unsigned long getRowsNbr() const
Definition: Matrix.hpp:107
T * getData() const
Definition: Matrix.hpp:124
#define DLLEXPORT
Definition: dllexport.hpp:51