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 // Permission is hereby granted, free of charge, to any person obtaining a
12 // copy of this software and associated documentation files (the "Software"),
13 // to deal in the Software without restriction, including without limitation
14 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 // and/or sell copies of the Software, and to permit persons to whom the
16 // Software is furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included
19 // in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 // DEALINGS IN THE SOFTWARE.
28 //
29 ///////////////////////////////////////////////////////////////////////////////
30 
32 
33 namespace Nektar
34 {
35 
36 template <typename DataType> ConstMatrix<DataType>::~ConstMatrix()
37 {
38 }
39 
40 template <typename DataType>
41 typename boost::call_traits<DataType>::value_type ConstMatrix<
42  DataType>::operator()(unsigned int row, unsigned int column) const
43 {
44  ASSERTL2(row < GetRows(),
45  std::string("Row ") + std::to_string(row) +
46  std::string(" requested in a matrix with a maximum of ") +
47  std::to_string(GetRows()) + std::string(" rows"));
48  ASSERTL2(column < GetColumns(),
49  std::string("Column ") + std::to_string(column) +
50  std::string(" requested in a matrix with a maximum of ") +
51  std::to_string(GetColumns()) + std::string(" columns"));
52  return v_GetValue(row, column);
53 }
54 
55 template <typename DataType>
57 {
58  return v_GetStorageSize();
59 }
60 
61 template <typename DataType> unsigned int ConstMatrix<DataType>::GetRows() const
62 {
63  return GetTransposedRows(GetTransposeFlag());
64 }
65 
66 template <typename DataType>
67 unsigned int ConstMatrix<DataType>::GetTransposedRows(char transpose) const
68 {
69  if (transpose == 'N')
70  {
71  return m_size[0];
72  }
73  else
74  {
75  return m_size[1];
76  }
77 }
78 
79 template <typename DataType>
81 {
82  return GetTransposedColumns(GetTransposeFlag());
83 }
84 
85 template <typename DataType>
86 unsigned int ConstMatrix<DataType>::GetTransposedColumns(char transpose) const
87 {
88  if (transpose == 'N')
89  {
90  return m_size[1];
91  }
92  else
93  {
94  return m_size[0];
95  }
96 }
97 
98 template <typename DataType>
99 const unsigned int *ConstMatrix<DataType>::GetSize() const
100 {
101  return m_size;
102 }
103 
104 template <typename DataType> void ConstMatrix<DataType>::Transpose()
105 {
106  if (m_transpose == 'N')
107  {
108  m_transpose = 'T';
109  }
110  else
111  {
112  m_transpose = 'N';
113  }
114  v_Transpose();
115 }
116 
117 template <typename DataType>
119 {
120  return v_GetTransposeFlag();
121 }
122 
123 template <typename DataType>
125  MatrixStorage type, unsigned int row, unsigned int col,
126  unsigned int numRows, unsigned int numColumns, const char transpose,
127  unsigned int numSubDiags, unsigned int numSuperDiags)
128 {
129  if (transpose == 'T')
130  {
131  std::swap(row, col);
132  }
133  switch (type)
134  {
135  case eFULL:
136  return FullMatrixFuncs::CalculateIndex(numRows, numColumns, row,
137  col);
138  break;
139  case eDIAGONAL:
140  return DiagonalMatrixFuncs::CalculateIndex(row, col);
141  break;
142  case eUPPER_TRIANGULAR:
144  break;
145  case eLOWER_TRIANGULAR:
146  return LowerTriangularMatrixFuncs::CalculateIndex(numColumns, row,
147  col);
148  break;
149  case eSYMMETRIC:
151  return SymmetricMatrixFuncs::CalculateIndex(row, col);
152  break;
153  case eBANDED:
155  numRows, numColumns, 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>
183  MatrixStorage type, unsigned int rows, unsigned int columns,
184  unsigned int subDiags, unsigned int superDiags)
185 {
186  switch (type)
187  {
188  case eFULL:
189  return FullMatrixFuncs::GetRequiredStorageSize(rows, columns);
190  break;
191  case eDIAGONAL:
192  return DiagonalMatrixFuncs::GetRequiredStorageSize(rows, columns);
193  break;
194  case eUPPER_TRIANGULAR:
196  columns);
197  break;
198  case eLOWER_TRIANGULAR:
200  columns);
201  break;
202  case eSYMMETRIC:
204  return SymmetricMatrixFuncs::GetRequiredStorageSize(rows, columns);
205  break;
206  case eBANDED:
208  rows, columns, subDiags, superDiags);
209  break;
210  case eSYMMETRIC_BANDED:
212  {
213  ASSERTL1(subDiags == superDiags,
214  std::string("Number of sub- and superdiagonals should ") +
215  std::string("be equal for a symmetric banded matrix"));
217  rows, columns, superDiags);
218  }
219  break;
221  NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
222  break;
224  NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
225  break;
226 
227  default:
228  NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
229  }
230 
231  return 0;
232 }
233 
234 template <typename DataType>
235 ConstMatrix<DataType>::ConstMatrix(unsigned int rows, unsigned int columns,
236  MatrixStorage policy)
237  : m_size(), m_transpose('N'), m_storageType(policy)
238 {
239  m_size[0] = rows;
240  m_size[1] = columns;
241 }
242 
243 template <typename DataType>
245  : m_size(), m_transpose(rhs.m_transpose), m_storageType(rhs.m_storageType)
246 {
247  m_size[0] = rhs.m_size[0];
248  m_size[1] = rhs.m_size[1];
249 }
250 
251 template <typename DataType>
253  const ConstMatrix<DataType> &rhs)
254 {
255  m_size[0] = rhs.m_size[0];
256  m_size[1] = rhs.m_size[1];
257  m_transpose = rhs.m_transpose;
258  m_storageType = rhs.m_storageType;
259  return *this;
260 }
261 
262 template <typename DataType>
263 void ConstMatrix<DataType>::Resize(unsigned int rows, unsigned int columns)
264 {
265  m_size[0] = rows;
266  m_size[1] = columns;
267 }
268 
269 template <typename DataType>
271 {
272  m_transpose = newValue;
273 }
274 
275 template <typename DataType> void ConstMatrix<DataType>::v_Transpose()
276 {
277 }
278 
279 template <typename DataType>
281 {
282  return m_transpose;
283 }
284 
285 template <typename DataType> Matrix<DataType>::~Matrix()
286 {
287 }
288 
289 template <typename DataType>
291  unsigned int row, unsigned int column,
292  typename boost::call_traits<DataType>::const_reference d)
293 {
294  ASSERTL2(row < this->GetRows(),
295  std::string("Row ") + std::to_string(row) +
296  std::string(" requested in a matrix with a maximum of ") +
297  std::to_string(this->GetRows()) + std::string(" rows"));
298  ASSERTL2(column < this->GetColumns(),
299  std::string("Column ") + std::to_string(column) +
300  std::string(" requested in a matrix with a maximum of ") +
301  std::to_string(this->GetColumns()) + std::string(" columns"));
302  v_SetValue(row, column, d);
303 }
304 
305 template <typename DataType>
306 Matrix<DataType>::Matrix(unsigned int rows, unsigned int columns,
307  MatrixStorage policy)
308  : ConstMatrix<DataType>(rows, columns, policy)
309 {
310 }
311 
312 template <typename DataType>
314  : ConstMatrix<DataType>(rhs)
315 {
316 }
317 
318 template <typename DataType>
320 {
322  return *this;
323 }
324 
325 template <typename DataType>
327 {
329  return *this;
330 }
331 
334 
336 template LIB_UTILITIES_EXPORT class Matrix<int>;
337 
340 
342 template LIB_UTILITIES_EXPORT class Matrix<float>;
343 } // namespace Nektar
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:249
#define ASSERTL2(condition, msg)
Assert Level 2 – Debugging which is used FULLDEBUG compilation mode. This level assert is designed to...
Definition: ErrorUtil.hpp:272
#define LIB_UTILITIES_EXPORT
void SetTransposeFlag(char newValue)
Definition: MatrixBase.cpp:270
unsigned int m_size[2]
Definition: MatrixBase.hpp:127
ConstMatrix< DataType > & operator=(const ConstMatrix< DataType > &rhs)
Definition: MatrixBase.cpp:252
unsigned int GetStorageSize() const
Definition: MatrixBase.cpp:56
ConstMatrix(unsigned int rows, unsigned int columns, MatrixStorage policy=eFULL)
Definition: MatrixBase.cpp:235
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:124
unsigned int GetTransposedColumns(char transpose) const
Definition: MatrixBase.cpp:86
virtual char v_GetTransposeFlag() const
Definition: MatrixBase.cpp:280
unsigned int GetRows() const
Definition: MatrixBase.cpp:61
virtual void v_Transpose()
Definition: MatrixBase.cpp:275
MatrixStorage m_storageType
Definition: MatrixBase.hpp:129
virtual ~ConstMatrix()
Definition: MatrixBase.cpp:36
unsigned int GetTransposedRows(char transpose) const
Definition: MatrixBase.cpp:67
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:263
static unsigned int GetRequiredStorageSize(MatrixStorage type, unsigned int rows, unsigned int columns, unsigned int subDiags=0, unsigned int superDiags=0)
Definition: MatrixBase.cpp:182
char GetTransposeFlag() const
Definition: MatrixBase.cpp:118
unsigned int GetColumns() const
Definition: MatrixBase.cpp:80
const unsigned int * GetSize() const
Definition: MatrixBase.cpp:99
void SetValue(unsigned int row, unsigned int column, typename boost::call_traits< DataType >::const_reference d)
Definition: MatrixBase.cpp:290
Matrix< DataType > & operator=(const Matrix< DataType > &rhs)
Definition: MatrixBase.cpp:319
Matrix(unsigned int rows, unsigned int columns, MatrixStorage policy=eFULL)
Definition: MatrixBase.cpp:306
virtual ~Matrix()
Definition: MatrixBase.cpp:285
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
@ eLOWER_TRIANGULAR_BANDED
@ ePOSITIVE_DEFINITE_SYMMETRIC_BANDED
@ ePOSITIVE_DEFINITE_SYMMETRIC
@ eUPPER_TRIANGULAR_BANDED
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:77
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:44
static unsigned int GetRequiredStorageSize(unsigned int rows, unsigned int columns)
static unsigned int CalculateIndex(unsigned int row, unsigned int col)
static unsigned int GetRequiredStorageSize(unsigned int rows, unsigned int columns)
static unsigned int CalculateIndex(unsigned int totalRows, unsigned int totalColumns, unsigned int curRow, unsigned int curColumn)
static unsigned int CalculateIndex(unsigned int totalColumns, unsigned int curRow, unsigned int curColumn)
static unsigned int CalculateIndex(unsigned int curRow, unsigned int curColumn, unsigned int nSuperDiags)
static unsigned int GetRequiredStorageSize(unsigned int rows, unsigned int columns, unsigned int nSubSuperDiags)
static unsigned int GetRequiredStorageSize(unsigned int rows, unsigned int columns)
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 curRow, unsigned int curColumn)