10#ifndef EIGEN_BASIC_PRECONDITIONERS_H
11#define EIGEN_BASIC_PRECONDITIONERS_H
14#include "./InternalHeaderCheck.h"
38template <
typename Scalar_>
40 typedef Scalar_ Scalar;
45 enum { ColsAtCompileTime =
Dynamic, MaxColsAtCompileTime =
Dynamic };
49 template <
typename MatType>
54 EIGEN_CONSTEXPR
Index rows()
const EIGEN_NOEXCEPT {
return m_invdiag.size(); }
55 EIGEN_CONSTEXPR
Index cols()
const EIGEN_NOEXCEPT {
return m_invdiag.size(); }
57 template <
typename MatType>
62 template <
typename MatType>
64 m_invdiag.
resize(mat.cols());
65 for (
int j = 0; j < mat.outerSize(); ++j) {
66 typename MatType::InnerIterator it(mat, j);
67 while (it && it.index() != j) ++it;
68 if (it && it.index() == j && it.value() != Scalar(0))
69 m_invdiag(j) = Scalar(1) / it.value();
71 m_invdiag(j) = Scalar(1);
73 m_isInitialized =
true;
77 template <
typename MatType>
79 return factorize(mat);
83 template <
typename Rhs,
typename Dest>
84 void _solve_impl(
const Rhs& b, Dest& x)
const {
85 x = m_invdiag.array() * b.array();
88 template <
typename Rhs>
90 eigen_assert(m_isInitialized &&
"DiagonalPreconditioner is not initialized.");
91 eigen_assert(m_invdiag.size() == b.
rows() &&
92 "DiagonalPreconditioner::solve(): invalid number of rows of the right hand side matrix b");
100 bool m_isInitialized;
120template <
typename Scalar_>
122 typedef Scalar_ Scalar;
123 typedef typename NumTraits<Scalar>::Real RealScalar;
125 using Base::m_invdiag;
130 template <
typename MatType>
135 template <
typename MatType>
140 template <
typename MatType>
143 m_invdiag.
resize(mat.cols());
144 if (MatType::IsRowMajor) {
146 for (
Index j = 0; j < mat.outerSize(); ++j) {
147 for (
typename MatType::InnerIterator it(mat, j); it; ++it) m_invdiag(it.index()) += numext::abs2(it.value());
149 for (
Index j = 0; j < mat.cols(); ++j)
150 if (numext::real(m_invdiag(j)) > RealScalar(0)) m_invdiag(j) = RealScalar(1) / numext::real(m_invdiag(j));
152 for (
Index j = 0; j < mat.outerSize(); ++j) {
153 RealScalar sum = mat.col(j).squaredNorm();
154 if (sum > RealScalar(0))
155 m_invdiag(j) = RealScalar(1) / sum;
157 m_invdiag(j) = RealScalar(1);
160 Base::m_isInitialized =
true;
164 template <
typename MatType>
166 return factorize(mat);
185 template <
typename MatrixType>
188 template <
typename MatrixType>
193 template <
typename MatrixType>
198 template <
typename MatrixType>
203 template <
typename Rhs>
204 inline const Rhs& solve(
const Rhs& b)
const {
internal::traits< Derived >::StorageIndex StorageIndex
The type used to store indices.
Definition DenseBase.h:59
Derived & derived()
Definition EigenBase.h:49
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition EigenBase.h:59
A preconditioner based on the digonal entries.
Definition BasicPreconditioners.h:39
A naive preconditioner which approximates any matrix as the identity matrix.
Definition BasicPreconditioners.h:181
Jacobi preconditioner for LeastSquaresConjugateGradient.
Definition BasicPreconditioners.h:121
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
Derived & setZero(Index size)
Definition CwiseNullaryOp.h:563
constexpr void resize(Index rows, Index cols)
Definition PlainObjectBase.h:294
Pseudo expression representing a solving operation.
Definition Solve.h:62
ComputationInfo
Definition Constants.h:438
@ Success
Definition Constants.h:440
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
const int Dynamic
Definition Constants.h:25