1
#ifndef KRYLOV_SOLVERS_H
2
#define KRYLOV_SOLVERS_H
4
#include <linear_operator.h>
6
enum KrylovSolverStatus{
8
KRYLOV_EXCEEDED_MAX_ITERATIONS,
12
//============================================================================
13
// Only guaranteed for symmetric positive definite systems.
14
// Singular systems may be solved, but round-off error may cause problems,
15
// and if the system is inconsistent convergence will not be automatically detected.
18
double tolerance_factor;
19
unsigned int max_iterations;
20
double residual_norm; // we use the infinity norm
21
unsigned int iteration;
22
KrylovSolverStatus status;
23
std::vector<double> r, z, s;
26
: tolerance_factor(1e-9), max_iterations(100), residual_norm(0), iteration(0), status(KRYLOV_CONVERGED), r(0), z(0), s(0)
29
// Attempt to solve A*result=rhs for result.
30
// Sets residual_norm, iteration, and status (and also returns status)
31
// If optional preconditioner given, use it.
32
// If "use_given_initial_guess" is false (default), take initial guess to be all zeros;
33
// if true, instead use whatever is in result at the moment.
34
KrylovSolverStatus solve(const LinearOperator &A, const double *rhs, double *result,
35
const LinearOperator *preconditioner=0, bool use_given_initial_guess=false);
38
//============================================================================
39
// MINRES using the Conjugate Residual (CR) algorithm.
40
// May work on symmetric indefinite problems, but is vulnerable to breakdown
41
// except in the positive definite case.
42
struct MINRES_CR_Solver
44
double tolerance_factor;
45
unsigned int max_iterations;
46
double residual_norm; // we use the infinity norm
47
unsigned int iteration;
48
KrylovSolverStatus status;
49
std::vector<double> r, z, q, s, t;
51
MINRES_CR_Solver(void)
52
: tolerance_factor(1e-9), max_iterations(100), residual_norm(0), iteration(0), status(KRYLOV_CONVERGED), r(0), z(0), q(0), s(0), t(0)
55
// Attempt to solve A*result=rhs for result.
56
// Sets residual_norm, iteration, and status (and also returns status)
57
// If optional preconditioner given, use it.
58
// If "use_given_initial_guess" is false (default), take initial guess to be all zeros;
59
// if true, instead use whatever is in result at the moment.
60
KrylovSolverStatus solve(const LinearOperator &A, const double *rhs, double *result,
61
const LinearOperator *preconditioner=0, bool use_given_initial_guess=false);
64
//============================================================================
65
// CGNR (Conjugate Gradient applied to the Normal Equations)
66
// Should be able to solve min ||b-Ax|| for A with full column rank,
67
// including the case Ax=b for general non-singular A. Rank-deficient problems
71
double tolerance_factor;
72
unsigned int max_iterations;
73
double residual_norm; // we use the infinity norm of the residual in the normal equations, A^T*(b-A*x)
74
// Please note --- this is not the same as the residual of the original, b-A*x
75
unsigned int iteration;
76
KrylovSolverStatus status;
77
std::vector<double> r, z, s, u;
80
: tolerance_factor(1e-9), max_iterations(100), residual_norm(0), iteration(0), status(KRYLOV_CONVERGED), r(0), z(0), s(0), u(0)
83
// Attempt to solve min ||rhs-A*result|| (or A*result=rhs for nonsingular A) for result.
84
// Sets residual_norm, iteration, and status (and also returns status)
85
// If optional preconditioner given, use it; it should precondition A, so that A*M is close to orthogonal.
86
// If "use_given_initial_guess" is false (default), take initial guess to be all zeros;
87
// if true, instead use whatever is in result at the moment.
88
KrylovSolverStatus solve(const LinearOperator &A, const double *rhs, double *result,
89
const LinearOperator *preconditioner=0, bool use_given_initial_guess=false);