36class DiagonalPreconditioner
38 typedef _Scalar Scalar;
41 typedef typename Vector::StorageIndex StorageIndex;
47 DiagonalPreconditioner() : m_isInitialized(
false) {}
49 template<
typename MatType>
50 explicit DiagonalPreconditioner(
const MatType& mat) : m_invdiag(mat.cols())
55 EIGEN_CONSTEXPR
Index rows()
const EIGEN_NOEXCEPT {
return m_invdiag.size(); }
56 EIGEN_CONSTEXPR
Index cols()
const EIGEN_NOEXCEPT {
return m_invdiag.size(); }
58 template<
typename MatType>
59 DiagonalPreconditioner& analyzePattern(
const MatType& )
64 template<
typename MatType>
65 DiagonalPreconditioner& factorize(
const MatType& mat)
67 m_invdiag.resize(mat.cols());
68 for(
int j=0; j<mat.outerSize(); ++j)
70 typename MatType::InnerIterator it(mat,j);
71 while(it && it.index()!=j) ++it;
72 if(it && it.index()==j && it.value()!=Scalar(0))
73 m_invdiag(j) = Scalar(1)/it.value();
75 m_invdiag(j) = Scalar(1);
77 m_isInitialized =
true;
81 template<
typename MatType>
82 DiagonalPreconditioner& compute(
const MatType& mat)
84 return factorize(mat);
88 template<
typename Rhs,
typename Dest>
89 void _solve_impl(
const Rhs& b, Dest& x)
const
91 x = m_invdiag.array() * b.array() ;
97 eigen_assert(m_isInitialized &&
"DiagonalPreconditioner is not initialized.");
98 eigen_assert(m_invdiag.size()==b.
rows()
99 &&
"DiagonalPreconditioner::solve(): invalid number of rows of the right hand side matrix b");
107 bool m_isInitialized;
128class LeastSquareDiagonalPreconditioner :
public DiagonalPreconditioner<_Scalar>
130 typedef _Scalar Scalar;
131 typedef typename NumTraits<Scalar>::Real RealScalar;
132 typedef DiagonalPreconditioner<_Scalar> Base;
133 using Base::m_invdiag;
136 LeastSquareDiagonalPreconditioner() : Base() {}
138 template<
typename MatType>
139 explicit LeastSquareDiagonalPreconditioner(
const MatType& mat) : Base()
144 template<
typename MatType>
145 LeastSquareDiagonalPreconditioner& analyzePattern(
const MatType& )
150 template<
typename MatType>
151 LeastSquareDiagonalPreconditioner& factorize(
const MatType& mat)
154 m_invdiag.resize(mat.cols());
155 if(MatType::IsRowMajor)
158 for(
Index j=0; j<mat.outerSize(); ++j)
160 for(
typename MatType::InnerIterator it(mat,j); it; ++it)
161 m_invdiag(it.index()) += numext::abs2(it.value());
163 for(
Index j=0; j<mat.cols(); ++j)
164 if(numext::real(m_invdiag(j))>RealScalar(0))
165 m_invdiag(j) = RealScalar(1)/numext::real(m_invdiag(j));
169 for(
Index j=0; j<mat.outerSize(); ++j)
171 RealScalar sum = mat.col(j).squaredNorm();
172 if(sum>RealScalar(0))
173 m_invdiag(j) = RealScalar(1)/sum;
175 m_invdiag(j) = RealScalar(1);
178 Base::m_isInitialized =
true;
182 template<
typename MatType>
183 LeastSquareDiagonalPreconditioner& compute(
const MatType& mat)
185 return factorize(mat);