25 template<
class T=real_t>
29 typedef memory::shared_ptr<gsIterativeSolver> Ptr;
30 typedef memory::unique_ptr<gsIterativeSolver> uPtr;
42 const LinOpPtr& precond)
70 template<
typename Derived>
72 const LinOpPtr& precond)
73 :
m_mat(makeMatrixOp(mat.derived())),
95 opt.
addInt (
"MaxIterations" ,
"Maximum number of iterations", 1000 );
96 opt.
addReal (
"Tolerance" ,
"Tolerance for the error criteria on the "
97 "relative residual error", 1e-10 );
138 error_history.resize(1,1);
144 std::vector<T> tmp_error_hist;
145 tmp_error_hist.clear();
147 tmp_error_hist.push_back(
m_error);
156 tmp_error_hist.push_back(
m_error);
161 tmp_error_hist.push_back(
m_error);
175 "Iterative solvers only work for single column right-hand side." );
177 "The right-hand side does not match the matrix: "
178 << rhs.rows() <<
"!="<<
m_mat->rows() );
186 x.setZero(rhs.rows(),rhs.cols());
192 x.setZero(rhs.rows(), rhs.cols());
196 "Iterative solvers only work for single right-hand side and solution." );
198 "The initial guess does not match the matrix: "
199 << x.rows() <<
"!="<<
m_mat->cols() );
204 virtual bool step( VectorType& x ) = 0;
238 virtual std::ostream &
print(std::ostream &os)
const = 0;
243 std::ostringstream os;
245 os <<
" Tolerance : " <<
tolerance() <<
"\n";
246 os <<
" Solver error : " <<
error() <<
"\n";
264 std::ostream &operator<<(std::ostream &os, const gsIterativeSolver<T>& b)
265 {
return b.print(os); }
268 template <
class SolverType>
271 typedef typename SolverType::ScalarType T;
275 typedef memory::shared_ptr<gsIterativeSolverOp>
Ptr;
278 typedef memory::unique_ptr<gsIterativeSolverOp>
uPtr;
281 template<
class MatrixType>
283 : m_solver(matrix, preconditioner) {}
286 template<
class MatrixType>
287 static uPtr make(
const MatrixType& matrix,
const LinOpPtr& preconditioner = LinOpPtr())
293 void apply(
const gsMatrix<T> & input, gsMatrix<T> & res)
const
295 res.setZero(input.rows(), input.cols());
296 m_solver.solve(input, res);
297 gsDebug << ( (m_solver.error() < m_solver.tolerance())
298 ?
"gsIterativeSolverOp reached desired error bound after "
299 :
"msIterativeSolverOp did not reach desired error bound within " )
300 << m_solver.iterations() <<
" iterations.\n";
301 gsDebug << input.rows() <<
"; " << input.norm() <<
"; " << res.norm() <<
"\n";
304 index_t rows()
const {
return m_solver.underlying()->rows(); }
306 index_t cols()
const {
return m_solver.underlying()->cols(); }
309 SolverType& solver() {
return m_solver; }
312 const SolverType& solver()
const {
return m_solver; }
315 mutable SolverType m_solver;
const LinOpPtr m_mat
The matrix/operator to be solved for.
Definition: gsIterativeSolver.h:252
unique_ptr< T > make_unique(T *x)
Definition: gsMemory.h:198
void solve(const VectorType &rhs, VectorType &x)
Solves the linear system and stores the solution in x.
Definition: gsIterativeSolver.h:114
index_t size() const
Returns the size of the linear system.
Definition: gsIterativeSolver.h:208
virtual gsIterativeSolver & setOptions(const gsOptionList &opt)
Set the options based on a gsOptionList.
Definition: gsIterativeSolver.h:102
virtual bool step(VectorType &x)=0
Perform one step, requires initIteration.
memory::unique_ptr< gsIterativeSolverOp > uPtr
Unique pointer for gsIterativeSolverOp.
Definition: gsIterativeSolver.h:278
#define gsDebug
Definition: gsDebug.h:61
virtual bool initIteration(const VectorType &rhs, VectorType &x)
Init the iteration.
Definition: gsIterativeSolver.h:172
gsOptionList::Real m_tol
The tolerance for m_error to be reached.
Definition: gsIterativeSolver.h:255
void setPreconditioner(const LinOpPtr &precond)
Set the preconditionner.
Definition: gsIterativeSolver.h:211
virtual index_t rows() const =0
Returns the number of rows of the operator.
static uPtr make(index_t dim)
Make function returning a smart pointer.
Definition: gsLinearOperator.h:132
#define index_t
Definition: gsConfig.h:32
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
LinOpPtr preconditioner() const
Get the preconditioner.
Definition: gsIterativeSolver.h:214
Handles shared library creation and other class attributes.
LinOpPtr underlying() const
Get the underlying matrix/operator to be solved for.
Definition: gsIterativeSolver.h:217
T m_rhs_norm
The norm of the right-hand-side.
Definition: gsIterativeSolver.h:257
void addInt(const std::string &label, const std::string &desc, const index_t &value)
Adds a option named label, with description desc and value value.
Definition: gsOptionList.cpp:201
Creates a mapped object or data pointer to a vector without copying data.
Definition: gsLinearAlgebra.h:129
Provides a list of labeled parameters/options that can be set and accessed easily.
memory::shared_ptr< gsLinearOperator > Ptr
Shared pointer for gsLinearOperator.
Definition: gsLinearOperator.h:33
static gsOptionList defaultOptions()
Returns a list of default options.
Definition: gsIterativeSolver.h:92
void solveDetailed(const VectorType &rhs, VectorType &x, VectorType &error_history)
Solves the linear system and stores the solution in x and records the error histroy.
Definition: gsIterativeSolver.h:134
index_t m_max_iters
The upper bound for the number of iterations.
Definition: gsIterativeSolver.h:254
void setTolerance(T tol)
Set the tolerance for the error criteria on the relative residual error (default: 1e-10) ...
Definition: gsIterativeSolver.h:223
T m_error
The relative error as absolute_error/m_rhs_norm.
Definition: gsIterativeSolver.h:258
Real askReal(const std::string &label, const Real &value=0) const
Reads value for option label from options.
Definition: gsOptionList.cpp:139
void setMaxIterations(index_t max_iters)
Set the maximum number of iterations (default: 1000)
Definition: gsIterativeSolver.h:220
virtual std::ostream & print(std::ostream &os) const =0
Prints the object as a string.
Simple abstract class for discrete operators.
Definition: gsLinearOperator.h:28
T tolerance() const
The chosen tolerance for the error criteria on the relative residual error.
Definition: gsIterativeSolver.h:235
index_t askInt(const std::string &label, const index_t &value=0) const
Reads value for option label from options.
Definition: gsOptionList.cpp:117
void addReal(const std::string &label, const std::string &desc, const Real &value)
Adds a option named label, with description desc and value value.
Definition: gsOptionList.cpp:211
virtual std::string detail() const
Prints the object as a string with extended details.
Definition: gsIterativeSolver.h:241
memory::shared_ptr< gsIterativeSolverOp > Ptr
Shared pointer for gsIterativeSolverOp.
Definition: gsIterativeSolver.h:275
index_t iterations() const
The number of iterations needed to reach the error criteria.
Definition: gsIterativeSolver.h:226
This is the main header file that collects wrappers of Eigen for linear algebra.
virtual void apply(const gsMatrix< SolverType::ScalarType > &input, gsMatrix< SolverType::ScalarType > &x) const =0
apply the operator on the input vector and store the result in x
virtual index_t cols() const =0
Returns the number of columns of the operator.
Abstract class for iterative solvers.
Definition: gsIterativeSolver.h:26
Class which holds a list of parameters/options, and provides easy access to them. ...
Definition: gsOptionList.h:32
gsIterativeSolver(const LinOpPtr &mat, const LinOpPtr &precond)
Contructor using a linear operator to be solved for and a preconditioner.
Definition: gsIterativeSolver.h:41
gsIterativeSolver(const gsEigen::EigenBase< Derived > &mat, const LinOpPtr &precond)
Contructor using any dense or sparse matrix and a preconditioner.
Definition: gsIterativeSolver.h:71
T error() const
The relative residual error of the current iterate.
Definition: gsIterativeSolver.h:232
This wrapper class allows gsIterativeSolver to be used as gsLinearOperator.
Definition: gsIterativeSolver.h:269
Simple adapter classes to use matrices or linear solvers as gsLinearOperators.
virtual void finalizeIteration(VectorType &)
Some post-processing might be required.
Definition: gsIterativeSolver.h:205
index_t m_num_iter
The number of iterations performed.
Definition: gsIterativeSolver.h:256
LinOpPtr m_precond
The preconditioner.
Definition: gsIterativeSolver.h:253