Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00057 #ifndef __MATRIX_HPP___
00058 #define __MATRIX_HPP___
00059
00060
00061
00062 #include <iostream>
00063 #include <openfluid/dllexport.hpp>
00064 #include <openfluid/base/OFException.hpp>
00065 #include <boost/multi_array.hpp>
00066
00067 namespace openfluid { namespace core {
00068
00069
00073 template <class T>
00074 class DLLEXPORT Matrix
00075 {
00076 protected :
00077
00078 boost::multi_array<T, 2> m_Data;
00079
00080 unsigned long m_ColsNbr;
00081
00082 unsigned long m_RowsNbr;
00083
00084
00085 public :
00086
00090 Matrix();
00091
00095 Matrix(const Matrix &Matrix);
00096
00100 Matrix(unsigned long ColsNbr,unsigned long RowsNbr);
00101
00105 Matrix(unsigned long ColsNbr,unsigned long RowsNbr, T InitValue);
00106
00110 virtual ~Matrix() {};
00111
00116 inline unsigned long getColsNbr() const { return m_ColsNbr; };
00117
00122 inline unsigned long getRowsNbr() const { return m_RowsNbr; };
00123
00128 inline unsigned long getSize() const { return (m_ColsNbr * m_RowsNbr); };
00129
00133 inline unsigned long size() const { return (m_ColsNbr * m_RowsNbr); };
00134
00135
00139 T* getData() const { return (T*)(m_Data.data()); };
00140
00144 void setData(T* Data);
00145
00149 T getElement(unsigned long ColIndex, unsigned long RowIndex) const;
00150
00154 inline T at(unsigned long ColIndex, unsigned long RowIndex) const { return getElement(ColIndex,RowIndex); };
00155
00159 inline T get(unsigned long ColIndex, unsigned long RowIndex) const { return getElement(ColIndex,RowIndex); };
00160
00161
00165 void setElement(unsigned long ColIndex, unsigned long RowIndex, T Element);
00166
00170 inline void set(unsigned long ColIndex, unsigned long RowIndex, T Element) { setElement(ColIndex,RowIndex,Element); };
00171
00175 Matrix<T>& operator = (const Matrix &A);
00176
00180 void fill(const T& Val);
00181
00185 void clear();
00186
00187 };
00188
00189
00190
00191
00192 template <class T>
00193 Matrix<T>::Matrix():
00194 m_Data(boost::extents[0][0],boost::fortran_storage_order()),
00195 m_ColsNbr(0),m_RowsNbr(0)
00196 {
00197 }
00198
00199
00200
00201
00202
00203 template <class T>
00204 Matrix<T>::Matrix(const Matrix &A):
00205 m_Data(boost::extents[A.m_ColsNbr][A.m_RowsNbr],boost::fortran_storage_order()),
00206 m_ColsNbr(A.m_ColsNbr),m_RowsNbr(A.m_RowsNbr)
00207 {
00208 m_Data = A.m_Data;
00209 }
00210
00211
00212
00213
00214
00215 template <class T>
00216 Matrix<T>::Matrix(unsigned long ColsNbr, unsigned long RowsNbr) :
00217 m_Data(boost::extents[ColsNbr][RowsNbr],boost::fortran_storage_order()),
00218 m_ColsNbr(ColsNbr),m_RowsNbr(RowsNbr)
00219 {
00220 fill(0);
00221 }
00222
00223
00224
00225
00226
00227
00228 template <class T>
00229 Matrix<T>::Matrix(unsigned long ColsNbr, unsigned long RowsNbr, T InitValue) :
00230 m_Data(boost::extents[ColsNbr][RowsNbr],boost::fortran_storage_order()),
00231 m_ColsNbr(ColsNbr),m_RowsNbr(RowsNbr)
00232 {
00233 fill(InitValue);
00234 }
00235
00236
00237
00238
00239
00240
00241 template <class T>
00242 void Matrix<T>::setData(T* Data)
00243 {
00244 for (unsigned long j=0; j < m_RowsNbr;j++)
00245 {
00246 for (unsigned long i=0; i < m_ColsNbr;i++)
00247 {
00248 m_Data[i][j] = Data[i+(j*m_ColsNbr)];
00249 }
00250 }
00251 }
00252
00253
00254
00255
00256
00257
00258 template <class T>
00259 T Matrix<T>::getElement(unsigned long ColIndex, unsigned long RowIndex) const
00260 {
00261 if (ColIndex >= m_ColsNbr || RowIndex >= m_RowsNbr) throw openfluid::base::OFException("OpenFLUID framework","Matrix::getElement","element access range error");
00262 return m_Data[ColIndex][RowIndex];
00263 }
00264
00265
00266
00267
00268
00269
00270 template <class T>
00271 void Matrix<T>::setElement(unsigned long ColIndex, unsigned long RowIndex, T Element)
00272 {
00273 if (ColIndex >= m_ColsNbr || RowIndex >= m_RowsNbr) throw openfluid::base::OFException("OpenFLUID framework","Matrix::setElement","element access range error");
00274 m_Data[ColIndex][RowIndex] = Element;
00275 }
00276
00277
00278
00279
00280
00281
00282 template <class T>
00283 Matrix<T>& Matrix<T>::operator=(const Matrix &A)
00284 {
00285
00286 if (this == &A) return *this;
00287
00288 m_Data.resize(boost::extents[A.m_ColsNbr][A.m_RowsNbr]);
00289 m_Data = A.m_Data;
00290 m_ColsNbr = A.m_ColsNbr;
00291 m_RowsNbr = A.m_RowsNbr;
00292
00293 return *this;
00294 }
00295
00296
00297
00298
00299
00300
00301 template <class T>
00302 void Matrix<T>::fill(const T& Val)
00303 {
00304 for (unsigned long i=0;i<m_ColsNbr;i++)
00305 for (unsigned long j=0;j<m_RowsNbr;j++)
00306 m_Data[i][j] = Val;
00307 }
00308
00309
00310
00311
00312
00313 template <class T>
00314 void Matrix<T>::clear()
00315 {
00316 m_Data.resize(boost::extents[0][0]);
00317 m_ColsNbr=0;
00318 m_RowsNbr=0;
00319 }
00320
00321
00322
00323 } }
00324
00325 #endif