10#ifndef EIGEN_ITERATIVE_SOLVER_BASE_H
11#define EIGEN_ITERATIVE_SOLVER_BASE_H
14#include "./InternalHeaderCheck.h"
20template <
typename MatrixType>
21struct is_ref_compatible_impl {
23 template <
typename T0>
24 struct any_conversion {
26 any_conversion(
const volatile T&);
38 static yes test(
const Ref<const T>&,
int);
40 static no test(any_conversion<T>, ...);
43 static MatrixType ms_from;
44 enum { value =
sizeof(test<MatrixType>(ms_from, 0)) ==
sizeof(yes) };
47template <
typename MatrixType>
48struct is_ref_compatible {
49 enum { value = is_ref_compatible_impl<remove_all_t<MatrixType>>::value };
52template <typename MatrixType, bool MatrixFree = !internal::is_ref_compatible<MatrixType>::value>
53class generic_matrix_wrapper;
56template <
typename MatrixType>
57class generic_matrix_wrapper<MatrixType, false> {
59 typedef Ref<const MatrixType> ActualMatrixType;
61 struct ConstSelfAdjointViewReturnType {
62 typedef typename ActualMatrixType::template ConstSelfAdjointViewReturnType<UpLo>::Type Type;
65 enum { MatrixFree =
false };
67 generic_matrix_wrapper() : m_dummy(0, 0), m_matrix(m_dummy) {}
69 template <
typename InputType>
70 generic_matrix_wrapper(
const InputType& mat) : m_matrix(mat) {}
72 const ActualMatrixType& matrix()
const {
return m_matrix; }
74 template <
typename MatrixDerived>
75 void grab(
const EigenBase<MatrixDerived>& mat) {
76 internal::destroy_at(&m_matrix);
77 internal::construct_at(&m_matrix, mat.derived());
80 void grab(
const Ref<const MatrixType>& mat) {
81 if (&(mat.derived()) != &m_matrix) {
82 internal::destroy_at(&m_matrix);
83 internal::construct_at(&m_matrix, mat);
89 ActualMatrixType m_matrix;
93template <
typename MatrixType>
94class generic_matrix_wrapper<MatrixType, true> {
96 typedef MatrixType ActualMatrixType;
98 struct ConstSelfAdjointViewReturnType {
99 typedef ActualMatrixType Type;
102 enum { MatrixFree =
true };
104 generic_matrix_wrapper() : mp_matrix(0) {}
106 generic_matrix_wrapper(
const MatrixType& mat) : mp_matrix(&mat) {}
108 const ActualMatrixType& matrix()
const {
return *mp_matrix; }
110 void grab(
const MatrixType& mat) { mp_matrix = &mat; }
113 const ActualMatrixType* mp_matrix;
123template <
typename Derived>
127 using Base::m_isInitialized;
130 typedef typename internal::traits<Derived>::MatrixType MatrixType;
131 typedef typename internal::traits<Derived>::Preconditioner Preconditioner;
132 typedef typename MatrixType::Scalar Scalar;
133 typedef typename MatrixType::StorageIndex StorageIndex;
134 typedef typename MatrixType::RealScalar RealScalar;
136 enum { ColsAtCompileTime = MatrixType::ColsAtCompileTime, MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime };
154 template <
typename MatrixDerived>
169 template <
typename MatrixDerived>
172 m_preconditioner.analyzePattern(matrix());
173 m_isInitialized =
true;
174 m_analysisIsOk =
true;
175 m_info = m_preconditioner.info();
189 template <
typename MatrixDerived>
191 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
193 m_preconditioner.factorize(matrix());
194 m_factorizationIsOk =
true;
195 m_info = m_preconditioner.info();
209 template <
typename MatrixDerived>
212 m_preconditioner.compute(matrix());
213 m_isInitialized =
true;
214 m_analysisIsOk =
true;
215 m_factorizationIsOk =
true;
216 m_info = m_preconditioner.info();
221 EIGEN_CONSTEXPR
Index rows() const EIGEN_NOEXCEPT {
return matrix().rows(); }
224 EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT {
return matrix().cols(); }
251 Index maxIterations()
const {
return (m_maxIterations < 0) ? 2 * matrix().cols() : m_maxIterations; }
257 m_maxIterations = maxIters;
263 eigen_assert(m_isInitialized &&
"IterativeSolverBase is not initialized.");
271 eigen_assert(m_isInitialized &&
"IterativeSolverBase is not initialized.");
280 template <
typename Rhs,
typename Guess>
282 eigen_assert(m_isInitialized &&
"Solver is not initialized.");
283 eigen_assert(derived().rows() == b.
rows() &&
"solve(): invalid number of rows of the right hand side matrix b");
289 eigen_assert(m_isInitialized &&
"IterativeSolverBase is not initialized.");
294 template <
typename Rhs,
typename DestDerived>
296 eigen_assert(rows() == b.rows());
298 Index rhsCols = b.cols();
299 Index size = b.rows();
300 DestDerived& dest(aDest.
derived());
301 typedef typename DestDerived::Scalar DestScalar;
307 typename DestDerived::PlainObject tmp(cols(), rhsCols);
309 for (
Index k = 0; k < rhsCols; ++k) {
312 derived()._solve_vector_with_guess_impl(tb, tx);
313 tmp.col(k) = tx.sparseView(0);
322 m_info = global_info;
326 template <
typename Rhs,
typename DestDerived>
327 std::enable_if_t<Rhs::ColsAtCompileTime != 1 && DestDerived::ColsAtCompileTime != 1> _solve_with_guess_impl(
328 const Rhs& b, MatrixBase<DestDerived>& aDest)
const {
329 eigen_assert(rows() == b.rows());
331 Index rhsCols = b.cols();
332 DestDerived& dest(aDest.derived());
333 ComputationInfo global_info =
Success;
334 for (Index k = 0; k < rhsCols; ++k) {
335 typename DestDerived::ColXpr xk(dest, k);
336 typename Rhs::ConstColXpr bk(b, k);
337 derived()._solve_vector_with_guess_impl(bk, xk);
346 m_info = global_info;
349 template <
typename Rhs,
typename DestDerived>
350 std::enable_if_t<Rhs::ColsAtCompileTime == 1 || DestDerived::ColsAtCompileTime == 1> _solve_with_guess_impl(
351 const Rhs& b, MatrixBase<DestDerived>& dest)
const {
352 derived()._solve_vector_with_guess_impl(b, dest.derived());
356 template <
typename Rhs,
typename Dest>
357 void _solve_impl(
const Rhs& b, Dest& x)
const {
359 derived()._solve_with_guess_impl(b, x);
364 m_isInitialized =
false;
365 m_analysisIsOk =
false;
366 m_factorizationIsOk =
false;
367 m_maxIterations = -1;
368 m_tolerance = NumTraits<Scalar>::epsilon();
371 typedef internal::generic_matrix_wrapper<MatrixType> MatrixWrapper;
372 typedef typename MatrixWrapper::ActualMatrixType ActualMatrixType;
374 const ActualMatrixType& matrix()
const {
return m_matrixWrapper.matrix(); }
376 template <
typename InputType>
377 void grab(
const InputType& A) {
378 m_matrixWrapper.grab(A);
381 MatrixWrapper m_matrixWrapper;
382 Preconditioner m_preconditioner;
384 Index m_maxIterations;
385 RealScalar m_tolerance;
387 mutable RealScalar m_error;
388 mutable Index m_iterations;
389 mutable ComputationInfo m_info;
390 mutable bool m_analysisIsOk, m_factorizationIsOk;
Derived & derived()
Definition EigenBase.h:49
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition EigenBase.h:59
Base class for linear iterative solvers.
Definition IterativeSolverBase.h:124
IterativeSolverBase()
Definition IterativeSolverBase.h:142
ComputationInfo info() const
Definition IterativeSolverBase.h:288
RealScalar error() const
Definition IterativeSolverBase.h:270
Index maxIterations() const
Definition IterativeSolverBase.h:251
Derived & setMaxIterations(Index maxIters)
Definition IterativeSolverBase.h:256
Derived & compute(const EigenBase< MatrixDerived > &A)
Definition IterativeSolverBase.h:210
IterativeSolverBase(const EigenBase< MatrixDerived > &A)
Definition IterativeSolverBase.h:155
const Preconditioner & preconditioner() const
Definition IterativeSolverBase.h:245
Derived & analyzePattern(const EigenBase< MatrixDerived > &A)
Definition IterativeSolverBase.h:170
Derived & factorize(const EigenBase< MatrixDerived > &A)
Definition IterativeSolverBase.h:190
Preconditioner & preconditioner()
Definition IterativeSolverBase.h:242
Derived & setTolerance(const RealScalar &tolerance)
Definition IterativeSolverBase.h:236
RealScalar tolerance() const
Definition IterativeSolverBase.h:229
const SolveWithGuess< Derived, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
Definition IterativeSolverBase.h:281
Index iterations() const
Definition IterativeSolverBase.h:262
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:186
Pseudo expression representing a solving operation.
Definition SolveWithGuess.h:42
Base class of any sparse matrices or sparse expressions.
Definition SparseMatrixBase.h:30
A base class for sparse solvers.
Definition SparseSolverBase.h:67
ComputationInfo
Definition Constants.h:438
@ NumericalIssue
Definition Constants.h:442
@ Success
Definition Constants.h:440
@ NoConvergence
Definition Constants.h:444
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
Definition EigenBase.h:33
Derived & derived()
Definition EigenBase.h:49