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