35 #ifndef NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_NEK_LINSYS_HPP
36 #define NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_NEK_LINSYS_HPP
48 #include <type_traits>
64 template <
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 dtrtrs";
121 std::string message =
122 "ERROR: The " + std::to_string(-info) +
123 "th diagonal element of A is 0 for dtrtrs";
133 x.GetRawPtr(), n, info);
137 std::string message =
138 "ERROR: The " + std::to_string(-info) +
139 "th parameter had an illegal parameter for dtrtrs";
144 std::string message =
145 "ERROR: The " + std::to_string(-info) +
146 "th diagonal element of A is 0 for dtrtrs";
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 dtrtrs";
293 std::string message =
294 "ERROR: The " + std::to_string(-info) +
295 "th diagonal element of A is 0 for dtrtrs";
303 char trans = m_transposeFlag;
319 std::string message =
320 "ERROR: The " + std::to_string(-info) +
321 "th parameter had an illegal parameter for dtrtrs";
326 std::string message =
327 "ERROR: The " + std::to_string(-info) +
328 "th diagonal element of A is 0 for dtrtrs";
336 Solve(b, x, m_matrixType, m_ipivot, n,
A, m_transposeFlag,
337 m_numberOfSubDiagonals, m_numberOfSuperDiagonals);
342 int KL = m_numberOfSubDiagonals;
343 int KU = m_numberOfSuperDiagonals;
347 2 * KL + KU + 1, m_ipivot.get(), x.GetRawPtr(),
352 std::string message =
353 "ERROR: The " + std::to_string(-info) +
354 "th parameter had an illegal parameter for dgbtrs";
378 template <
typename MatrixType>
389 ASSERTL0(theA->GetTransposeFlag() ==
'N',
390 "LinearSystem requires a non-transposed matrix.");
392 wrapperType ==
eCopy,
393 "Banded matrices can't be wrapped");
395 if (wrapperType ==
eCopy)
404 template <
typename MatrixType>
414 ASSERTL0(theA.GetTransposeFlag() ==
'N',
415 "LinearSystem requires a non-transposed matrix.");
417 wrapperType ==
eCopy,
418 "Banded matrices can't be wrapped");
420 if (wrapperType ==
eCopy)
450 template <
typename VectorType>
462 template <
typename BType,
typename XType>
463 void Solve(
const BType &b, XType &x)
const
473 template <
typename VectorType>
485 template <
typename BType,
typename XType>
511 int m = theA.GetRows();
512 int n = theA.GetColumns();
514 int pivotSize = std::max(1, std::min(m,
n));
522 std::string message =
523 "ERROR: The " + std::to_string(-info) +
524 "th parameter had an illegal parameter for dgetrf";
529 std::string message =
530 "ERROR: Element u_" + std::to_string(info) +
531 std::to_string(info) +
" is 0 from dgetrf";
537 for (
unsigned int i = 0; i < theA.GetColumns(); ++i)
539 A[i] = 1.0 / theA(i, i);
548 int pivotSize = theA.GetRows();
556 std::string message =
557 "ERROR: The " + std::to_string(-info) +
558 "th parameter had an illegal parameter for dsptrf";
563 std::string message =
564 "ERROR: Element u_" + std::to_string(info) +
565 std::to_string(info) +
" is 0 from dsptrf";
577 std::string message =
578 "ERROR: The " + std::to_string(-info) +
579 "th parameter had an illegal parameter for dpptrf";
584 std::string message =
585 "ERROR: The leading minor of order " +
586 std::to_string(info) +
587 " is not positive definite from dpptrf";
602 unsigned int requiredStorageSize =
606 unsigned int rawRows = KL + KU + 1;
610 for (
unsigned int i = 0; i < theA.GetColumns(); ++i)
612 std::copy(theA.GetRawPtr() + i * rawRows,
613 theA.GetRawPtr() + (i + 1) * rawRows,
614 A.get() + (i + 1) * KL + i * rawRows);
618 int pivotSize = theA.GetRows();
626 std::string message =
627 "ERROR: The " + std::to_string(-info) +
628 "th parameter had an illegal parameter for dgbtrf";
633 std::string message =
634 "ERROR: Element u_" + std::to_string(info) +
635 std::to_string(info) +
" is 0 from dgbtrf";
644 std::string(
"Number of sub- and superdiagonals should ") +
645 std::string(
"be equal for a symmetric banded matrix"));
653 std::string message =
654 "ERROR: The " + std::to_string(-info) +
655 "th parameter had an illegal parameter for dpbtrf";
660 std::string message =
661 "ERROR: The leading minor of order " +
662 std::to_string(info) +
663 " 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)
RawType_t< VectorType > SolveTranspose(const VectorType &b)
RawType_t< VectorType > Solve(const VectorType &b)
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
LinearSystem(const LinearSystem &rhs)
unsigned int m_numberOfSuperDiagonals
unsigned int m_numberOfSubDiagonals
MatrixStorage m_matrixType
LinearSystem & operator=(const LinearSystem &rhs)
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 defiinte 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 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)
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)