35#ifndef NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_NEK_LINSYS_HPP
36#define NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_NEK_LINSYS_HPP
64template <
typename DataType>
74 template <
typename BVectorType,
typename XVectorType>
75 static void Solve(
const BVectorType &b, XVectorType &x,
79 unsigned int m_numberOfSubDiagonals,
80 unsigned int m_numberOfSuperDiagonals)
89 x.GetRawPtr(), n, info);
93 "ERROR: The " + std::to_string(-info) +
94 "th parameter had an illegal parameter for dgetrs";
100 for (
unsigned int i = 0; i <
A.size(); ++i)
110 x.GetRawPtr(), n, info);
114 std::string message =
115 "ERROR: The " + std::to_string(-info) +
116 "th parameter had an illegal parameter for dtptrs";
121 std::string message =
122 "ERROR: The " + std::to_string(-info) +
123 "th diagonal element of A is 0 for dtptrs";
133 x.GetRawPtr(), n, info);
137 std::string message =
138 "ERROR: The " + std::to_string(-info) +
139 "th parameter had an illegal parameter for dtptrs";
144 std::string message =
145 "ERROR: The " + std::to_string(-info) +
146 "th diagonal element of A is 0 for dtptrs";
156 x.GetRawPtr(), x.GetRows(), info);
159 std::string message =
160 "ERROR: The " + std::to_string(-info) +
161 "th parameter had an illegal parameter for dsptrs";
174 std::string message =
175 "ERROR: The " + std::to_string(-info) +
176 "th parameter had an illegal parameter for dpptrs";
184 int KL = m_numberOfSubDiagonals;
185 int KU = m_numberOfSuperDiagonals;
189 2 * KL + KU + 1, m_ipivot.get(), x.GetRawPtr(),
194 std::string message =
195 "ERROR: The " + std::to_string(-info) +
196 "th parameter had an illegal parameter for dgbtrs";
204 int KU = m_numberOfSuperDiagonals;
212 std::string message =
213 "ERROR: The " + std::to_string(-info) +
214 "th parameter had an illegal parameter for dpbtrs";
234 template <
typename BVectorType,
typename XVectorType>
240 char m_transposeFlag,
241 unsigned int m_numberOfSubDiagonals,
242 unsigned int m_numberOfSuperDiagonals)
244 switch (m_matrixType)
251 x.GetRawPtr(), n, info);
255 std::string message =
256 "ERROR: The " + std::to_string(-info) +
257 "th parameter had an illegal parameter for dgetrs";
264 Solve(b, x, m_matrixType, m_ipivot, n,
A, m_transposeFlag,
265 m_numberOfSubDiagonals, m_numberOfSuperDiagonals);
269 char trans = m_transposeFlag;
286 std::string message =
287 "ERROR: The " + std::to_string(-info) +
288 "th parameter had an illegal parameter for dtptrs";
293 std::string message =
294 "ERROR: The " + std::to_string(-info) +
295 "th diagonal element of A is 0 for dtptrs";
303 char trans = m_transposeFlag;
320 std::string message =
321 "ERROR: The " + std::to_string(-info) +
322 "th parameter had an illegal parameter for dtptrs";
327 std::string message =
328 "ERROR: The " + std::to_string(-info) +
329 "th diagonal element of A is 0 for dtptrs";
337 Solve(b, x, m_matrixType, m_ipivot, n,
A, m_transposeFlag,
338 m_numberOfSubDiagonals, m_numberOfSuperDiagonals);
343 int KL = m_numberOfSubDiagonals;
344 int KU = m_numberOfSuperDiagonals;
348 2 * KL + KU + 1, m_ipivot.get(), x.GetRawPtr(),
353 std::string message =
354 "ERROR: The " + std::to_string(-info) +
355 "th parameter had an illegal parameter for dgbtrs";
379 template <
typename MatrixType>
390 ASSERTL0(theA->GetTransposeFlag() ==
'N',
391 "LinearSystem requires a non-transposed matrix.");
393 wrapperType ==
eCopy,
394 "Banded matrices can't be wrapped");
396 if (wrapperType ==
eCopy)
405 template <
typename MatrixType>
415 ASSERTL0(theA.GetTransposeFlag() ==
'N',
416 "LinearSystem requires a non-transposed matrix.");
418 wrapperType ==
eCopy,
419 "Banded matrices can't be wrapped");
421 if (wrapperType ==
eCopy)
451 template <
typename VectorType>
463 template <
typename BType,
typename XType>
464 void Solve(
const BType &b, XType &x)
const
474 template <
typename VectorType>
486 template <
typename BType,
typename XType>
512 int m = theA.GetRows();
513 int n = theA.GetColumns();
515 int pivotSize = std::max(1, std::min(m,
n));
523 std::string message =
524 "ERROR: The " + std::to_string(-info) +
525 "th parameter had an illegal parameter for dgetrf";
530 std::string message =
531 "ERROR: Element u_" + std::to_string(info) +
532 std::to_string(info) +
" is 0 from dgetrf";
538 for (
unsigned int i = 0; i < theA.GetColumns(); ++i)
540 A[i] = 1.0 / theA(i, i);
549 int pivotSize = theA.GetRows();
557 std::string message =
558 "ERROR: The " + std::to_string(-info) +
559 "th parameter had an illegal parameter for dsptrf";
564 std::string message =
565 "ERROR: Element u_" + std::to_string(info) +
566 std::to_string(info) +
" is 0 from dsptrf";
578 std::string message =
579 "ERROR: The " + std::to_string(-info) +
580 "th parameter had an illegal parameter for dpptrf";
585 std::string message =
586 "ERROR: The leading minor of order " +
587 std::to_string(info) +
588 " is not positive definite from dpptrf";
603 unsigned int requiredStorageSize =
607 unsigned int rawRows = KL + KU + 1;
611 for (
unsigned int i = 0; i < theA.GetColumns(); ++i)
613 std::copy(theA.GetRawPtr() + i * rawRows,
614 theA.GetRawPtr() + (i + 1) * rawRows,
615 A.get() + (i + 1) * KL + i * rawRows);
619 int pivotSize = theA.GetRows();
627 std::string message =
628 "ERROR: The " + std::to_string(-info) +
629 "th parameter had an illegal parameter for dgbtrf";
634 std::string message =
635 "ERROR: Element u_" + std::to_string(info) +
636 std::to_string(info) +
" is 0 from dgbtrf";
645 std::string(
"Number of sub- and superdiagonals should ") +
646 std::string(
"be equal for a symmetric banded matrix"));
654 std::string message =
655 "ERROR: The " + std::to_string(-info) +
656 "th parameter had an illegal parameter for dpbtrf";
661 std::string message =
662 "ERROR: The leading minor of order " +
663 std::to_string(info) +
664 " is not positive definite from dpbtrf";
#define ASSERTL0(condition, msg)
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
unsigned int GetColumns() const
LinearSystem(const MatrixType &theA, PointerWrapper wrapperType=eCopy)
void swap(LinearSystem &rhs)
void Solve(const BType &b, XType &x) const
LinearSystem(const std::shared_ptr< MatrixType > &theA, PointerWrapper wrapperType=eCopy)
Array< OneD, int > m_ipivot
void FactorMatrix(const MatrixType &theA)
void SolveTranspose(const BType &b, XType &x) const
RawType_t< VectorType > SolveTranspose(const VectorType &b)
RawType_t< VectorType > Solve(const VectorType &b)
LinearSystem & operator=(const LinearSystem &rhs)
LinearSystem(const LinearSystem &rhs)
unsigned int m_numberOfSuperDiagonals
unsigned int m_numberOfSubDiagonals
MatrixStorage m_matrixType
unsigned int GetRows() const
static void Dpptrs(const char &uplo, const int &n, const int &nrhs, const double *ap, double *b, const int &ldb, int &info)
Solve a real positive definite symmetric matrix problem using Cholesky factorization.
static void Dgbtrf(const int &m, const int &n, const int &kl, const int &ku, double *a, const int &lda, int *ipiv, int &info)
General banded matrix LU factorisation.
static void Dsptrs(const char &uplo, const int &n, const int &nrhs, const double *ap, const int *ipiv, double *b, const int &ldb, int &info)
Solve a real packed-symmetric matrix problem using Bunch-Kaufman pivoting.
static void Dgetrf(const int &m, const int &n, double *a, const int &lda, int *ipiv, int &info)
General matrix LU factorisation.
static void Dpptrf(const char &uplo, const int &n, double *ap, int &info)
Cholesky factor a real positive definite packed-symmetric matrix.
static void Dgetrs(const char &trans, const int &n, const int &nrhs, const double *a, const int &lda, int *ipiv, double *b, const int &ldb, int &info)
General matrix LU backsolve.
static void Dsptrf(const char &uplo, const int &n, double *ap, int *ipiv, int &info)
factor a real packed-symmetric matrix using Bunch-Kaufman pivoting.
static void Dpbtrf(const char &uplo, const int &n, const int &kd, double *ab, const int &ldab, int &info)
Cholesky factorize a real positive definite banded-symmetric matrix.
static void Dgbtrs(const char &trans, const int &n, const int &kl, const int &ku, const int &nrhs, const double *a, const int &lda, const int *ipiv, double *b, const int &ldb, int &info)
Solve general banded matrix using LU factorisation.
static void Dtptrs(const char &uplo, const char &trans, const char &diag, const int &n, const int &nrhs, const double *a, double *b, const int &ldb, int &info)
Solve a triangular system.
static void Dpbtrs(const char &uplo, const int &n, const int &kd, const int &nrhs, const double *ab, const int &ldab, double *b, const int &ldb, int &info)
Solve a real, positive definite banded-symmetric matrix problem using Cholesky factorization.
The above copyright notice and this permission notice shall be included.
typename RawType< T >::type RawType_t
@ eLOWER_TRIANGULAR_BANDED
@ ePOSITIVE_DEFINITE_SYMMETRIC_BANDED
@ ePOSITIVE_DEFINITE_SYMMETRIC
@ eUPPER_TRIANGULAR_BANDED
PointerWrapper
Specifies if the pointer passed to a NekMatrix or NekVector is copied into an internal representation...
void CopyArray(const Array< OneD, ConstDataType > &source, Array< OneD, DataType > &dest)
static unsigned int GetRequiredStorageSize(unsigned int totalRows, unsigned int totalColumns, unsigned int subDiags, unsigned int superDiags)
Calculates and returns the storage size required.
static void SolveTranspose(const BVectorType &b, XVectorType &x, MatrixStorage m_matrixType, const Array< OneD, const int > &m_ipivot, unsigned int n, const Array< OneD, const double > &A, char m_transposeFlag, unsigned int m_numberOfSubDiagonals, unsigned int m_numberOfSuperDiagonals)
static void Solve(const BVectorType &b, XVectorType &x, MatrixStorage m_matrixType, const Array< OneD, const int > &m_ipivot, unsigned int n, const Array< OneD, const double > &A, char m_transposeFlag, unsigned int m_numberOfSubDiagonals, unsigned int m_numberOfSuperDiagonals)