Nektar++
MatrixBase.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // For more information, please see: http://www.nektar.info
4 //
5 // The MIT License
6 //
7 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
8 // Department of Aeronautics, Imperial College London (UK), and Scientific
9 // Computing and Imaging Institute, University of Utah (USA).
10 //
11 // License for the specific language governing rights and limitations under
12 // Permission is hereby granted, free of charge, to any person obtaining a
13 // copy of this software and associated documentation files (the "Software"),
14 // to deal in the Software without restriction, including without limitation
15 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 // and/or sell copies of the Software, and to permit persons to whom the
17 // Software is furnished to do so, subject to the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be included
20 // in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 // DEALINGS IN THE SOFTWARE.
29 //
30 ///////////////////////////////////////////////////////////////////////////////
31 
33 
34 namespace Nektar
35 {
36 
37  template<typename DataType>
39 
40  template<typename DataType>
41  typename boost::call_traits<DataType>::value_type ConstMatrix<DataType>::operator()(unsigned int row, unsigned int column) const
42  {
43  ASSERTL2(row < GetRows(), std::string("Row ") + boost::lexical_cast<std::string>(row) +
44  std::string(" requested in a matrix with a maximum of ") + boost::lexical_cast<std::string>(GetRows()) +
45  std::string(" rows"));
46  ASSERTL2(column < GetColumns(), std::string("Column ") + boost::lexical_cast<std::string>(column) +
47  std::string(" requested in a matrix with a maximum of ") + boost::lexical_cast<std::string>(GetColumns()) +
48  std::string(" columns"));
49  return v_GetValue(row, column);
50  }
51 
52  template<typename DataType>
54  {
55  return v_GetStorageSize();
56  }
57 
58  template<typename DataType>
60  {
61  return v_GetStorageType();
62  }
63 
64  template<typename DataType>
65  unsigned int ConstMatrix<DataType>::GetRows() const
66  {
67  return GetTransposedRows(GetTransposeFlag());
68  }
69 
70  template<typename DataType>
71  unsigned int ConstMatrix<DataType>::GetTransposedRows(char transpose) const
72  {
73  if( transpose == 'N' )
74  {
75  return m_size[0];
76  }
77  else
78  {
79  return m_size[1];
80  }
81  }
82 
83  template<typename DataType>
84  unsigned int ConstMatrix<DataType>::GetColumns() const
85  {
86  return GetTransposedColumns(GetTransposeFlag());
87  }
88 
89  template<typename DataType>
90  unsigned int ConstMatrix<DataType>::GetTransposedColumns(char transpose) const
91  {
92  if( transpose == 'N' )
93  {
94  return m_size[1];
95  }
96  else
97  {
98  return m_size[0];
99  }
100  }
101 
102  template<typename DataType>
103  const unsigned int* ConstMatrix<DataType>::GetSize() const { return m_size; }
104 
105  template<typename DataType>
107  {
108  if( m_transpose == 'N' )
109  {
110  m_transpose = 'T';
111  }
112  else
113  {
114  m_transpose = 'N';
115  }
116  v_Transpose();
117  }
118 
119  template<typename DataType>
121  {
122  return v_GetTransposeFlag();
123  }
124 
125  template<typename DataType>
127  unsigned int row, unsigned int col,
128  unsigned int numRows, unsigned int numColumns, const char transpose,
129  unsigned int numSubDiags, unsigned int numSuperDiags )
130  {
131  if(transpose == 'T' )
132  {
133  std::swap(row, col);
134  }
135  switch(type)
136  {
137  case eFULL:
138  return FullMatrixFuncs::CalculateIndex(numRows, numColumns, row, col);
139  break;
140  case eDIAGONAL:
141  return DiagonalMatrixFuncs::CalculateIndex(row, col);
142  break;
143  case eUPPER_TRIANGULAR:
145  break;
146  case eLOWER_TRIANGULAR:
147  return LowerTriangularMatrixFuncs::CalculateIndex(numColumns, row, col);
148  break;
149  case eSYMMETRIC:
151  return SymmetricMatrixFuncs::CalculateIndex(row, col);
152  break;
153  case eBANDED:
154  return BandedMatrixFuncs::CalculateIndex(numRows, numColumns,
155  row, col, numSubDiags, numSuperDiags);
156  break;
157  case eSYMMETRIC_BANDED:
159  {
160  ASSERTL1(numSubDiags==numSuperDiags,
161  std::string("Number of sub- and superdiagonals should ") +
162  std::string("be equal for a symmetric banded matrix"));
164  numSuperDiags);
165  }
166  break;
168  NEKERROR(ErrorUtil::efatal, "Not yet implemented.");
169  break;
171  NEKERROR(ErrorUtil::efatal, "Not yet implemented.");
172  break;
173 
174  default:
175  NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
176  }
177 
178  return std::numeric_limits<unsigned int>::max();
179  }
180 
181  template<typename DataType>
182  unsigned int ConstMatrix<DataType>::GetRequiredStorageSize(MatrixStorage type, unsigned int rows,
183  unsigned int columns, unsigned int subDiags, unsigned int superDiags)
184  {
185  switch(type)
186  {
187  case eFULL:
188  return FullMatrixFuncs::GetRequiredStorageSize(rows, columns);
189  break;
190  case eDIAGONAL:
191  return DiagonalMatrixFuncs::GetRequiredStorageSize(rows, columns);
192  break;
193  case eUPPER_TRIANGULAR:
195  break;
196  case eLOWER_TRIANGULAR:
198  break;
199  case eSYMMETRIC:
201  return SymmetricMatrixFuncs::GetRequiredStorageSize(rows, columns);
202  break;
203  case eBANDED:
204  return BandedMatrixFuncs::GetRequiredStorageSize(rows, columns,
205  subDiags, superDiags);
206  break;
207  case eSYMMETRIC_BANDED:
209  {
210  ASSERTL1(subDiags==superDiags,
211  std::string("Number of sub- and superdiagonals should ") +
212  std::string("be equal for a symmetric banded matrix"));
214  superDiags);
215  }
216  break;
218  NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
219  break;
221  NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
222  break;
223 
224  default:
225  NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
226  }
227 
228  return 0;
229  }
230 
231  template<typename DataType>
232  ConstMatrix<DataType>::ConstMatrix(unsigned int rows, unsigned int columns) :
233  m_size(),
234  m_transpose('N')
235  {
236  m_size[0] = rows;
237  m_size[1] = columns;
238  }
239 
240  template<typename DataType>
242  m_size(),
243  m_transpose(rhs.m_transpose)
244  {
245  m_size[0] = rhs.m_size[0];
246  m_size[1] = rhs.m_size[1];
247  }
248 
249  template<typename DataType>
251  {
252  m_size[0] = rhs.m_size[0];
253  m_size[1] = rhs.m_size[1];
254  m_transpose = rhs.m_transpose;
255  return *this;
256  }
257 
258 
259  template<typename DataType>
260  void ConstMatrix<DataType>::Resize(unsigned int rows, unsigned int columns)
261  {
262  m_size[0] = rows;
263  m_size[1] = columns;
264  }
265 
266  template<typename DataType>
268  {
269  m_transpose = newValue;
270  }
271 
272  template<typename DataType>
273  char ConstMatrix<DataType>::GetRawTransposeFlag() const { return m_transpose; }
274 
275  template<typename DataType>
277 
278  template<typename DataType>
279  char ConstMatrix<DataType>::v_GetTransposeFlag() const { return m_transpose; }
280 
281 
282  template<typename DataType>
284 
285  template<typename DataType>
286  void Matrix<DataType>::SetValue(unsigned int row, unsigned int column, typename boost::call_traits<DataType>::const_reference d)
287  {
288  ASSERTL2(row < this->GetRows(), std::string("Row ") + boost::lexical_cast<std::string>(row) +
289  std::string(" requested in a matrix with a maximum of ") + boost::lexical_cast<std::string>(this->GetRows()) +
290  std::string(" rows"));
291  ASSERTL2(column < this->GetColumns(), std::string("Column ") + boost::lexical_cast<std::string>(column) +
292  std::string(" requested in a matrix with a maximum of ") + boost::lexical_cast<std::string>(this->GetColumns()) +
293  std::string(" columns"));
294  v_SetValue(row, column, d);
295  }
296 
297  template<typename DataType>
298  Matrix<DataType>::Matrix(unsigned int rows, unsigned int columns) :
299  ConstMatrix<DataType>(rows, columns)
300  {
301  }
302 
303  template<typename DataType>
305  ConstMatrix<DataType>(rhs)
306  {
307  }
308 
309  template<typename DataType>
311  {
313  return *this;
314  }
315 
316  template<typename DataType>
318  {
320  return *this;
321  }
322 
323 
326 
327  template LIB_UTILITIES_EXPORT class ConstMatrix<int>;
328  template LIB_UTILITIES_EXPORT class Matrix<int>;
329 
332 
334  template LIB_UTILITIES_EXPORT class Matrix<float>;
335 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:158
void Resize(unsigned int rows, unsigned int columns)
Resets the rows and columns in the array. This method does not update the data storage to match the n...
Definition: MatrixBase.cpp:260
static unsigned int CalculateIndex(unsigned int totalRows, unsigned int totalColumns, unsigned int row, unsigned int column, unsigned int sub, unsigned int super)
Definition: MatrixFuncs.cpp:70
static unsigned int CalculateIndex(unsigned int curRow, unsigned int curColumn, unsigned int nSuperDiags)
static unsigned int CalculateIndex(unsigned int curRow, unsigned int curColumn)
unsigned int m_size[2]
Definition: MatrixBase.hpp:115
const unsigned int * GetSize() const
Definition: MatrixBase.cpp:103
static unsigned int GetRequiredStorageSize(unsigned int rows, unsigned int columns)
static unsigned int CalculateIndex(unsigned int totalColumns, unsigned int curRow, unsigned int curColumn)
static unsigned int GetRequiredStorageSize(unsigned int rows, unsigned int columns)
char GetTransposeFlag() const
Definition: MatrixBase.cpp:120
virtual void v_Transpose()
Definition: MatrixBase.cpp:276
unsigned int GetColumns() const
Definition: MatrixBase.cpp:84
unsigned int GetStorageSize() const
Definition: MatrixBase.cpp:53
virtual ~ConstMatrix()
Definition: MatrixBase.cpp:38
Matrix< DataType > & operator=(const Matrix< DataType > &rhs)
Definition: MatrixBase.cpp:310
static unsigned int CalculateIndex(MatrixStorage type, unsigned int row, unsigned int col, unsigned int numRows, unsigned int numColumns, const char transpose= 'N', unsigned int numSubDiags=0, unsigned int numSuperDiags=0)
Definition: MatrixBase.cpp:126
#define LIB_UTILITIES_EXPORT
virtual char v_GetTransposeFlag() const
Definition: MatrixBase.cpp:279
MatrixStorage GetStorageType() const
Definition: MatrixBase.cpp:59
static unsigned int GetRequiredStorageSize(unsigned int rows, unsigned int columns, unsigned int nSubSuperDiags)
char GetRawTransposeFlag() const
Definition: MatrixBase.cpp:273
unsigned int GetTransposedColumns(char transpose) const
Definition: MatrixBase.cpp:90
ConstMatrix(unsigned int rows, unsigned int columns)
Definition: MatrixBase.cpp:232
void SetTransposeFlag(char newValue)
Definition: MatrixBase.cpp:267
Matrix(unsigned int rows, unsigned int columns)
Definition: MatrixBase.cpp:298
static unsigned int CalculateIndex(unsigned int curRow, unsigned int curColumn)
static unsigned int GetRequiredStorageSize(unsigned int rows, unsigned int columns)
static unsigned int CalculateIndex(unsigned int row, unsigned int col)
unsigned int GetRows() const
Definition: MatrixBase.cpp:65
ConstMatrix< DataType > & operator=(const ConstMatrix< DataType > &rhs)
Definition: MatrixBase.cpp:250
#define ASSERTL2(condition, msg)
Assert Level 2 – Debugging which is used FULLDEBUG compilation mode. This level assert is designed t...
Definition: ErrorUtil.hpp:213
static unsigned int GetRequiredStorageSize(MatrixStorage type, unsigned int rows, unsigned int columns, unsigned int subDiags=0, unsigned int superDiags=0)
Definition: MatrixBase.cpp:182
static unsigned int CalculateIndex(unsigned int totalRows, unsigned int totalColumns, unsigned int curRow, unsigned int curColumn)
void SetValue(unsigned int row, unsigned int column, typename boost::call_traits< DataType >::const_reference d)
Definition: MatrixBase.cpp:286
boost::call_traits< DataType >::value_type operator()(unsigned int row, unsigned int column) const
Definition: MatrixBase.cpp:41
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
Definition: ErrorUtil.hpp:191
unsigned int GetTransposedRows(char transpose) const
Definition: MatrixBase.cpp:71
virtual ~Matrix()
Definition: MatrixBase.cpp:283
static unsigned int GetRequiredStorageSize(unsigned int totalRows, unsigned int totalColumns, unsigned int subDiags, unsigned int superDiags)
Calculates and returns the storage size required.
Definition: MatrixFuncs.cpp:43