10 #ifndef EIGEN_ITERATIVE_SOLVER_BASE_H
11 #define EIGEN_ITERATIVE_SOLVER_BASE_H
13 #include "./InternalHeaderCheck.h"
19 template<
typename MatrixType>
20 struct is_ref_compatible_impl
23 template <
typename T0>
26 template <
typename T> any_conversion(
const volatile T&);
27 template <
typename T> any_conversion(T&);
29 struct yes {
int a[1];};
30 struct no {
int a[2];};
33 static yes test(
const Ref<const T>&,
int);
35 static no test(any_conversion<T>, ...);
38 static MatrixType ms_from;
39 enum { value =
sizeof(test<MatrixType>(ms_from, 0))==
sizeof(yes) };
42 template<
typename MatrixType>
43 struct is_ref_compatible
45 enum { value = is_ref_compatible_impl<remove_all_t<MatrixType>>::value };
48 template<typename MatrixType, bool MatrixFree = !internal::is_ref_compatible<MatrixType>::value>
49 class generic_matrix_wrapper;
52 template<
typename MatrixType>
53 class generic_matrix_wrapper<MatrixType,false>
56 typedef Ref<const MatrixType> ActualMatrixType;
57 template<
int UpLo>
struct ConstSelfAdjointViewReturnType {
58 typedef typename ActualMatrixType::template ConstSelfAdjointViewReturnType<UpLo>::Type Type;
65 generic_matrix_wrapper()
66 : m_dummy(0,0), m_matrix(m_dummy)
69 template<
typename InputType>
70 generic_matrix_wrapper(
const InputType &mat)
74 const ActualMatrixType& matrix()
const
79 template<
typename MatrixDerived>
80 void grab(
const EigenBase<MatrixDerived> &mat)
82 internal::destroy_at(&m_matrix);
83 internal::construct_at(&m_matrix, mat.derived());
86 void grab(
const Ref<const MatrixType> &mat)
88 if(&(mat.derived()) != &m_matrix)
90 internal::destroy_at(&m_matrix);
91 internal::construct_at(&m_matrix, mat);
97 ActualMatrixType m_matrix;
101 template<
typename MatrixType>
102 class generic_matrix_wrapper<MatrixType,true>
105 typedef MatrixType ActualMatrixType;
106 template<
int UpLo>
struct ConstSelfAdjointViewReturnType
108 typedef ActualMatrixType Type;
115 generic_matrix_wrapper()
119 generic_matrix_wrapper(
const MatrixType &mat)
123 const ActualMatrixType& matrix()
const
128 void grab(
const MatrixType &mat)
134 const ActualMatrixType *mp_matrix;
144 template<
typename Derived>
149 using Base::m_isInitialized;
152 typedef typename internal::traits<Derived>::MatrixType MatrixType;
153 typedef typename internal::traits<Derived>::Preconditioner Preconditioner;
154 typedef typename MatrixType::Scalar Scalar;
155 typedef typename MatrixType::StorageIndex StorageIndex;
156 typedef typename MatrixType::RealScalar RealScalar;
159 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
160 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
183 template<
typename MatrixDerived>
185 : m_matrixWrapper(A.derived())
201 template<
typename MatrixDerived>
205 m_preconditioner.analyzePattern(matrix());
206 m_isInitialized =
true;
207 m_analysisIsOk =
true;
208 m_info = m_preconditioner.info();
221 template<
typename MatrixDerived>
224 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
226 m_preconditioner.factorize(matrix());
227 m_factorizationIsOk =
true;
228 m_info = m_preconditioner.info();
242 template<
typename MatrixDerived>
246 m_preconditioner.compute(matrix());
247 m_isInitialized =
true;
248 m_analysisIsOk =
true;
249 m_factorizationIsOk =
true;
250 m_info = m_preconditioner.info();
255 EIGEN_CONSTEXPR
Index rows() const EIGEN_NOEXCEPT {
return matrix().rows(); }
258 EIGEN_CONSTEXPR
Index cols() const EIGEN_NOEXCEPT {
return matrix().cols(); }
288 return (m_maxIterations<0) ? 2*matrix().cols() : m_maxIterations;
296 m_maxIterations = maxIters;
303 eigen_assert(m_isInitialized &&
"IterativeSolverBase is not initialized.");
312 eigen_assert(m_isInitialized &&
"IterativeSolverBase is not initialized.");
321 template<
typename Rhs,
typename Guess>
325 eigen_assert(m_isInitialized &&
"Solver is not initialized.");
326 eigen_assert(derived().rows()==b.
rows() &&
"solve(): invalid number of rows of the right hand side matrix b");
333 eigen_assert(m_isInitialized &&
"IterativeSolverBase is not initialized.");
338 template<
typename Rhs,
typename DestDerived>
341 eigen_assert(rows()==b.rows());
343 Index rhsCols = b.cols();
344 Index size = b.rows();
345 DestDerived& dest(aDest.
derived());
346 typedef typename DestDerived::Scalar DestScalar;
351 typename DestDerived::PlainObject tmp(cols(),rhsCols);
353 for(
Index k=0; k<rhsCols; ++k)
357 derived()._solve_vector_with_guess_impl(tb,tx);
358 tmp.col(k) = tx.sparseView(0);
367 m_info = global_info;
371 template<
typename Rhs,
typename DestDerived>
372 std::enable_if_t<Rhs::ColsAtCompileTime!=1 && DestDerived::ColsAtCompileTime!=1>
373 _solve_with_guess_impl(
const Rhs& b, MatrixBase<DestDerived> &aDest)
const
375 eigen_assert(rows()==b.rows());
377 Index rhsCols = b.cols();
378 DestDerived& dest(aDest.derived());
380 for(
Index k=0; k<rhsCols; ++k)
382 typename DestDerived::ColXpr xk(dest,k);
383 typename Rhs::ConstColXpr bk(b,k);
384 derived()._solve_vector_with_guess_impl(bk,xk);
393 m_info = global_info;
396 template<
typename Rhs,
typename DestDerived>
397 std::enable_if_t<Rhs::ColsAtCompileTime==1 || DestDerived::ColsAtCompileTime==1>
398 _solve_with_guess_impl(
const Rhs& b, MatrixBase<DestDerived> &dest)
const
400 derived()._solve_vector_with_guess_impl(b,dest.derived());
404 template<
typename Rhs,
typename Dest>
405 void _solve_impl(
const Rhs& b, Dest& x)
const
408 derived()._solve_with_guess_impl(b,x);
414 m_isInitialized =
false;
415 m_analysisIsOk =
false;
416 m_factorizationIsOk =
false;
417 m_maxIterations = -1;
418 m_tolerance = NumTraits<Scalar>::epsilon();
421 typedef internal::generic_matrix_wrapper<MatrixType> MatrixWrapper;
422 typedef typename MatrixWrapper::ActualMatrixType ActualMatrixType;
424 const ActualMatrixType& matrix()
const
426 return m_matrixWrapper.matrix();
429 template<
typename InputType>
430 void grab(
const InputType &A)
432 m_matrixWrapper.grab(A);
435 MatrixWrapper m_matrixWrapper;
436 Preconditioner m_preconditioner;
438 Index m_maxIterations;
439 RealScalar m_tolerance;
441 mutable RealScalar m_error;
442 mutable Index m_iterations;
444 mutable bool m_analysisIsOk, m_factorizationIsOk;
Derived & derived()
Definition: EigenBase.h:48
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: EigenBase.h:62
Base class for linear iterative solvers.
Definition: IterativeSolverBase.h:146
IterativeSolverBase()
Definition: IterativeSolverBase.h:168
ComputationInfo info() const
Definition: IterativeSolverBase.h:331
RealScalar error() const
Definition: IterativeSolverBase.h:310
Derived & factorize(const EigenBase< MatrixDerived > &A)
Definition: IterativeSolverBase.h:222
Index maxIterations() const
Definition: IterativeSolverBase.h:286
IterativeSolverBase(const EigenBase< MatrixDerived > &A)
Definition: IterativeSolverBase.h:184
Derived & analyzePattern(const EigenBase< MatrixDerived > &A)
Definition: IterativeSolverBase.h:202
Preconditioner & preconditioner()
Definition: IterativeSolverBase.h:277
const Preconditioner & preconditioner() const
Definition: IterativeSolverBase.h:280
Derived & compute(const EigenBase< MatrixDerived > &A)
Definition: IterativeSolverBase.h:243
Derived & setTolerance(const RealScalar &tolerance)
Definition: IterativeSolverBase.h:270
RealScalar tolerance() const
Definition: IterativeSolverBase.h:263
const SolveWithGuess< Derived, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
Definition: IterativeSolverBase.h:323
Index iterations() const
Definition: IterativeSolverBase.h:301
Derived & setMaxIterations(Index maxIters)
Definition: IterativeSolverBase.h:294
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
Pseudo expression representing a solving operation.
Definition: SolveWithGuess.h:17
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
A base class for sparse solvers.
Definition: SparseSolverBase.h:70
ComputationInfo
Definition: Constants.h:442
@ NumericalIssue
Definition: Constants.h:446
@ Success
Definition: Constants.h:444
@ NoConvergence
Definition: Constants.h:448
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
Definition: EigenBase.h:32
Derived & derived()
Definition: EigenBase.h:48