Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
| 2 | // for linear algebra. |
| 3 | // |
| 4 | // Copyright (C) 2012 Giacomo Po <gpo@ucla.edu> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 5 | // Copyright (C) 2011-2014 Gael Guennebaud <gael.guennebaud@inria.fr> |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 6 | // Copyright (C) 2018 David Hyde <dabh@stanford.edu> |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 7 | // |
| 8 | // This Source Code Form is subject to the terms of the Mozilla |
| 9 | // Public License v. 2.0. If a copy of the MPL was not distributed |
| 10 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 11 | |
| 12 | |
| 13 | #ifndef EIGEN_MINRES_H_ |
| 14 | #define EIGEN_MINRES_H_ |
| 15 | |
| 16 | |
| 17 | namespace Eigen { |
| 18 | |
| 19 | namespace internal { |
| 20 | |
| 21 | /** \internal Low-level MINRES algorithm |
| 22 | * \param mat The matrix A |
| 23 | * \param rhs The right hand side vector b |
| 24 | * \param x On input and initial solution, on output the computed solution. |
| 25 | * \param precond A right preconditioner being able to efficiently solve for an |
| 26 | * approximation of Ax=b (regardless of b) |
| 27 | * \param iters On input the max number of iteration, on output the number of performed iterations. |
| 28 | * \param tol_error On input the tolerance error, on output an estimation of the relative error. |
| 29 | */ |
| 30 | template<typename MatrixType, typename Rhs, typename Dest, typename Preconditioner> |
| 31 | EIGEN_DONT_INLINE |
| 32 | void minres(const MatrixType& mat, const Rhs& rhs, Dest& x, |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 33 | const Preconditioner& precond, Index& iters, |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 34 | typename Dest::RealScalar& tol_error) |
| 35 | { |
| 36 | using std::sqrt; |
| 37 | typedef typename Dest::RealScalar RealScalar; |
| 38 | typedef typename Dest::Scalar Scalar; |
| 39 | typedef Matrix<Scalar,Dynamic,1> VectorType; |
| 40 | |
| 41 | // Check for zero rhs |
| 42 | const RealScalar rhsNorm2(rhs.squaredNorm()); |
| 43 | if(rhsNorm2 == 0) |
| 44 | { |
| 45 | x.setZero(); |
| 46 | iters = 0; |
| 47 | tol_error = 0; |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | // initialize |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 52 | const Index maxIters(iters); // initialize maxIters to iters |
| 53 | const Index N(mat.cols()); // the size of the matrix |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 54 | const RealScalar threshold2(tol_error*tol_error*rhsNorm2); // convergence threshold (compared to residualNorm2) |
| 55 | |
| 56 | // Initialize preconditioned Lanczos |
| 57 | VectorType v_old(N); // will be initialized inside loop |
| 58 | VectorType v( VectorType::Zero(N) ); //initialize v |
| 59 | VectorType v_new(rhs-mat*x); //initialize v_new |
| 60 | RealScalar residualNorm2(v_new.squaredNorm()); |
| 61 | VectorType w(N); // will be initialized inside loop |
| 62 | VectorType w_new(precond.solve(v_new)); // initialize w_new |
| 63 | // RealScalar beta; // will be initialized inside loop |
| 64 | RealScalar beta_new2(v_new.dot(w_new)); |
| 65 | eigen_assert(beta_new2 >= 0.0 && "PRECONDITIONER IS NOT POSITIVE DEFINITE"); |
| 66 | RealScalar beta_new(sqrt(beta_new2)); |
| 67 | const RealScalar beta_one(beta_new); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 68 | // Initialize other variables |
| 69 | RealScalar c(1.0); // the cosine of the Givens rotation |
| 70 | RealScalar c_old(1.0); |
| 71 | RealScalar s(0.0); // the sine of the Givens rotation |
| 72 | RealScalar s_old(0.0); // the sine of the Givens rotation |
| 73 | VectorType p_oold(N); // will be initialized in loop |
| 74 | VectorType p_old(VectorType::Zero(N)); // initialize p_old=0 |
| 75 | VectorType p(p_old); // initialize p=0 |
| 76 | RealScalar eta(1.0); |
| 77 | |
| 78 | iters = 0; // reset iters |
| 79 | while ( iters < maxIters ) |
| 80 | { |
| 81 | // Preconditioned Lanczos |
| 82 | /* Note that there are 4 variants on the Lanczos algorithm. These are |
| 83 | * described in Paige, C. C. (1972). Computational variants of |
| 84 | * the Lanczos method for the eigenproblem. IMA Journal of Applied |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 85 | * Mathematics, 10(3), 373-381. The current implementation corresponds |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 86 | * to the case A(2,7) in the paper. It also corresponds to |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 87 | * algorithm 6.14 in Y. Saad, Iterative Methods for Sparse Linear |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 88 | * Systems, 2003 p.173. For the preconditioned version see |
| 89 | * A. Greenbaum, Iterative Methods for Solving Linear Systems, SIAM (1987). |
| 90 | */ |
| 91 | const RealScalar beta(beta_new); |
| 92 | v_old = v; // update: at first time step, this makes v_old = 0 so value of beta doesn't matter |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 93 | v_new /= beta_new; // overwrite v_new for next iteration |
| 94 | w_new /= beta_new; // overwrite w_new for next iteration |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 95 | v = v_new; // update |
| 96 | w = w_new; // update |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 97 | v_new.noalias() = mat*w - beta*v_old; // compute v_new |
| 98 | const RealScalar alpha = v_new.dot(w); |
| 99 | v_new -= alpha*v; // overwrite v_new |
| 100 | w_new = precond.solve(v_new); // overwrite w_new |
| 101 | beta_new2 = v_new.dot(w_new); // compute beta_new |
| 102 | eigen_assert(beta_new2 >= 0.0 && "PRECONDITIONER IS NOT POSITIVE DEFINITE"); |
| 103 | beta_new = sqrt(beta_new2); // compute beta_new |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 104 | |
| 105 | // Givens rotation |
| 106 | const RealScalar r2 =s*alpha+c*c_old*beta; // s, s_old, c and c_old are still from previous iteration |
| 107 | const RealScalar r3 =s_old*beta; // s, s_old, c and c_old are still from previous iteration |
| 108 | const RealScalar r1_hat=c*alpha-c_old*s*beta; |
| 109 | const RealScalar r1 =sqrt( std::pow(r1_hat,2) + std::pow(beta_new,2) ); |
| 110 | c_old = c; // store for next iteration |
| 111 | s_old = s; // store for next iteration |
| 112 | c=r1_hat/r1; // new cosine |
| 113 | s=beta_new/r1; // new sine |
| 114 | |
| 115 | // Update solution |
| 116 | p_oold = p_old; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 117 | p_old = p; |
| 118 | p.noalias()=(w-r2*p_old-r3*p_oold) /r1; // IS NOALIAS REQUIRED? |
| 119 | x += beta_one*c*eta*p; |
| 120 | |
| 121 | /* Update the squared residual. Note that this is the estimated residual. |
| 122 | The real residual |Ax-b|^2 may be slightly larger */ |
| 123 | residualNorm2 *= s*s; |
| 124 | |
| 125 | if ( residualNorm2 < threshold2) |
| 126 | { |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | eta=-s*eta; // update eta |
| 131 | iters++; // increment iteration number (for output purposes) |
| 132 | } |
| 133 | |
| 134 | /* Compute error. Note that this is the estimated error. The real |
| 135 | error |Ax-b|/|b| may be slightly larger */ |
| 136 | tol_error = std::sqrt(residualNorm2 / rhsNorm2); |
| 137 | } |
| 138 | |
| 139 | } |
| 140 | |
| 141 | template< typename _MatrixType, int _UpLo=Lower, |
| 142 | typename _Preconditioner = IdentityPreconditioner> |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 143 | class MINRES; |
| 144 | |
| 145 | namespace internal { |
| 146 | |
| 147 | template< typename _MatrixType, int _UpLo, typename _Preconditioner> |
| 148 | struct traits<MINRES<_MatrixType,_UpLo,_Preconditioner> > |
| 149 | { |
| 150 | typedef _MatrixType MatrixType; |
| 151 | typedef _Preconditioner Preconditioner; |
| 152 | }; |
| 153 | |
| 154 | } |
| 155 | |
| 156 | /** \ingroup IterativeLinearSolvers_Module |
| 157 | * \brief A minimal residual solver for sparse symmetric problems |
| 158 | * |
| 159 | * This class allows to solve for A.x = b sparse linear problems using the MINRES algorithm |
| 160 | * of Paige and Saunders (1975). The sparse matrix A must be symmetric (possibly indefinite). |
| 161 | * The vectors x and b can be either dense or sparse. |
| 162 | * |
| 163 | * \tparam _MatrixType the type of the sparse matrix A, can be a dense or a sparse matrix. |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 164 | * \tparam _UpLo the triangular part that will be used for the computations. It can be Lower, |
| 165 | * Upper, or Lower|Upper in which the full matrix entries will be considered. Default is Lower. |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 166 | * \tparam _Preconditioner the type of the preconditioner. Default is DiagonalPreconditioner |
| 167 | * |
| 168 | * The maximal number of iterations and tolerance value can be controlled via the setMaxIterations() |
| 169 | * and setTolerance() methods. The defaults are the size of the problem for the maximal number of iterations |
| 170 | * and NumTraits<Scalar>::epsilon() for the tolerance. |
| 171 | * |
| 172 | * This class can be used as the direct solver classes. Here is a typical usage example: |
| 173 | * \code |
| 174 | * int n = 10000; |
| 175 | * VectorXd x(n), b(n); |
| 176 | * SparseMatrix<double> A(n,n); |
| 177 | * // fill A and b |
| 178 | * MINRES<SparseMatrix<double> > mr; |
| 179 | * mr.compute(A); |
| 180 | * x = mr.solve(b); |
| 181 | * std::cout << "#iterations: " << mr.iterations() << std::endl; |
| 182 | * std::cout << "estimated error: " << mr.error() << std::endl; |
| 183 | * // update b, and solve again |
| 184 | * x = mr.solve(b); |
| 185 | * \endcode |
| 186 | * |
| 187 | * By default the iterations start with x=0 as an initial guess of the solution. |
| 188 | * One can control the start using the solveWithGuess() method. |
| 189 | * |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 190 | * MINRES can also be used in a matrix-free context, see the following \link MatrixfreeSolverExample example \endlink. |
| 191 | * |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 192 | * \sa class ConjugateGradient, BiCGSTAB, SimplicialCholesky, DiagonalPreconditioner, IdentityPreconditioner |
| 193 | */ |
| 194 | template< typename _MatrixType, int _UpLo, typename _Preconditioner> |
| 195 | class MINRES : public IterativeSolverBase<MINRES<_MatrixType,_UpLo,_Preconditioner> > |
| 196 | { |
| 197 | |
| 198 | typedef IterativeSolverBase<MINRES> Base; |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 199 | using Base::matrix; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 200 | using Base::m_error; |
| 201 | using Base::m_iterations; |
| 202 | using Base::m_info; |
| 203 | using Base::m_isInitialized; |
| 204 | public: |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 205 | using Base::_solve_impl; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 206 | typedef _MatrixType MatrixType; |
| 207 | typedef typename MatrixType::Scalar Scalar; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 208 | typedef typename MatrixType::RealScalar RealScalar; |
| 209 | typedef _Preconditioner Preconditioner; |
| 210 | |
| 211 | enum {UpLo = _UpLo}; |
| 212 | |
| 213 | public: |
| 214 | |
| 215 | /** Default constructor. */ |
| 216 | MINRES() : Base() {} |
| 217 | |
| 218 | /** Initialize the solver with matrix \a A for further \c Ax=b solving. |
| 219 | * |
| 220 | * This constructor is a shortcut for the default constructor followed |
| 221 | * by a call to compute(). |
| 222 | * |
| 223 | * \warning this class stores a reference to the matrix A as well as some |
| 224 | * precomputed values that depend on it. Therefore, if \a A is changed |
| 225 | * this class becomes invalid. Call compute() to update it with the new |
| 226 | * matrix A, or modify a copy of A. |
| 227 | */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 228 | template<typename MatrixDerived> |
| 229 | explicit MINRES(const EigenBase<MatrixDerived>& A) : Base(A.derived()) {} |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 230 | |
| 231 | /** Destructor. */ |
| 232 | ~MINRES(){} |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 233 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 234 | /** \internal */ |
| 235 | template<typename Rhs,typename Dest> |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 236 | void _solve_vector_with_guess_impl(const Rhs& b, Dest& x) const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 237 | { |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 238 | typedef typename Base::MatrixWrapper MatrixWrapper; |
| 239 | typedef typename Base::ActualMatrixType ActualMatrixType; |
| 240 | enum { |
| 241 | TransposeInput = (!MatrixWrapper::MatrixFree) |
| 242 | && (UpLo==(Lower|Upper)) |
| 243 | && (!MatrixType::IsRowMajor) |
| 244 | && (!NumTraits<Scalar>::IsComplex) |
| 245 | }; |
| 246 | typedef typename internal::conditional<TransposeInput,Transpose<const ActualMatrixType>, ActualMatrixType const&>::type RowMajorWrapper; |
| 247 | EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(MatrixWrapper::MatrixFree,UpLo==(Lower|Upper)),MATRIX_FREE_CONJUGATE_GRADIENT_IS_COMPATIBLE_WITH_UPPER_UNION_LOWER_MODE_ONLY); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 248 | typedef typename internal::conditional<UpLo==(Lower|Upper), |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 249 | RowMajorWrapper, |
| 250 | typename MatrixWrapper::template ConstSelfAdjointViewReturnType<UpLo>::Type |
| 251 | >::type SelfAdjointWrapper; |
| 252 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 253 | m_iterations = Base::maxIterations(); |
| 254 | m_error = Base::m_tolerance; |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 255 | RowMajorWrapper row_mat(matrix()); |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 256 | internal::minres(SelfAdjointWrapper(row_mat), b, x, |
| 257 | Base::m_preconditioner, m_iterations, m_error); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 258 | m_info = m_error <= Base::m_tolerance ? Success : NoConvergence; |
| 259 | } |
| 260 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 261 | protected: |
| 262 | |
| 263 | }; |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 264 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 265 | } // end namespace Eigen |
| 266 | |
| 267 | #endif // EIGEN_MINRES_H |