10 #ifndef EIGEN_INCOMPLETE_CHOlESKY_H 11 #define EIGEN_INCOMPLETE_CHOlESKY_H 12 #include "Eigen/src/IterativeLinearSolvers/IncompleteLUT.h" 13 #include <Eigen/OrderingMethods> 29 template <
typename Scalar,
int _UpLo = Lower,
typename _OrderingType = NaturalOrdering<
int> >
34 typedef _OrderingType OrderingType;
36 typedef typename MatrixType::Index Index;
40 typedef std::vector<std::list<Index> > VectorList;
41 enum { UpLo = _UpLo };
49 Index rows()
const {
return m_L.
rows(); }
51 Index cols()
const {
return m_L.
cols(); }
61 eigen_assert(m_isInitialized &&
"IncompleteLLT is not initialized.");
68 void setShift( Scalar shift) { m_shift = shift; }
73 template<
typename MatrixType>
77 ord(mat.template selfadjointView<UpLo>(), m_perm);
78 m_analysisIsOk =
true;
81 template<
typename MatrixType>
82 void factorize(
const MatrixType& amat);
84 template<
typename MatrixType>
85 void compute (
const MatrixType& matrix)
91 template<
typename Rhs,
typename Dest>
92 void _solve(
const Rhs& b, Dest& x)
const 94 eigen_assert(m_factorizationIsOk &&
"factorize() should be called first");
95 if (m_perm.
rows() == b.rows())
99 x = m_scal.asDiagonal() * x;
100 x = m_L.template triangularView<UnitLower>().solve(x);
101 x = m_L.adjoint().template triangularView<Upper>().solve(x);
102 if (m_perm.
rows() == b.rows())
104 x = m_scal.asDiagonal() * x;
109 eigen_assert(m_factorizationIsOk &&
"IncompleteLLT did not succeed");
110 eigen_assert(m_isInitialized &&
"IncompleteLLT is not initialized.");
111 eigen_assert(cols()==b.rows()
112 &&
"IncompleteLLT::solve(): invalid number of rows of the right hand side matrix b");
120 bool m_factorizationIsOk;
121 bool m_isInitialized;
123 PermutationType m_perm;
126 template <
typename IdxType,
typename SclType>
127 inline void updateList(
const IdxType& colPtr, IdxType& rowIdx, SclType& vals,
const Index& col,
const Index& jk, IndexType& firstElt, VectorList& listCol);
130 template<
typename Scalar,
int _UpLo,
typename OrderingType>
131 template<
typename _MatrixType>
136 eigen_assert(m_analysisIsOk &&
"analyzePattern() should be called first");
141 if (m_perm.
rows() == mat.rows() )
142 m_L.template selfadjointView<Lower>() = mat.template selfadjointView<_UpLo>().twistedBy(m_perm);
144 m_L.template selfadjointView<Lower>() = mat.template selfadjointView<_UpLo>();
146 Index n = m_L.
cols();
152 VectorList listCol(n);
159 for (
int j = 0; j < n; j++)
161 m_scal(j) = m_L.
col(j).norm();
162 m_scal(j) = sqrt(m_scal(j));
165 Scalar mindiag = vals[0];
166 for (
int j = 0; j < n; j++){
167 for (
int k = colPtr[j]; k < colPtr[j+1]; k++)
168 vals[k] /= (m_scal(j) * m_scal(rowIdx[k]));
169 mindiag = (min)(vals[colPtr[j]], mindiag);
172 if(mindiag <
Scalar(0.)) m_shift = m_shift - mindiag;
174 for (
int j = 0; j < n; j++)
175 vals[colPtr[j]] += m_shift;
177 for (
int j=0; j < n; ++j)
181 Scalar diag = vals[colPtr[j]];
183 irow.setLinSpaced(n,0,n-1);
184 for (
int i = colPtr[j] + 1; i < colPtr[j+1]; i++)
186 curCol(rowIdx[i]) = vals[i];
187 irow(rowIdx[i]) = rowIdx[i];
189 std::list<int>::iterator k;
191 for(k = listCol[j].begin(); k != listCol[j].end(); k++)
193 int jk = firstElt(*k);
195 for (
int i = jk; i < colPtr[*k+1]; i++)
197 curCol(rowIdx[i]) -= vals[i] * vals[jk] ;
199 updateList(colPtr,rowIdx,vals, *k, jk, firstElt, listCol);
203 if(RealScalar(diag) <= 0)
205 std::cerr <<
"\nNegative diagonal during Incomplete factorization... "<< j <<
"\n";
209 RealScalar rdiag = sqrt(RealScalar(diag));
210 vals[colPtr[j]] = rdiag;
211 for (
int i = j+1; i < n; i++)
216 vals[colPtr[i]] -= curCol(i) * curCol(i);
220 int p = colPtr[j+1] - colPtr[j] - 1 ;
221 internal::QuickSplit(curCol, irow, p);
224 for (
int i = colPtr[j]+1; i < colPtr[j+1]; i++)
226 vals[i] = curCol(cpt);
227 rowIdx[i] = irow(cpt);
231 Index jk = colPtr(j)+1;
232 updateList(colPtr,rowIdx,vals,j,jk,firstElt,listCol);
234 m_factorizationIsOk =
true;
235 m_isInitialized =
true;
239 template<
typename Scalar,
int _UpLo,
typename OrderingType>
240 template <
typename IdxType,
typename SclType>
243 if (jk < colPtr(col+1) )
245 Index p = colPtr(col+1) - jk;
247 rowIdx.segment(jk,p).minCoeff(&minpos);
249 if (rowIdx(minpos) != rowIdx(jk))
252 std::swap(rowIdx(jk),rowIdx(minpos));
253 std::swap(vals(jk),vals(minpos));
256 listCol[rowIdx(jk)].push_back(col);
261 template<
typename _Scalar,
int _UpLo,
typename OrderingType,
typename Rhs>
266 EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
268 template<
typename Dest>
void evalTo(Dest& dst)
const 270 dec()._solve(rhs(),dst);
Definition: ForwardDeclarations.h:124
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: IncompleteCholesky.h:59
const Scalar * valuePtr() const
Definition: SparseMatrix.h:131
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:104
Modified Incomplete Cholesky with dual threshold.
Definition: IncompleteCholesky.h:30
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: TestIMU_Common.h:87
void setShift(Scalar shift)
Set the initial shift parameter.
Definition: IncompleteCholesky.h:68
Index cols() const
Definition: SparseMatrix.h:121
const Index * outerIndexPtr() const
Definition: SparseMatrix.h:149
Index nonZeros() const
Definition: SparseMatrix.h:246
The provided data did not satisfy the prerequisites.
Definition: Constants.h:378
EIGEN_STRONG_INLINE void resize(Index nbRows, Index nbCols)
Resizes *this to a rows x cols matrix.
Definition: PlainObjectBase.h:235
Index rows() const
Definition: SparseMatrix.h:119
Derived & setZero(Index size)
Resizes to the given size, and sets all coefficients in this expression to zero.
Definition: CwiseNullaryOp.h:515
Computation was successful.
Definition: Constants.h:376
void analyzePattern(const MatrixType &mat)
Computes the fill reducing permutation vector.
Definition: IncompleteCholesky.h:74
Definition: BandTriangularSolver.h:13
const Index * innerIndexPtr() const
Definition: SparseMatrix.h:140
Transpose< PermutationBase > inverse() const
Definition: PermutationMatrix.h:201
Definition: ForwardDeclarations.h:125
ColXpr col(Index i)
Definition: SparseMatrixBase.h:733
NumTraits< Scalar >::Real RealScalar
This is the "real scalar" type; if the Scalar type is already real numbers (e.g.
Definition: SparseMatrixBase.h:124
ComputationInfo
Enum for reporting the status of a computation.
Definition: Constants.h:374
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
double Scalar
Common scalar type.
Definition: FlexibleKalmanBase.h:48
Index rows() const
Definition: PermutationMatrix.h:108