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)
447 template <
typename VectorType>
459 template <
typename BType,
typename XType>
460 void Solve(
const BType &b, XType &x)
const
470 template <
typename VectorType>
482 template <
typename BType,
typename XType>
508 int m = theA.GetRows();
509 int n = theA.GetColumns();
511 int pivotSize = std::max(1, std::min(m,
n));
519 std::string message =
520 "ERROR: The " + std::to_string(-info) +
521 "th parameter had an illegal parameter for dgetrf";
526 std::string message =
527 "ERROR: Element u_" + std::to_string(info) +
528 std::to_string(info) +
" is 0 from dgetrf";
534 for (
unsigned int i = 0; i < theA.GetColumns(); ++i)
536 A[i] = 1.0 / theA(i, i);
545 int pivotSize = theA.GetRows();
553 std::string message =
554 "ERROR: The " + std::to_string(-info) +
555 "th parameter had an illegal parameter for dsptrf";
560 std::string message =
561 "ERROR: Element u_" + std::to_string(info) +
562 std::to_string(info) +
" is 0 from dsptrf";
574 std::string message =
575 "ERROR: The " + std::to_string(-info) +
576 "th parameter had an illegal parameter for dpptrf";
581 std::string message =
582 "ERROR: The leading minor of order " +
583 std::to_string(info) +
584 " is not positive definite from dpptrf";
599 unsigned int requiredStorageSize =
603 unsigned int rawRows = KL + KU + 1;
607 for (
unsigned int i = 0; i < theA.GetColumns(); ++i)
609 std::copy(theA.GetRawPtr() + i * rawRows,
610 theA.GetRawPtr() + (i + 1) * rawRows,
611 A.get() + (i + 1) * KL + i * rawRows);
615 int pivotSize = theA.GetRows();
623 std::string message =
624 "ERROR: The " + std::to_string(-info) +
625 "th parameter had an illegal parameter for dgbtrf";
630 std::string message =
631 "ERROR: Element u_" + std::to_string(info) +
632 std::to_string(info) +
" is 0 from dgbtrf";
641 std::string(
"Number of sub- and superdiagonals should ") +
642 std::string(
"be equal for a symmetric banded matrix"));
650 std::string message =
651 "ERROR: The " + std::to_string(-info) +
652 "th parameter had an illegal parameter for dpbtrf";
657 std::string message =
658 "ERROR: The leading minor of order " +
659 std::to_string(info) +
660 " 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.
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)