NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
numcxx::TSolverLapackLU< T > Class Template Reference

Description

template<typename T>
class numcxx::TSolverLapackLU< T >

Lapack LU factorization class.

Examples:
13-numcxx-lapack.cxx.

Definition at line 18 of file tsolver-lapacklu.hxx.

+ Inheritance diagram for numcxx::TSolverLapackLU< T >:
+ Collaboration diagram for numcxx::TSolverLapackLU< T >:

Public Member Functions

 TSolverLapackLU (const std::shared_ptr< TMatrix< T >> pMatrix)
 Object constructor, calls update() to obtain factorization. More...
 
 TSolverLapackLU (const TMatrix< T > &Matrix)
 Object constructor, calls update(Matrix) to obtain factorization. More...
 
void update ()
 Perform computation of LU factorization using actual state of matrix. More...
 
void update (const TMatrix< T > &Matrix)
 Perform computation of LU factorization using actual state of matrix. More...
 
void solve (TArray< T > &Sol, const TArray< T > &Rhs) const
 Solve LU factorized system. More...
 
void solve (std::shared_ptr< TArray1< T >> Sol, const std::shared_ptr< TArray1< T >> Rhs) const
 Solve LU factorized system. More...
 
std::shared_ptr< TMatrix< T > > calculate_inverse ()
 Calculate inverse of matrix A from its LU factors. More...
 
 TSolverLapackLU ()
 Default constructor for swig. More...
 
TMatrix< T > & LU ()
 
TArray1< int > & IPiv ()
 
template<>
void update (const TMatrix< double > &Matrix)
 
template<>
void solve (TArray< double > &sol, const TArray< double > &rhs) const
 
template<>
void update (const TMatrix< float > &Matrix)
 
template<>
void solve (TArray< float > &sol, const TArray< float > &rhs) const
 
template<>
std::shared_ptr< TMatrix< double > > calculate_inverse ()
 
template<>
std::shared_ptr< TMatrix< float > > calculate_inverse ()
 

Static Public Member Functions

static std::shared_ptr< TSolverLapackLU< T > > create (const std::shared_ptr< TMatrix< T >> pMatrix)
 Static wrapper around constructor. More...
 

Private Attributes

const std::shared_ptr< TMatrix< T > > pMatrix
 
const std::shared_ptr< TMatrix< T > > pLU
 
const std::shared_ptr< TArray1< int > > pIPiv
 

Constructor & Destructor Documentation

template<typename T >
numcxx::TSolverLapackLU< T >::TSolverLapackLU ( const std::shared_ptr< TMatrix< T >>  pMatrix)
inline

Object constructor, calls update() to obtain factorization.

Definition at line 9 of file tsolver-lapacklu.ixx.

9  :
10  TLinSolver<T>(),
12  pLU(pMatrix->clone()),
14  {
15  update();;
16  }
const std::shared_ptr< TMatrix< T > > pMatrix
void update()
Perform computation of LU factorization using actual state of matrix.
const std::shared_ptr< TArray1< int > > pIPiv
static std::shared_ptr< TArray1< T > > create(index n1)
Construct smart pointer empty 1D Array.
Definition: tarray1.hxx:81
const std::shared_ptr< TMatrix< T > > pLU

+ Here is the call graph for this function:

template<typename T >
numcxx::TSolverLapackLU< T >::TSolverLapackLU ( const TMatrix< T > &  Matrix)
inline

Object constructor, calls update(Matrix) to obtain factorization.

Definition at line 19 of file tsolver-lapacklu.ixx.

19  :
20  TLinSolver<T>(),
21  pMatrix(0),
22  pLU(TMatrix<T>::create(Matrix.shape(0),Matrix.shape(1))),
23  pIPiv(TArray1<int>::create(Matrix.shape(0)))
24  {
25  update(Matrix);
26  }
const std::shared_ptr< TMatrix< T > > pMatrix
static std::shared_ptr< TMatrix< T > > create(index n1, index n2)
Construct empty square matrix.
Definition: tmatrix.hxx:81
void update()
Perform computation of LU factorization using actual state of matrix.
const std::shared_ptr< TArray1< int > > pIPiv
static std::shared_ptr< TArray1< T > > create(index n1)
Construct smart pointer empty 1D Array.
Definition: tarray1.hxx:81
const std::shared_ptr< TMatrix< T > > pLU

+ Here is the call graph for this function:

template<typename T >
numcxx::TSolverLapackLU< T >::TSolverLapackLU ( )
inline

Default constructor for swig.

Definition at line 60 of file tsolver-lapacklu.hxx.

60 {};

Member Function Documentation

template<typename T >
std::shared_ptr< TSolverLapackLU< T > > numcxx::TSolverLapackLU< T >::create ( const std::shared_ptr< TMatrix< T >>  pMatrix)
inlinestatic

Static wrapper around constructor.

Examples:
14-numcxx-lapack-sharedptr.cxx.

Definition at line 29 of file tsolver-lapacklu.ixx.

30  {
31  return std::make_shared<TSolverLapackLU<T>>(a);
32  }

+ Here is the call graph for this function:

template<typename T >
void numcxx::TSolverLapackLU< T >::update ( void  )
virtual

Perform computation of LU factorization using actual state of matrix.

Uses dgetrf from the LAPACK library (for T=double)

Reimplemented from numcxx::TLinSolver< T >.

Definition at line 95 of file tsolver-lapacklu.ixx.

96  {
97  if (pMatrix==nullptr)
98  throw std::runtime_error("numcxx: TSolverLapackLU created without smartpointer");
99  update(*pMatrix);
100  }
const std::shared_ptr< TMatrix< T > > pMatrix
void update()
Perform computation of LU factorization using actual state of matrix.
template<typename T >
void numcxx::TSolverLapackLU< T >::update ( const TMatrix< T > &  Matrix)

Perform computation of LU factorization using actual state of matrix.

template<typename T >
void numcxx::TSolverLapackLU< T >::solve ( TArray< T > &  Sol,
const TArray< T > &  Rhs 
) const
virtual

Solve LU factorized system.

Uses dgetrs from the LAPACK library (for T=double)

Reimplemented from numcxx::TLinSolver< T >.

Examples:
13-numcxx-lapack.cxx.
template<typename T >
void numcxx::TSolverLapackLU< T >::solve ( std::shared_ptr< TArray1< T >>  Sol,
const std::shared_ptr< TArray1< T >>  Rhs 
) const
inline

Solve LU factorized system.

Definition at line 53 of file tsolver-lapacklu.hxx.

53 {solve(*Sol,*Rhs);};
void solve(TArray< T > &Sol, const TArray< T > &Rhs) const
Solve LU factorized system.

+ Here is the call graph for this function:

template<typename T >
std::shared_ptr<TMatrix<T> > numcxx::TSolverLapackLU< T >::calculate_inverse ( )

Calculate inverse of matrix A from its LU factors.

Todo:
move constructor
template<typename T >
TMatrix<T>& numcxx::TSolverLapackLU< T >::LU ( )
inline

Definition at line 64 of file tsolver-lapacklu.hxx.

64 { return *pLU;}
const std::shared_ptr< TMatrix< T > > pLU
template<typename T >
TArray1<int>& numcxx::TSolverLapackLU< T >::IPiv ( )
inline

Definition at line 65 of file tsolver-lapacklu.hxx.

65 { return *pIPiv;}
const std::shared_ptr< TArray1< int > > pIPiv
template<>
void numcxx::TSolverLapackLU< double >::update ( const TMatrix< double > &  Matrix)
inline

Definition at line 46 of file tsolver-lapacklu.ixx.

47  {
48  int n=pLU->shape(0);
49  int info=0;
50  *pLU=Matrix;
51  dgetrf_(&n,&n,pLU->data(),&n,pIPiv->data(),&info);
52  if (info!=0)
53  {
54  char errormsg[80];
55  snprintf(errormsg,80,"numcxx::TSolverLapackLU::update: dgetrf error %d\n",info);
56  throw std::runtime_error(errormsg);
57  }
58  }
void dgetrf_(int *n, int *m, double *a, int *lda, int *ipiv, int *info)
const std::shared_ptr< TArray1< int > > pIPiv
const std::shared_ptr< TMatrix< T > > pLU

+ Here is the call graph for this function:

template<>
void numcxx::TSolverLapackLU< double >::solve ( TArray< double > &  sol,
const TArray< double > &  rhs 
) const
inline

Definition at line 61 of file tsolver-lapacklu.ixx.

62  {
63  assign(sol,rhs);
64  char trans[2]={'T','\0'};
65  int n=pLU->shape(0);
66  int one=1;
67  int info=0;
68  dgetrs_(trans,&n,&one,pLU->data(),&n,pIPiv->data(),sol.data(),&n,&info);
69  if (info!=0)
70  {
71  char errormsg[80];
72  snprintf(errormsg,80,"numcxx::TSolverLapackLU::update: dgetrs error %d\n",info);
73  throw std::runtime_error(errormsg);
74  }
75  }
void dgetrs_(char *trans, int *n, const int *nrhs, double *a, int *lda, int *ipiv, double *b, int *ldb, int *info)
const std::shared_ptr< TArray1< int > > pIPiv
TArray< T > & assign(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:35
const std::shared_ptr< TMatrix< T > > pLU

+ Here is the call graph for this function:

template<>
void numcxx::TSolverLapackLU< float >::update ( const TMatrix< float > &  Matrix)
inline

Definition at line 79 of file tsolver-lapacklu.ixx.

80  {
81  int n=pLU->shape(0);
82  int info;
83  *pLU=Matrix;
84  sgetrf_(&n,&n,pLU->data(),&n,pIPiv->data(),&info);
85  if (info!=0)
86  {
87  char errormsg[80];
88  snprintf(errormsg,80,"numcxx::TSolverLapackLU::update: sgetrf error %d\n",info);
89  throw std::runtime_error(errormsg);
90  }
91 
92  }
const std::shared_ptr< TArray1< int > > pIPiv
const std::shared_ptr< TMatrix< T > > pLU
void sgetrf_(int *n, int *m, float *a, int *lda, int *ipiv, int *info)

+ Here is the call graph for this function:

template<>
void numcxx::TSolverLapackLU< float >::solve ( TArray< float > &  sol,
const TArray< float > &  rhs 
) const
inline

Definition at line 104 of file tsolver-lapacklu.ixx.

105  {
106  assign(sol,rhs);
107  char trans[2]={'T','\0'};
108  int n=pLU->shape(0);
109  int one=1;
110  int info;
111  sgetrs_(trans,&n,&one,pLU->data(),&n,pIPiv->data(),sol.data(),&n,&info);
112  if (info!=0)
113  {
114  char errormsg[80];
115  snprintf(errormsg,80,"numcxx::TSolverLapackLU::update: sgetrs error %d\n",info);
116  throw std::runtime_error(errormsg);
117  }
118 
119  }
const std::shared_ptr< TArray1< int > > pIPiv
TArray< T > & assign(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:35
const std::shared_ptr< TMatrix< T > > pLU
void sgetrs_(char *trans, int *n, const int *nrhs, float *a, int *lda, int *ipiv, float *b, int *ldb, int *info)

+ Here is the call graph for this function:

template<>
std::shared_ptr< TMatrix< double > > numcxx::TSolverLapackLU< double >::calculate_inverse ( )
inline

Definition at line 124 of file tsolver-lapacklu.ixx.

125  {
126  int n=pLU->shape(0);
127  auto pInverse=std::make_shared<TMatrix<double>>(*pLU);
128  int info;
129  TArray1<double> Work(n);
130  dgetri_(&n,
131  pInverse->data(),
132  &n,
133  pIPiv->data(),
134  Work.data(),
135  &n,
136  &info);
137  if (info!=0)
138  {
139  char errormsg[80];
140  snprintf(errormsg,80,"numcxx::TSolverLapackLU::calculate_inverse: dgetri error %d\n",info);
141  throw std::runtime_error(errormsg);
142  }
143  return pInverse;
144  }
void dgetri_(int *n, double *a, int *lda, int *ipiv, double *work, int *lwork, int *info)
const std::shared_ptr< TArray1< int > > pIPiv
const std::shared_ptr< TMatrix< T > > pLU

+ Here is the call graph for this function:

template<>
std::shared_ptr< TMatrix< float > > numcxx::TSolverLapackLU< float >::calculate_inverse ( )
inline

Definition at line 147 of file tsolver-lapacklu.ixx.

148  {
149  int n=pLU->shape(0);
150  auto pInverse=std::make_shared<TMatrix<float>>(*pLU);
151  int info;
152  TArray1<float> Work(n);
153  sgetri_(&n,
154  pInverse->data(),
155  &n,
156  pIPiv->data(),
157  Work.data(),
158  &n,
159  &info);
160  if (info!=0)
161  {
162  char errormsg[80];
163  snprintf(errormsg,80,"numcxx::TSolverLapackLU::calculate_inverse: sgetri error %d\n",info);
164  throw std::runtime_error(errormsg);
165  }
166  return pInverse;
167  }
const std::shared_ptr< TArray1< int > > pIPiv
const std::shared_ptr< TMatrix< T > > pLU
void sgetri_(int *n, float *a, int *lda, int *ipiv, float *work, int *lwork, int *info)

+ Here is the call graph for this function:

Member Data Documentation

template<typename T >
const std::shared_ptr< TMatrix<T> > numcxx::TSolverLapackLU< T >::pMatrix
private

Definition at line 20 of file tsolver-lapacklu.hxx.

template<typename T >
const std::shared_ptr< TMatrix<T> > numcxx::TSolverLapackLU< T >::pLU
private

Definition at line 21 of file tsolver-lapacklu.hxx.

template<typename T >
const std::shared_ptr< TArray1<int> > numcxx::TSolverLapackLU< T >::pIPiv
private

Definition at line 22 of file tsolver-lapacklu.hxx.


The documentation for this class was generated from the following files: