11#ifndef EIGEN_INCOMPLETE_LUT_H
12#define EIGEN_INCOMPLETE_LUT_H
15#include "./InternalHeaderCheck.h"
30template <
typename VectorV,
typename VectorI>
31Index QuickSplit(VectorV& row, VectorI& ind, Index ncut) {
32 typedef typename VectorV::RealScalar RealScalar;
42 if (ncut < first || ncut > last)
return 0;
46 RealScalar abskey =
abs(row(mid));
47 for (Index j = first + 1; j <= last; j++) {
48 if (
abs(row(j)) > abskey) {
50 swap(row(mid), row(j));
51 swap(ind(mid), ind(j));
55 swap(row(mid), row(first));
56 swap(ind(mid), ind(first));
62 }
while (mid != ncut);
101template <
typename Scalar_,
typename StorageIndex_ =
int>
105 using Base::m_isInitialized;
108 typedef Scalar_ Scalar;
109 typedef StorageIndex_ StorageIndex;
110 typedef typename NumTraits<Scalar>::Real RealScalar;
115 enum { ColsAtCompileTime =
Dynamic, MaxColsAtCompileTime =
Dynamic };
121 m_analysisIsOk(
false),
122 m_factorizationIsOk(
false) {}
124 template <
typename MatrixType>
127 : m_droptol(droptol), m_fillfactor(fillfactor), m_analysisIsOk(
false), m_factorizationIsOk(
false) {
128 eigen_assert(fillfactor != 0);
132 EIGEN_CONSTEXPR
Index rows()
const EIGEN_NOEXCEPT {
return m_lu.
rows(); }
134 EIGEN_CONSTEXPR
Index cols()
const EIGEN_NOEXCEPT {
return m_lu.
cols(); }
142 eigen_assert(m_isInitialized &&
"IncompleteLUT is not initialized.");
146 template <
typename MatrixType>
147 void analyzePattern(
const MatrixType& amat);
149 template <
typename MatrixType>
150 void factorize(
const MatrixType& amat);
157 template <
typename MatrixType>
159 analyzePattern(amat);
167 template <
typename Rhs,
typename Dest>
168 void _solve_impl(
const Rhs& b, Dest& x)
const {
170 x = m_lu.template triangularView<UnitLower>().solve(x);
171 x = m_lu.template triangularView<Upper>().solve(x);
178 inline bool operator()(
const Index& row,
const Index& col,
const Scalar&)
const {
return row != col; }
183 RealScalar m_droptol;
186 bool m_factorizationIsOk;
196template <
typename Scalar,
typename StorageIndex>
198 this->m_droptol = droptol;
205template <
typename Scalar,
typename StorageIndex>
207 this->m_fillfactor = fillfactor;
210template <
typename Scalar,
typename StorageIndex>
211template <
typename MatrixType_>
224 m_Pinv = m_P.inverse();
225 m_analysisIsOk =
true;
226 m_factorizationIsOk =
false;
227 m_isInitialized =
true;
230template <
typename Scalar,
typename StorageIndex>
231template <
typename MatrixType_>
232void IncompleteLUT<Scalar, StorageIndex>::factorize(
const MatrixType_& amat) {
233 using internal::convert_index;
238 eigen_assert((amat.rows() == amat.cols()) &&
"The factorization should be done on a square matrix");
239 Index n = amat.cols();
247 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
248 SparseMatrix<Scalar, RowMajor, StorageIndex> mat;
249 mat = amat.twistedBy(m_Pinv);
257 Index fill_in = (amat.nonZeros() * m_fillfactor) / n + 1;
258 if (fill_in > n) fill_in = n;
261 Index nnzL = fill_in / 2;
263 m_lu.reserve(n * (nnzL + nnzU + 1));
266 for (Index ii = 0; ii < n; ii++) {
271 ju(ii) = convert_index<StorageIndex>(ii);
273 jr(ii) = convert_index<StorageIndex>(ii);
274 RealScalar rownorm = 0;
276 typename FactorType::InnerIterator j_it(mat, ii);
277 for (; j_it; ++j_it) {
278 Index k = j_it.index();
281 ju(sizel) = convert_index<StorageIndex>(k);
282 u(sizel) = j_it.value();
283 jr(k) = convert_index<StorageIndex>(sizel);
285 }
else if (k == ii) {
286 u(ii) = j_it.value();
289 Index jpos = ii + sizeu;
290 ju(jpos) = convert_index<StorageIndex>(k);
291 u(jpos) = j_it.value();
292 jr(k) = convert_index<StorageIndex>(jpos);
295 rownorm += numext::abs2(j_it.value());
304 rownorm = sqrt(rownorm);
313 Index minrow = ju.segment(jj, sizel - jj).minCoeff(&k);
315 if (minrow != ju(jj)) {
319 jr(minrow) = convert_index<StorageIndex>(jj);
320 jr(j) = convert_index<StorageIndex>(k);
327 typename FactorType::InnerIterator ki_it(m_lu, minrow);
328 while (ki_it && ki_it.index() < minrow) ++ki_it;
329 eigen_internal_assert(ki_it && ki_it.col() == minrow);
330 Scalar fact = u(jj) / ki_it.value();
333 if (abs(fact) <= m_droptol) {
340 for (; ki_it; ++ki_it) {
341 Scalar prod = fact * ki_it.value();
342 Index j = ki_it.index();
351 eigen_internal_assert(sizeu <= n);
356 eigen_internal_assert(sizel <= ii);
358 ju(newpos) = convert_index<StorageIndex>(j);
360 jr(j) = convert_index<StorageIndex>(newpos);
366 ju(len) = convert_index<StorageIndex>(minrow);
373 for (Index k = 0; k < sizeu; k++) jr(ju(ii + k)) = -1;
379 len = (std::min)(sizel, nnzL);
380 typename Vector::SegmentReturnType ul(u.segment(0, sizel));
381 typename VectorI::SegmentReturnType jul(ju.segment(0, sizel));
382 internal::QuickSplit(ul, jul, len);
386 for (Index k = 0; k < len; k++) m_lu.insertBackByOuterInnerUnordered(ii, ju(k)) = u(k);
390 if (u(ii) == Scalar(0)) u(ii) = sqrt(m_droptol) * rownorm;
391 m_lu.insertBackByOuterInnerUnordered(ii, ii) = u(ii);
396 for (Index k = 1; k < sizeu; k++) {
397 if (abs(u(ii + k)) > m_droptol * rownorm) {
399 u(ii + len) = u(ii + k);
400 ju(ii + len) = ju(ii + k);
404 len = (std::min)(sizeu, nnzU);
405 typename Vector::SegmentReturnType uu(u.segment(ii + 1, sizeu - 1));
406 typename VectorI::SegmentReturnType juu(ju.segment(ii + 1, sizeu - 1));
407 internal::QuickSplit(uu, juu, len);
410 for (Index k = ii + 1; k < ii + len; k++) m_lu.insertBackByOuterInnerUnordered(ii, ju(k)) = u(k);
413 m_lu.makeCompressed();
415 m_factorizationIsOk =
true;
Incomplete LU factorization with dual-threshold strategy.
Definition IncompleteLUT.h:102
void setFillfactor(int fillfactor)
Definition IncompleteLUT.h:206
IncompleteLUT & compute(const MatrixType &amat)
Definition IncompleteLUT.h:158
void setDroptol(const RealScalar &droptol)
Definition IncompleteLUT.h:197
ComputationInfo info() const
Reports whether previous computation was successful.
Definition IncompleteLUT.h:141
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
Permutation matrix.
Definition PermutationMatrix.h:280
A versatible sparse matrix representation.
Definition SparseUtil.h:47
Index cols() const
Definition SparseMatrix.h:161
Index rows() const
Definition SparseMatrix.h:159
A base class for sparse solvers.
Definition SparseSolverBase.h:67
ComputationInfo
Definition Constants.h:438
@ NumericalIssue
Definition Constants.h:442
@ Success
Definition Constants.h:440
Namespace containing all symbols from the Eigen library.
Definition Core:137
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
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
Definition IncompleteLUT.h:177
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition Meta.h:523