10#ifndef EIGEN_ITERATIVE_SOLVER_BASE_H
11#define EIGEN_ITERATIVE_SOLVER_BASE_H
13#include "./InternalHeaderCheck.h"
19template<
typename MatrixType>
20struct 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) };
42template<
typename MatrixType>
43struct is_ref_compatible
45 enum { value = is_ref_compatible_impl<typename remove_all<MatrixType>::type>::value };
48template<typename MatrixType, bool MatrixFree = !internal::is_ref_compatible<MatrixType>::value>
49class generic_matrix_wrapper;
52template<
typename MatrixType>
53class 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 m_matrix.~Ref<
const MatrixType>();
83 ::new (&m_matrix) Ref<const MatrixType>(mat.derived());
86 void grab(
const Ref<const MatrixType> &mat)
88 if(&(mat.derived()) != &m_matrix)
90 m_matrix.~Ref<
const MatrixType>();
91 ::new (&m_matrix) Ref<const MatrixType>(mat);
97 ActualMatrixType m_matrix;
101template<
typename MatrixType>
102class 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;
144template<
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())
198 template<
typename MatrixDerived>
202 m_preconditioner.analyzePattern(matrix());
203 m_isInitialized =
true;
204 m_analysisIsOk =
true;
205 m_info = m_preconditioner.info();
218 template<
typename MatrixDerived>
221 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
223 m_preconditioner.factorize(matrix());
224 m_factorizationIsOk =
true;
225 m_info = m_preconditioner.info();
239 template<
typename MatrixDerived>
243 m_preconditioner.compute(matrix());
244 m_isInitialized =
true;
245 m_analysisIsOk =
true;
246 m_factorizationIsOk =
true;
247 m_info = m_preconditioner.info();
252 EIGEN_CONSTEXPR
Index rows() const EIGEN_NOEXCEPT {
return matrix().rows(); }
255 EIGEN_CONSTEXPR
Index cols() const EIGEN_NOEXCEPT {
return matrix().cols(); }
285 return (m_maxIterations<0) ? 2*matrix().cols() : m_maxIterations;
293 m_maxIterations = maxIters;
300 eigen_assert(m_isInitialized &&
"ConjugateGradient is not initialized.");
309 eigen_assert(m_isInitialized &&
"ConjugateGradient is not initialized.");
318 template<
typename Rhs,
typename Guess>
322 eigen_assert(m_isInitialized &&
"Solver is not initialized.");
323 eigen_assert(derived().rows()==b.
rows() &&
"solve(): invalid number of rows of the right hand side matrix b");
330 eigen_assert(m_isInitialized &&
"IterativeSolverBase is not initialized.");
335 template<
typename Rhs,
typename DestDerived>
338 eigen_assert(rows()==b.rows());
340 Index rhsCols = b.cols();
341 Index size = b.rows();
342 DestDerived& dest(aDest.
derived());
343 typedef typename DestDerived::Scalar DestScalar;
348 typename DestDerived::PlainObject tmp(cols(),rhsCols);
350 for(
Index k=0; k<rhsCols; ++k)
354 derived()._solve_vector_with_guess_impl(tb,tx);
355 tmp.col(k) = tx.sparseView(0);
364 m_info = global_info;
368 template<
typename Rhs,
typename DestDerived>
369 typename internal::enable_if<Rhs::ColsAtCompileTime!=1 && DestDerived::ColsAtCompileTime!=1>::type
370 _solve_with_guess_impl(
const Rhs& b, MatrixBase<DestDerived> &aDest)
const
372 eigen_assert(rows()==b.rows());
374 Index rhsCols = b.cols();
375 DestDerived& dest(aDest.derived());
377 for(
Index k=0; k<rhsCols; ++k)
379 typename DestDerived::ColXpr xk(dest,k);
380 typename Rhs::ConstColXpr bk(b,k);
381 derived()._solve_vector_with_guess_impl(bk,xk);
390 m_info = global_info;
393 template<
typename Rhs,
typename DestDerived>
394 typename internal::enable_if<Rhs::ColsAtCompileTime==1 || DestDerived::ColsAtCompileTime==1>::type
395 _solve_with_guess_impl(
const Rhs& b, MatrixBase<DestDerived> &dest)
const
397 derived()._solve_vector_with_guess_impl(b,dest.derived());
401 template<
typename Rhs,
typename Dest>
402 void _solve_impl(
const Rhs& b, Dest& x)
const
405 derived()._solve_with_guess_impl(b,x);
411 m_isInitialized =
false;
412 m_analysisIsOk =
false;
413 m_factorizationIsOk =
false;
414 m_maxIterations = -1;
415 m_tolerance = NumTraits<Scalar>::epsilon();
418 typedef internal::generic_matrix_wrapper<MatrixType> MatrixWrapper;
419 typedef typename MatrixWrapper::ActualMatrixType ActualMatrixType;
421 const ActualMatrixType& matrix()
const
423 return m_matrixWrapper.matrix();
426 template<
typename InputType>
427 void grab(
const InputType &A)
429 m_matrixWrapper.grab(A);
432 MatrixWrapper m_matrixWrapper;
433 Preconditioner m_preconditioner;
435 Index m_maxIterations;
436 RealScalar m_tolerance;
438 mutable RealScalar m_error;
439 mutable Index m_iterations;
441 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:328
RealScalar error() const
Definition: IterativeSolverBase.h:307
Index maxIterations() const
Definition: IterativeSolverBase.h:283
Derived & setMaxIterations(Index maxIters)
Definition: IterativeSolverBase.h:291
Derived & compute(const EigenBase< MatrixDerived > &A)
Definition: IterativeSolverBase.h:240
IterativeSolverBase(const EigenBase< MatrixDerived > &A)
Definition: IterativeSolverBase.h:184
const Preconditioner & preconditioner() const
Definition: IterativeSolverBase.h:277
Derived & analyzePattern(const EigenBase< MatrixDerived > &A)
Definition: IterativeSolverBase.h:199
Derived & factorize(const EigenBase< MatrixDerived > &A)
Definition: IterativeSolverBase.h:219
Preconditioner & preconditioner()
Definition: IterativeSolverBase.h:274
Derived & setTolerance(const RealScalar &tolerance)
Definition: IterativeSolverBase.h:267
RealScalar tolerance() const
Definition: IterativeSolverBase.h:260
const SolveWithGuess< Derived, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
Definition: IterativeSolverBase.h:320
Index iterations() const
Definition: IterativeSolverBase.h:298
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: B01_Experimental.dox:1
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