35 #ifndef NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_NEK_LINSYS_HPP 36 #define NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_NEK_LINSYS_HPP 48 #include <type_traits> 60 template<
typename DataType>
63 template<
typename DataType>
71 template<
typename BVectorType,
typename XVectorType>
75 char m_transposeFlag,
unsigned int m_numberOfSubDiagonals,
76 unsigned int m_numberOfSuperDiagonals)
84 Lapack::Dgetrs(
'N',n,1,A.get(),n,(
int *)m_ipivot.get(),x.GetRawPtr(),n,info);
87 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dgetrs";
94 for(
unsigned int i = 0; i < A.num_elements(); ++i)
103 Lapack::Dtptrs(
'U', m_transposeFlag,
'N', n, 1, A.get(), x.GetRawPtr(), n, info);
107 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dtrtrs";
112 std::string message =
"ERROR: The " + std::to_string(-info) +
"th diagonal element of A is 0 for dtrtrs";
121 Lapack::Dtptrs(
'L', m_transposeFlag,
'N', n, 1, A.get(), x.GetRawPtr(), n, info);
125 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dtrtrs";
130 std::string message =
"ERROR: The " + std::to_string(-info) +
"th diagonal element of A is 0 for dtrtrs";
139 Lapack::Dsptrs(
'U', n, 1, A.get(), m_ipivot.get(), x.GetRawPtr(), x.GetRows(), info);
142 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dsptrs";
151 Lapack::Dpptrs(
'U', n, 1, A.get(), x.GetRawPtr(), x.GetRows(), info);
154 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dpptrs";
162 int KL = m_numberOfSubDiagonals;
163 int KU = m_numberOfSuperDiagonals;
166 Lapack::Dgbtrs(m_transposeFlag, n, KL, KU, 1, A.get(), 2*KL+KU+1, m_ipivot.get(), x.GetRawPtr(), n, info);
170 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dgbtrs";
178 int KU = m_numberOfSuperDiagonals;
181 Lapack::Dpbtrs(
'U', n, KU, 1, A.get(), KU+1, x.GetRawPtr(), n, info);
185 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dpbtrs";
206 template<
typename BVectorType,
typename XVectorType>
210 char m_transposeFlag,
unsigned int m_numberOfSubDiagonals,
211 unsigned int m_numberOfSuperDiagonals)
219 Lapack::Dgetrs(
'T',n,1,A.get(),n,(
int *)m_ipivot.get(),x.GetRawPtr(), n,info);
223 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dgetrs";
230 Solve(b, x, m_matrixType, m_ipivot, n, A, m_transposeFlag, m_numberOfSubDiagonals, m_numberOfSuperDiagonals);
234 char trans = m_transposeFlag;
246 Lapack::Dtptrs(
'U', trans,
'N', n, 1, A.get(), x.GetRawPtr(), n, info);
250 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dtrtrs";
255 std::string message =
"ERROR: The " + std::to_string(-info) +
"th diagonal element of A is 0 for dtrtrs";
263 char trans = m_transposeFlag;
274 Lapack::Dtptrs(
'L', trans,
'N', n, 1, A.get(), x.GetRawPtr(), n, info);
278 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dtrtrs";
283 std::string message =
"ERROR: The " + std::to_string(-info) +
"th diagonal element of A is 0 for dtrtrs";
291 Solve(b, x, m_matrixType, m_ipivot, n, A, m_transposeFlag, m_numberOfSubDiagonals, m_numberOfSuperDiagonals);
296 int KL = m_numberOfSubDiagonals;
297 int KU = m_numberOfSuperDiagonals;
300 Lapack::Dgbtrs(m_transposeFlag, n, KL, KU, 1, A.get(), 2*KL+KU+1, m_ipivot.get(), x.GetRawPtr(), n, info);
304 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dgbtrs";
329 template<
typename MatrixType>
334 m_numberOfSubDiagonals(theA->GetNumberOfSubDiagonals()),
335 m_numberOfSuperDiagonals(theA->GetNumberOfSuperDiagonals()),
336 m_matrixType(theA->GetType()),
337 m_transposeFlag(theA->GetTransposeFlag())
341 ASSERTL0(theA->GetTransposeFlag() ==
'N',
"LinearSystem requires a non-transposed matrix.");
344 if( wrapperType ==
eCopy )
353 template<
typename MatrixType>
358 m_numberOfSubDiagonals(theA.GetNumberOfSubDiagonals()),
359 m_numberOfSuperDiagonals(theA.GetNumberOfSuperDiagonals()),
360 m_matrixType(theA.GetType()),
361 m_transposeFlag(theA.GetTransposeFlag())
365 ASSERTL0(theA.GetTransposeFlag() ==
'N',
"LinearSystem requires a non-transposed matrix.");
368 if( wrapperType ==
eCopy )
380 m_ipivot(rhs.m_ipivot),
381 m_numberOfSubDiagonals(rhs.m_numberOfSubDiagonals),
382 m_numberOfSuperDiagonals(rhs.m_numberOfSuperDiagonals),
383 m_matrixType(rhs.m_matrixType),
384 m_transposeFlag(rhs.m_transposeFlag)
399 template<
typename VectorType>
404 m_ipivot, n,
A, m_transposeFlag, m_numberOfSubDiagonals, m_numberOfSuperDiagonals);
408 template<
typename BType,
typename XType>
409 void Solve(
const BType& b, XType& x)
const 413 m_ipivot, n,
A, m_transposeFlag, m_numberOfSubDiagonals, m_numberOfSuperDiagonals);
417 template<
typename VectorType>
422 m_ipivot, n,
A, m_transposeFlag, m_numberOfSubDiagonals, m_numberOfSuperDiagonals);
426 template<
typename BType,
typename XType>
431 m_ipivot, n,
A, m_transposeFlag, m_numberOfSubDiagonals, m_numberOfSuperDiagonals);
438 template<
typename MatrixType>
445 int m = theA.GetRows();
446 int n = theA.GetColumns();
448 int pivotSize = std::max(1, std::min(m, n));
456 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dgetrf";
461 std::string message =
"ERROR: Element u_" + std::to_string(info) + std::to_string(info) +
" is 0 from dgetrf";
467 for(
unsigned int i = 0; i < theA.GetColumns(); ++i)
469 A[i] = 1.0/theA(i,i);
478 int pivotSize = theA.GetRows();
485 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dsptrf";
490 std::string message =
"ERROR: Element u_" + std::to_string(info) + std::to_string(info) +
" is 0 from dsptrf";
502 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dpptrf";
507 std::string message =
"ERROR: The leading minor of order " + std::to_string(info) +
" is not positive definite from dpptrf";
516 int KL = m_numberOfSubDiagonals;
517 int KU = m_numberOfSuperDiagonals;
525 unsigned int rawRows = KL+KU+1;
529 for(
unsigned int i = 0; i < theA.GetColumns(); ++i)
531 std::copy(theA.GetRawPtr() + i*rawRows, theA.GetRawPtr() + (i+1)*rawRows,
532 A.get() + (i+1)*KL + i*rawRows);
536 int pivotSize = theA.GetRows();
539 Lapack::Dgbtrf(M, N, KL, KU,
A.get(), 2*KL+KU+1, m_ipivot.get(), info);
543 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dgbtrf";
548 std::string message =
"ERROR: Element u_" + std::to_string(info) + std::to_string(info) +
" is 0 from dgbtrf";
555 ASSERTL1(m_numberOfSuperDiagonals==m_numberOfSuperDiagonals,
556 std::string(
"Number of sub- and superdiagonals should ") +
557 std::string(
"be equal for a symmetric banded matrix"));
559 int KU = m_numberOfSuperDiagonals;
565 std::string message =
"ERROR: The " + std::to_string(-info) +
"th parameter had an illegal parameter for dpbtrf";
570 std::string message =
"ERROR: The leading minor of order " + std::to_string(info) +
" is not positive definite from dpbtrf";
611 #endif //NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_NEK_LINSYS_HPP void SolveTranspose(const BType &b, XType &x) const
static void Dgetrf(const int &m, const int &n, double *a, const int &lda, int *ipiv, int &info)
General matrix LU factorisation.
#define ASSERTL0(condition, msg)
unsigned int GetRows() const
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
MatrixStorage m_matrixType
RawType_t< VectorType > Solve(const VectorType &b)
void swap(LinearSystem &rhs)
LinearSystem(const std::shared_ptr< MatrixType > &theA, PointerWrapper wrapperType=eCopy)
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.
LinearSystem & operator=(const LinearSystem &rhs)
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.
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.
unsigned int m_numberOfSuperDiagonals
static void Solve(Nektar::Array< OneD, NekDouble > pX, struct crs_data *pCrs, Nektar::Array< OneD, NekDouble > pB)
Solve the matrix system for a given input vector b.
unsigned int GetColumns() const
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 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.
RawType_t< VectorType > SolveTranspose(const VectorType &b)
void FactorMatrix(const MatrixType &theA)
void CopyArray(const Array< OneD, ConstDataType > &source, Array< OneD, DataType > &dest)
PointerWrapper
Specifies if the pointer passed to a NekMatrix or NekVector is copied into an internal representation...
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 Dpptrf(const char &uplo, const int &n, double *ap, int &info)
Cholesky factor a real Positive Definite packed-symmetric matrix.
LinearSystem(const LinearSystem &rhs)
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 Dsptrf(const char &uplo, const int &n, double *ap, int *ipiv, int &info)
factor a real packed-symmetric matrix using Bunch-Kaufman pivoting.
LinearSystem(const MatrixType &theA, PointerWrapper wrapperType=eCopy)
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)
void Solve(const BType &b, XType &x) const
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs
unsigned int m_numberOfSubDiagonals
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
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.
typename RawType< T >::type RawType_t
static unsigned int GetRequiredStorageSize(unsigned int totalRows, unsigned int totalColumns, unsigned int subDiags, unsigned int superDiags)
Calculates and returns the storage size required.
Array< OneD, int > m_ipivot