39class DiagonalPreconditioner {
40 typedef Scalar_ Scalar;
44 typedef typename Vector::StorageIndex StorageIndex;
45 enum { ColsAtCompileTime =
Dynamic, MaxColsAtCompileTime =
Dynamic };
47 DiagonalPreconditioner() : m_isInitialized(
false) {}
49 template <
typename MatType>
50 explicit DiagonalPreconditioner(
const MatType& mat) : m_invdiag(mat.cols()) {
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>
58 DiagonalPreconditioner& analyzePattern(
const MatType&) {
62 template <
typename MatType>
63 DiagonalPreconditioner& factorize(
const MatType& mat) {
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>
78 DiagonalPreconditioner& compute(
const MatType& mat) {
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;
121class LeastSquareDiagonalPreconditioner :
public DiagonalPreconditioner<Scalar_> {
122 typedef Scalar_ Scalar;
123 typedef typename NumTraits<Scalar>::Real RealScalar;
124 typedef DiagonalPreconditioner<Scalar_> Base;
125 using Base::m_invdiag;
128 LeastSquareDiagonalPreconditioner() : Base() {}
130 template <
typename MatType>
131 explicit LeastSquareDiagonalPreconditioner(
const MatType& mat) : Base() {
135 template <
typename MatType>
136 LeastSquareDiagonalPreconditioner& analyzePattern(
const MatType&) {
140 template <
typename MatType>
141 LeastSquareDiagonalPreconditioner& factorize(
const MatType& mat) {
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>
165 LeastSquareDiagonalPreconditioner& compute(
const MatType& mat) {
166 return factorize(mat);
Base class for all dense matrices, vectors, and expressions.
Definition ForwardDeclarations.h:73