10 #ifndef EIGEN_ITERATIVE_SOLVER_BASE_H 11 #define EIGEN_ITERATIVE_SOLVER_BASE_H 17 template<
typename MatrixType>
18 struct is_ref_compatible_impl
21 template <
typename T0>
24 template <
typename T> any_conversion(
const volatile T&);
25 template <
typename T> any_conversion(T&);
27 struct yes {
int a[1];};
28 struct no {
int a[2];};
31 static yes test(
const Ref<const T>&,
int);
33 static no test(any_conversion<T>, ...);
36 static MatrixType ms_from;
37 enum { value =
sizeof(test<MatrixType>(ms_from, 0))==
sizeof(yes) };
40 template<
typename MatrixType>
41 struct is_ref_compatible
43 enum { value = is_ref_compatible_impl<typename remove_all<MatrixType>::type>::value };
46 template<typename MatrixType, bool MatrixFree = !internal::is_ref_compatible<MatrixType>::value>
47 class generic_matrix_wrapper;
50 template<
typename MatrixType>
51 class generic_matrix_wrapper<MatrixType,false>
54 typedef Ref<const MatrixType> ActualMatrixType;
55 template<
int UpLo>
struct ConstSelfAdjointViewReturnType {
56 typedef typename ActualMatrixType::template ConstSelfAdjointViewReturnType<UpLo>::Type Type;
63 generic_matrix_wrapper()
64 : m_dummy(0,0), m_matrix(m_dummy)
67 template<
typename InputType>
68 generic_matrix_wrapper(
const InputType &mat)
72 const ActualMatrixType& matrix()
const 77 template<
typename MatrixDerived>
78 void grab(
const EigenBase<MatrixDerived> &mat)
80 m_matrix.~Ref<
const MatrixType>();
81 ::new (&m_matrix) Ref<const MatrixType>(mat.derived());
84 void grab(
const Ref<const MatrixType> &mat)
86 if(&(mat.derived()) != &m_matrix)
88 m_matrix.~Ref<
const MatrixType>();
89 ::new (&m_matrix) Ref<const MatrixType>(mat);
95 ActualMatrixType m_matrix;
99 template<
typename MatrixType>
100 class generic_matrix_wrapper<MatrixType,true>
103 typedef MatrixType ActualMatrixType;
104 template<
int UpLo>
struct ConstSelfAdjointViewReturnType
106 typedef ActualMatrixType Type;
113 generic_matrix_wrapper()
117 generic_matrix_wrapper(
const MatrixType &mat)
121 const ActualMatrixType& matrix()
const 126 void grab(
const MatrixType &mat)
132 const ActualMatrixType *mp_matrix;
142 template<
typename Derived>
147 using Base::m_isInitialized;
150 typedef typename internal::traits<Derived>::MatrixType MatrixType;
151 typedef typename internal::traits<Derived>::Preconditioner Preconditioner;
152 typedef typename MatrixType::Scalar Scalar;
153 typedef typename MatrixType::StorageIndex StorageIndex;
154 typedef typename MatrixType::RealScalar RealScalar;
157 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
158 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
181 template<
typename MatrixDerived>
183 : m_matrixWrapper(A.derived())
196 template<
typename MatrixDerived>
200 m_preconditioner.analyzePattern(matrix());
201 m_isInitialized =
true;
202 m_analysisIsOk =
true;
203 m_info = m_preconditioner.info();
216 template<
typename MatrixDerived>
219 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
221 m_preconditioner.factorize(matrix());
222 m_factorizationIsOk =
true;
223 m_info = m_preconditioner.info();
237 template<
typename MatrixDerived>
241 m_preconditioner.compute(matrix());
242 m_isInitialized =
true;
243 m_analysisIsOk =
true;
244 m_factorizationIsOk =
true;
245 m_info = m_preconditioner.info();
250 EIGEN_CONSTEXPR
Index rows()
const EIGEN_NOEXCEPT {
return matrix().rows(); }
253 EIGEN_CONSTEXPR
Index cols()
const EIGEN_NOEXCEPT {
return matrix().cols(); }
267 m_tolerance = tolerance;
283 return (m_maxIterations<0) ? 2*matrix().cols() : m_maxIterations;
291 m_maxIterations = maxIters;
298 eigen_assert(m_isInitialized &&
"ConjugateGradient is not initialized.");
307 eigen_assert(m_isInitialized &&
"ConjugateGradient is not initialized.");
316 template<
typename Rhs,
typename Guess>
320 eigen_assert(m_isInitialized &&
"Solver is not initialized.");
321 eigen_assert(derived().rows()==b.
rows() &&
"solve(): invalid number of rows of the right hand side matrix b");
328 eigen_assert(m_isInitialized &&
"IterativeSolverBase is not initialized.");
333 template<
typename Rhs,
typename DestDerived>
336 eigen_assert(rows()==b.rows());
338 Index rhsCols = b.cols();
339 Index size = b.rows();
340 DestDerived& dest(aDest.
derived());
341 typedef typename DestDerived::Scalar DestScalar;
346 typename DestDerived::PlainObject tmp(cols(),rhsCols);
348 for(
Index k=0; k<rhsCols; ++k)
352 derived()._solve_vector_with_guess_impl(tb,tx);
362 m_info = global_info;
366 template<
typename Rhs,
typename DestDerived>
367 typename internal::enable_if<Rhs::ColsAtCompileTime!=1 && DestDerived::ColsAtCompileTime!=1>::type
370 eigen_assert(rows()==b.rows());
372 Index rhsCols = b.cols();
373 DestDerived& dest(aDest.
derived());
375 for(
Index k=0; k<rhsCols; ++k)
377 typename DestDerived::ColXpr xk(dest,k);
378 typename Rhs::ConstColXpr bk(b,k);
379 derived()._solve_vector_with_guess_impl(bk,xk);
388 m_info = global_info;
391 template<
typename Rhs,
typename DestDerived>
392 typename internal::enable_if<Rhs::ColsAtCompileTime==1 || DestDerived::ColsAtCompileTime==1>::type
395 derived()._solve_vector_with_guess_impl(b,dest.
derived());
399 template<
typename Rhs,
typename Dest>
400 void _solve_impl(
const Rhs& b, Dest& x)
const 403 derived()._solve_with_guess_impl(b,x);
409 m_isInitialized =
false;
410 m_analysisIsOk =
false;
411 m_factorizationIsOk =
false;
412 m_maxIterations = -1;
416 typedef internal::generic_matrix_wrapper<MatrixType> MatrixWrapper;
417 typedef typename MatrixWrapper::ActualMatrixType ActualMatrixType;
419 const ActualMatrixType& matrix()
const 421 return m_matrixWrapper.matrix();
424 template<
typename InputType>
425 void grab(
const InputType &A)
427 m_matrixWrapper.grab(A);
430 MatrixWrapper m_matrixWrapper;
431 Preconditioner m_preconditioner;
433 Index m_maxIterations;
434 RealScalar m_tolerance;
436 mutable RealScalar m_error;
437 mutable Index m_iterations;
439 mutable bool m_analysisIsOk, m_factorizationIsOk;
444 #endif // EIGEN_ITERATIVE_SOLVER_BASE_H ColXpr col(Index i)
Definition: DenseBase.h:1098
Pseudo expression representing a solving operation.
Definition: SolveWithGuess.h:15
Derived & setTolerance(const RealScalar &tolerance)
Definition: IterativeSolverBase.h:265
ComputationInfo info() const
Definition: IterativeSolverBase.h:326
IterativeSolverBase(const EigenBase< MatrixDerived > &A)
Definition: IterativeSolverBase.h:182
A base class for sparse solvers.
Definition: SparseSolverBase.h:67
Namespace containing all symbols from the Eigen library.
Definition: Core:141
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
const Preconditioner & preconditioner() const
Definition: IterativeSolverBase.h:275
Index iterations() const
Definition: IterativeSolverBase.h:296
Derived & derived()
Definition: EigenBase.h:46
Derived & factorize(const EigenBase< MatrixDerived > &A)
Definition: IterativeSolverBase.h:217
RealScalar tolerance() const
Definition: IterativeSolverBase.h:258
Definition: EigenBase.h:29
RealScalar error() const
Definition: IterativeSolverBase.h:305
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:26
Derived & analyzePattern(const EigenBase< MatrixDerived > &A)
Definition: IterativeSolverBase.h:197
Derived & compute(const EigenBase< MatrixDerived > &A)
Definition: IterativeSolverBase.h:238
Definition: Constants.h:444
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const SolveWithGuess< Derived, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
Definition: IterativeSolverBase.h:318
Derived & setMaxIterations(Index maxIters)
Definition: IterativeSolverBase.h:289
Definition: Constants.h:442
Definition: Eigen_Colamd.h:50
const SparseView< Derived > sparseView(const Scalar &m_reference=Scalar(0), const typename NumTraits< Scalar >::Real &m_epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:226
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: EigenBase.h:60
Preconditioner & preconditioner()
Definition: IterativeSolverBase.h:272
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
IterativeSolverBase()
Definition: IterativeSolverBase.h:166
ComputationInfo
Definition: Constants.h:440
Base class for linear iterative solvers.
Definition: IterativeSolverBase.h:143
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Definition: Constants.h:446
Index maxIterations() const
Definition: IterativeSolverBase.h:281