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