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>
38 
39  template<typename DataType>
40  typename boost::call_traits<DataType>::value_type ConstMatrix<DataType>::operator()(unsigned int row, unsigned int column) const
41  {
42  ASSERTL2(row < GetRows(), std::string("Row ") + std::to_string(row) +
43  std::string(" requested in a matrix with a maximum of ") + std::to_string(GetRows()) +
44  std::string(" rows"));
45  ASSERTL2(column < GetColumns(), std::string("Column ") + std::to_string(column) +
46  std::string(" requested in a matrix with a maximum of ") + std::to_string(GetColumns()) +
47  std::string(" columns"));
48  return v_GetValue(row, column);
49  }
50 
51  template<typename DataType>
53  {
54  return v_GetStorageSize();
55  }
56 
57  template<typename DataType>
58  unsigned int ConstMatrix<DataType>::GetRows() const
59  {
60  return GetTransposedRows(GetTransposeFlag());
61  }
62 
63  template<typename DataType>
64  unsigned int ConstMatrix<DataType>::GetTransposedRows(char transpose) const
65  {
66  if( transpose == 'N' )
67  {
68  return m_size[0];
69  }
70  else
71  {
72  return m_size[1];
73  }
74  }
75 
76  template<typename DataType>
77  unsigned int ConstMatrix<DataType>::GetColumns() const
78  {
79  return GetTransposedColumns(GetTransposeFlag());
80  }
81 
82  template<typename DataType>
83  unsigned int ConstMatrix<DataType>::GetTransposedColumns(char transpose) const
84  {
85  if( transpose == 'N' )
86  {
87  return m_size[1];
88  }
89  else
90  {
91  return m_size[0];
92  }
93  }
94 
95  template<typename DataType>
96  const unsigned int* ConstMatrix<DataType>::GetSize() const { return m_size; }
97 
98  template<typename DataType>
100  {
101  if( m_transpose == 'N' )
102  {
103  m_transpose = 'T';
104  }
105  else
106  {
107  m_transpose = 'N';
108  }
109  v_Transpose();
110  }
111 
112  template<typename DataType>
114  {
115  return v_GetTransposeFlag();
116  }
117 
118  template<typename DataType>
120  unsigned int row, unsigned int col,
121  unsigned int numRows, unsigned int numColumns, const char transpose,
122  unsigned int numSubDiags, unsigned int numSuperDiags )
123  {
124  if(transpose == 'T' )
125  {
126  std::swap(row, col);
127  }
128  switch(type)
129  {
130  case eFULL:
131  return FullMatrixFuncs::CalculateIndex(numRows, numColumns, row, col);
132  break;
133  case eDIAGONAL:
134  return DiagonalMatrixFuncs::CalculateIndex(row, col);
135  break;
136  case eUPPER_TRIANGULAR:
138  break;
139  case eLOWER_TRIANGULAR:
140  return LowerTriangularMatrixFuncs::CalculateIndex(numColumns, row, col);
141  break;
142  case eSYMMETRIC:
144  return SymmetricMatrixFuncs::CalculateIndex(row, col);
145  break;
146  case eBANDED:
147  return BandedMatrixFuncs::CalculateIndex(numRows, numColumns,
148  row, col, numSubDiags, numSuperDiags);
149  break;
150  case eSYMMETRIC_BANDED:
152  {
153  ASSERTL1(numSubDiags==numSuperDiags,
154  std::string("Number of sub- and superdiagonals should ") +
155  std::string("be equal for a symmetric banded matrix"));
157  numSuperDiags);
158  }
159  break;
161  NEKERROR(ErrorUtil::efatal, "Not yet implemented.");
162  break;
164  NEKERROR(ErrorUtil::efatal, "Not yet implemented.");
165  break;
166 
167  default:
168  NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
169  }
170 
171  return std::numeric_limits<unsigned int>::max();
172  }
173 
174  template<typename DataType>
175  unsigned int ConstMatrix<DataType>::GetRequiredStorageSize(MatrixStorage type, unsigned int rows,
176  unsigned int columns, unsigned int subDiags, unsigned int superDiags)
177  {
178  switch(type)
179  {
180  case eFULL:
181  return FullMatrixFuncs::GetRequiredStorageSize(rows, columns);
182  break;
183  case eDIAGONAL:
184  return DiagonalMatrixFuncs::GetRequiredStorageSize(rows, columns);
185  break;
186  case eUPPER_TRIANGULAR:
188  break;
189  case eLOWER_TRIANGULAR:
191  break;
192  case eSYMMETRIC:
194  return SymmetricMatrixFuncs::GetRequiredStorageSize(rows, columns);
195  break;
196  case eBANDED:
197  return BandedMatrixFuncs::GetRequiredStorageSize(rows, columns,
198  subDiags, superDiags);
199  break;
200  case eSYMMETRIC_BANDED:
202  {
203  ASSERTL1(subDiags==superDiags,
204  std::string("Number of sub- and superdiagonals should ") +
205  std::string("be equal for a symmetric banded matrix"));
207  superDiags);
208  }
209  break;
211  NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
212  break;
214  NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
215  break;
216 
217  default:
218  NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
219  }
220 
221  return 0;
222  }
223 
224  template<typename DataType>
225  ConstMatrix<DataType>::ConstMatrix(unsigned int rows, unsigned int columns,
226  MatrixStorage policy) :
227  m_size(),
228  m_transpose('N'),
229  m_storageType(policy)
230  {
231  m_size[0] = rows;
232  m_size[1] = columns;
233  }
234 
235  template<typename DataType>
237  m_size(),
238  m_transpose(rhs.m_transpose),
239  m_storageType(rhs.m_storageType)
240  {
241  m_size[0] = rhs.m_size[0];
242  m_size[1] = rhs.m_size[1];
243  }
244 
245  template<typename DataType>
247  {
248  m_size[0] = rhs.m_size[0];
249  m_size[1] = rhs.m_size[1];
250  m_transpose = rhs.m_transpose;
251  m_storageType = rhs.m_storageType;
252  return *this;
253  }
254 
255 
256  template<typename DataType>
257  void ConstMatrix<DataType>::Resize(unsigned int rows, unsigned int columns)
258  {
259  m_size[0] = rows;
260  m_size[1] = columns;
261  }
262 
263  template<typename DataType>
265  {
266  m_transpose = newValue;
267  }
268 
269  template<typename DataType>
271 
272  template<typename DataType>
273  char ConstMatrix<DataType>::v_GetTransposeFlag() const { return m_transpose; }
274 
275 
276  template<typename DataType>
278 
279  template<typename DataType>
280  void Matrix<DataType>::SetValue(unsigned int row, unsigned int column, typename boost::call_traits<DataType>::const_reference d)
281  {
282  ASSERTL2(row < this->GetRows(), std::string("Row ") + std::to_string(row) +
283  std::string(" requested in a matrix with a maximum of ") + std::to_string(this->GetRows()) +
284  std::string(" rows"));
285  ASSERTL2(column < this->GetColumns(), std::string("Column ") + std::to_string(column) +
286  std::string(" requested in a matrix with a maximum of ") + std::to_string(this->GetColumns()) +
287  std::string(" columns"));
288  v_SetValue(row, column, d);
289  }
290 
291  template<typename DataType>
292  Matrix<DataType>::Matrix(unsigned int rows, unsigned int columns,
293  MatrixStorage policy) :
294  ConstMatrix<DataType>(rows, columns,policy)
295  {
296  }
297 
298  template<typename DataType>
300  ConstMatrix<DataType>(rhs)
301  {
302  }
303 
304  template<typename DataType>
306  {
308  return *this;
309  }
310 
311  template<typename DataType>
313  {
315  return *this;
316  }
317 
318 
321 
322  template LIB_UTILITIES_EXPORT class ConstMatrix<int>;
323  template LIB_UTILITIES_EXPORT class Matrix<int>;
324 
327 
329  template LIB_UTILITIES_EXPORT class Matrix<float>;
330 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:209
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:71
static unsigned int CalculateIndex(unsigned int curRow, unsigned int curColumn, unsigned int nSuperDiags)
static unsigned int CalculateIndex(unsigned int curRow, unsigned int curColumn)
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:119
unsigned int m_size[2]
Definition: MatrixBase.hpp:123
unsigned int GetTransposedRows(char transpose) const
Definition: MatrixBase.cpp:64
static unsigned int GetRequiredStorageSize(unsigned int rows, unsigned int columns)
Matrix(unsigned int rows, unsigned int columns, MatrixStorage policy=eFULL)
Definition: MatrixBase.cpp:292
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:113
unsigned int GetStorageSize() const
Definition: MatrixBase.cpp:52
MatrixStorage m_storageType
Definition: MatrixBase.hpp:125
unsigned int GetColumns() const
Definition: MatrixBase.cpp:77
unsigned int GetTransposedColumns(char transpose) const
Definition: MatrixBase.cpp:83
ConstMatrix(unsigned int rows, unsigned int columns, MatrixStorage policy=eFULL)
Definition: MatrixBase.cpp:225
virtual ~ConstMatrix()
Definition: MatrixBase.cpp:37
Matrix< DataType > & operator=(const Matrix< DataType > &rhs)
Definition: MatrixBase.cpp:305
#define LIB_UTILITIES_EXPORT
static unsigned int GetRequiredStorageSize(unsigned int rows, unsigned int columns, unsigned int nSubSuperDiags)
static unsigned int CalculateIndex(unsigned int curRow, unsigned int curColumn)
static unsigned int GetRequiredStorageSize(unsigned int rows, unsigned int columns)
unsigned int GetRows() const
Definition: MatrixBase.cpp:58
static unsigned int CalculateIndex(unsigned int row, unsigned int col)
ConstMatrix< DataType > & operator=(const ConstMatrix< DataType > &rhs)
Definition: MatrixBase.cpp:246
#define ASSERTL2(condition, msg)
Assert Level 2 – Debugging which is used FULLDEBUG compilation mode. This level assert is designed t...
Definition: ErrorUtil.hpp:274
static unsigned int GetRequiredStorageSize(MatrixStorage type, unsigned int rows, unsigned int columns, unsigned int subDiags=0, unsigned int superDiags=0)
Definition: MatrixBase.cpp:175
boost::call_traits< DataType >::value_type operator()(unsigned int row, unsigned int column) const
Definition: MatrixBase.cpp:40
static unsigned int CalculateIndex(unsigned int totalRows, unsigned int totalColumns, unsigned int curRow, unsigned int curColumn)
const unsigned int * GetSize() const
Definition: MatrixBase.cpp:96
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
Definition: ErrorUtil.hpp:250
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