11#ifndef EIGEN_ITERATIVE_SOLVER_BASE_H
12#define EIGEN_ITERATIVE_SOLVER_BASE_H
15#include "./InternalHeaderCheck.h"
22auto is_ref_compatible_test(T& matrix) ->
decltype(Ref<const T>(matrix), std::true_type());
23std::false_type is_ref_compatible_test(...);
25template <
typename MatrixType>
26using is_ref_compatible =
decltype(is_ref_compatible_test(std::declval<remove_all_t<MatrixType>&>()));
31template <
typename MatrixType>
32MatrixType random_orthonormal_basis(Index rows, Index cols) {
33 HouseholderQR<MatrixType> qr(MatrixType::Random(rows, cols));
34 return qr.householderQ() * MatrixType::Identity(rows, cols);
45template <
typename RealScalar>
46EIGEN_DEVICE_FUNC RealScalar iterative_solver_scaling_factor(RealScalar norm) {
50 const RealScalar sqrtMin = numext::sqrt((std::numeric_limits<RealScalar>::min)());
51 const RealScalar sqrtMax = numext::sqrt((std::numeric_limits<RealScalar>::max)());
52 return norm < sqrtMin / NumTraits<RealScalar>::epsilon() || norm > sqrtMax ? norm : RealScalar(1);
61template <
typename Rhs,
typename Dest,
typename Solve>
62auto iterative_solver_on_scaled_problem(
const Rhs& b, Dest& x, Solve solve) ->
decltype(solve(b, x)) {
63 using RealScalar =
typename Dest::RealScalar;
64 using VectorType = Matrix<typename Dest::Scalar, Dynamic, 1>;
65 const RealScalar normb = b.stableNorm();
66 if (normb > RealScalar(0) && (numext::isfinite)(normb)) {
67 const RealScalar scale = iterative_solver_scaling_factor(normb);
68 if (scale != RealScalar(1)) {
69 VectorType scaledGuess = x / scale;
70 if (scaledGuess.allFinite()) {
71 const VectorType scaledRhs = b / scale;
72 const auto ret = solve(scaledRhs, scaledGuess);
73 x = scaledGuess * scale;
83template <
typename RealScalar>
84EIGEN_DEVICE_FUNC RealScalar iterative_solver_residual_floor() {
85 return numext::sqrt((std::numeric_limits<RealScalar>::min)());
93template <
typename RealScalar>
94EIGEN_DEVICE_FUNC RealScalar iterative_solver_threshold(RealScalar tol, RealScalar norm, RealScalar floor) {
95 return numext::maxi(RealScalar(tol * norm), numext::mini(floor, RealScalar(NumTraits<RealScalar>::epsilon() * norm)));
98template <typename MatrixType, bool MatrixFree = !internal::is_ref_compatible<MatrixType>::value>
99class generic_matrix_wrapper;
102template <
typename MatrixType>
103class generic_matrix_wrapper<MatrixType, false> {
105 using ActualMatrixType = Ref<const MatrixType>;
107 struct ConstSelfAdjointViewReturnType {
108 using Type =
typename ActualMatrixType::template ConstSelfAdjointViewReturnType<UpLo>::Type;
111 enum { MatrixFree =
false };
114 generic_matrix_wrapper() : m_dummy(), m_matrix(m_dummy) {}
116 template <
typename InputType>
117 generic_matrix_wrapper(
const InputType& mat) : m_matrix(mat) {}
119 const ActualMatrixType& matrix()
const {
return m_matrix; }
121 bool hasMatrix()
const {
return true; }
123 template <
typename MatrixDerived>
124 void grab(
const EigenBase<MatrixDerived>& mat) {
125 internal::destroy_at(&m_matrix);
126 internal::construct_at(&m_matrix, mat.derived());
129 void grab(
const Ref<const MatrixType>& mat) {
130 if (&(mat.derived()) != &m_matrix) {
131 internal::destroy_at(&m_matrix);
132 internal::construct_at(&m_matrix, mat);
138 ActualMatrixType m_matrix;
142template <
typename MatrixType>
143class generic_matrix_wrapper<MatrixType, true> {
145 using ActualMatrixType = MatrixType;
147 struct ConstSelfAdjointViewReturnType {
148 using Type = ActualMatrixType;
151 enum { MatrixFree =
true };
153 generic_matrix_wrapper() =
default;
155 generic_matrix_wrapper(
const MatrixType& mat) : mp_matrix(&mat) {}
157 const ActualMatrixType& matrix()
const {
return *mp_matrix; }
160 bool hasMatrix()
const {
return mp_matrix !=
nullptr; }
162 void grab(
const MatrixType& mat) { mp_matrix = &mat; }
165 const ActualMatrixType* mp_matrix =
nullptr;
175template <
typename Derived>
179 using Base::m_isInitialized;
182 using MatrixType =
typename internal::traits<Derived>::MatrixType;
183 using Preconditioner =
typename internal::traits<Derived>::Preconditioner;
184 using Scalar =
typename MatrixType::Scalar;
185 using StorageIndex =
typename MatrixType::StorageIndex;
186 using RealScalar =
typename MatrixType::RealScalar;
188 enum { ColsAtCompileTime = MatrixType::ColsAtCompileTime, MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime };
206 template <
typename MatrixDerived>
219 template <
typename MatrixDerived>
222 m_preconditioner.analyzePattern(matrix());
223 m_isInitialized =
true;
224 m_analysisIsOk =
true;
225 m_info = m_preconditioner.info();
239 template <
typename MatrixDerived>
241 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
243 m_preconditioner.factorize(matrix());
244 m_factorizationIsOk =
true;
245 m_info = m_preconditioner.info();
259 template <
typename MatrixDerived>
262 m_preconditioner.compute(matrix());
263 m_isInitialized =
true;
264 m_analysisIsOk =
true;
265 m_factorizationIsOk =
true;
266 m_info = m_preconditioner.info();
271 constexpr Index rows() const noexcept {
return matrix().rows(); }
274 constexpr Index cols() const noexcept {
return matrix().cols(); }
307 Index
maxIterations()
const {
return (m_maxIterations < 0) ? 2 * attachedCols() : m_maxIterations; }
313 m_maxIterations = maxIters;
319 eigen_assert(m_isInitialized &&
"IterativeSolverBase is not initialized.");
329 eigen_assert(m_isInitialized &&
"IterativeSolverBase is not initialized.");
338 template <
typename Rhs,
typename Guess>
340 eigen_assert(m_isInitialized &&
"Solver is not initialized.");
341 EIGEN_INTERNAL_ASSERT_SOLVER_RHS_MSG(derived(), b,
false,
342 "solve(): invalid number of rows of the right hand side matrix b");
356 template <
typename Rhs,
typename Dest>
358 eigen_assert(m_isInitialized &&
"Solver is not initialized.");
359 EIGEN_INTERNAL_ASSERT_SOLVER_RHS_MSG(derived(), b,
false,
360 "solveWithGuessInPlace(): invalid number of rows of the right hand side b");
361 derived()._solve_vector_with_guess_impl(b, x);
369 eigen_assert(m_isInitialized &&
"IterativeSolverBase is not initialized.");
374 template <
typename Rhs,
typename DestDerived>
376 EIGEN_INTERNAL_ASSERT_SOLVER_RHS(derived(), b,
false);
378 Index rhsCols = b.cols();
379 Index size = b.rows();
380 DestDerived& dest(aDest.
derived());
381 using DestScalar =
typename DestDerived::Scalar;
387 typename DestDerived::PlainObject tmp(cols(), rhsCols);
389 for (Index k = 0; k < rhsCols; ++k) {
392 derived()._solve_vector_with_guess_impl(tb, tx);
393 tmp.col(k) = tx.sparseView(0);
396 global_info = internal::most_severe_info(global_info, m_info);
398 m_info = global_info;
402 template <
typename Rhs,
typename DestDerived>
403 std::enable_if_t<Rhs::ColsAtCompileTime != 1 && DestDerived::ColsAtCompileTime != 1> _solve_with_guess_impl(
404 const Rhs& b, MatrixBase<DestDerived>& aDest)
const {
405 EIGEN_INTERNAL_ASSERT_SOLVER_RHS(derived(), b,
false);
407 Index rhsCols = b.cols();
408 DestDerived& dest(aDest.derived());
410 for (Index k = 0; k < rhsCols; ++k) {
411 typename DestDerived::ColXpr xk(dest, k);
412 typename Rhs::ConstColXpr bk(b, k);
413 derived()._solve_vector_with_guess_impl(bk, xk);
416 global_info = internal::most_severe_info(global_info, m_info);
418 m_info = global_info;
421 template <
typename Rhs,
typename DestDerived>
422 std::enable_if_t<Rhs::ColsAtCompileTime == 1 || DestDerived::ColsAtCompileTime == 1> _solve_with_guess_impl(
423 const Rhs& b, MatrixBase<DestDerived>& dest)
const {
424 derived()._solve_vector_with_guess_impl(b, dest.derived());
428 template <
typename Rhs,
typename Dest>
429 void _solve_impl(
const Rhs& b, Dest& x)
const {
431 derived()._solve_with_guess_impl(b, x);
436 m_isInitialized =
false;
437 m_analysisIsOk =
false;
438 m_factorizationIsOk =
false;
439 m_maxIterations = -1;
440 m_tolerance = NumTraits<Scalar>::epsilon();
443 using MatrixWrapper = internal::generic_matrix_wrapper<MatrixType>;
444 using ActualMatrixType =
typename MatrixWrapper::ActualMatrixType;
446 const ActualMatrixType& matrix()
const {
return m_matrixWrapper.matrix(); }
449 Index attachedCols()
const {
return m_matrixWrapper.hasMatrix() ? matrix().cols() : Index(0); }
453 RealScalar sizeScaledTolerance()
const {
454 return RealScalar(8) * numext::sqrt(RealScalar(numext::maxi(attachedCols(), Index(1)))) *
455 NumTraits<RealScalar>::epsilon();
460 RealScalar toleranceOrEps()
const {
461 return m_tolerance >= RealScalar(0) ? m_tolerance : NumTraits<RealScalar>::epsilon();
469 const bool confirmed =
error <= tol || (m_tolerance < RealScalar(0) &&
error <= sizeScaledTolerance());
473 template <
typename InputType>
474 void grab(
const InputType& A) {
475 m_matrixWrapper.grab(A);
478 MatrixWrapper m_matrixWrapper;
479 Preconditioner m_preconditioner;
481 Index m_maxIterations;
482 RealScalar m_tolerance;
484 mutable RealScalar m_error;
485 mutable Index m_iterations;
487 mutable bool m_analysisIsOk, m_factorizationIsOk;
Base class for linear iterative solvers.
Definition IterativeSolverBase.h:176
IterativeSolverBase()
Definition IterativeSolverBase.h:194
ComputationInfo info() const
Definition IterativeSolverBase.h:368
RealScalar error() const
Definition IterativeSolverBase.h:328
Index maxIterations() const
Definition IterativeSolverBase.h:307
Derived & setMaxIterations(Index maxIters)
Definition IterativeSolverBase.h:312
Derived & compute(const EigenBase< MatrixDerived > &A)
Definition IterativeSolverBase.h:260
IterativeSolverBase(const EigenBase< MatrixDerived > &A)
Definition IterativeSolverBase.h:207
void solveWithGuessInPlace(const Rhs &b, Dest &x) const
Definition IterativeSolverBase.h:357
const Preconditioner & preconditioner() const
Definition IterativeSolverBase.h:301
Derived & analyzePattern(const EigenBase< MatrixDerived > &A)
Definition IterativeSolverBase.h:220
Derived & factorize(const EigenBase< MatrixDerived > &A)
Definition IterativeSolverBase.h:240
Preconditioner & preconditioner()
Definition IterativeSolverBase.h:298
Derived & setTolerance(const RealScalar &tolerance)
Definition IterativeSolverBase.h:292
RealScalar tolerance() const
Definition IterativeSolverBase.h:279
const SolveWithGuess< Derived, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
Definition IterativeSolverBase.h:339
Index iterations() const
Definition IterativeSolverBase.h:318
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
Pseudo expression representing a solving operation.
Definition SolveWithGuess.h:44
Base class of any sparse matrices or sparse expressions.
Definition SparseMatrixBase.h:31
SparseSolverBase()=default
ComputationInfo
Definition Constants.h:455
@ NumericalIssue
Definition Constants.h:459
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34
constexpr Derived & derived()
Definition EigenBase.h:50