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)
33 typedef typename VectorV::RealScalar RealScalar;
43 if (ncut < first || ncut >
last )
return 0;
47 RealScalar abskey =
abs(row(mid));
48 for (
Index j = first + 1; j <=
last; j++) {
49 if (
abs(row(j)) > abskey) {
51 swap(row(mid), row(j));
52 swap(ind(mid), ind(j));
56 swap(row(mid), row(first));
57 swap(ind(mid), ind(first));
59 if (mid > ncut)
last = mid - 1;
60 else if (mid < ncut ) first = mid + 1;
61 }
while (mid != ncut );
100template <
typename Scalar_,
typename StorageIndex_ =
int>
105 using Base::m_isInitialized;
107 typedef Scalar_ Scalar;
108 typedef StorageIndex_ StorageIndex;
123 m_analysisIsOk(
false), m_factorizationIsOk(
false)
126 template<
typename MatrixType>
128 : m_droptol(droptol),m_fillfactor(fillfactor),
129 m_analysisIsOk(
false),m_factorizationIsOk(
false)
131 eigen_assert(fillfactor != 0);
135 EIGEN_CONSTEXPR
Index rows()
const EIGEN_NOEXCEPT {
return m_lu.
rows(); }
137 EIGEN_CONSTEXPR
Index cols()
const EIGEN_NOEXCEPT {
return m_lu.
cols(); }
146 eigen_assert(m_isInitialized &&
"IncompleteLUT is not initialized.");
150 template<
typename MatrixType>
151 void analyzePattern(
const MatrixType& amat);
153 template<
typename MatrixType>
154 void factorize(
const MatrixType& amat);
161 template<
typename MatrixType>
164 analyzePattern(amat);
172 template<
typename Rhs,
typename Dest>
173 void _solve_impl(
const Rhs& b, Dest& x)
const
176 x = m_lu.template triangularView<UnitLower>().solve(x);
177 x = m_lu.template triangularView<Upper>().solve(x);
185 inline bool operator() (
const Index& row,
const Index& col,
const Scalar&)
const
194 RealScalar m_droptol;
197 bool m_factorizationIsOk;
207template<
typename Scalar,
typename StorageIndex>
210 this->m_droptol = droptol;
217template<
typename Scalar,
typename StorageIndex>
220 this->m_fillfactor = fillfactor;
223template <
typename Scalar,
typename StorageIndex>
224template<
typename MatrixType_>
238 m_Pinv = m_P.inverse();
239 m_analysisIsOk =
true;
240 m_factorizationIsOk =
false;
241 m_isInitialized =
true;
244template <
typename Scalar,
typename StorageIndex>
245template<
typename MatrixType_>
246void IncompleteLUT<Scalar,StorageIndex>::factorize(
const MatrixType_& amat)
251 using internal::convert_index;
253 eigen_assert((amat.rows() == amat.cols()) &&
"The factorization should be done on a square matrix");
254 Index n = amat.cols();
262 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
263 SparseMatrix<Scalar,RowMajor, StorageIndex> mat;
264 mat = amat.twistedBy(m_Pinv);
272 Index fill_in = (amat.nonZeros()*m_fillfactor)/n + 1;
273 if (fill_in > n) fill_in = n;
276 Index nnzL = fill_in/2;
278 m_lu.reserve(n * (nnzL + nnzU + 1));
281 for (
Index ii = 0; ii < n; ii++)
287 ju(ii) = convert_index<StorageIndex>(ii);
289 jr(ii) = convert_index<StorageIndex>(ii);
290 RealScalar rownorm = 0;
292 typename FactorType::InnerIterator j_it(mat, ii);
295 Index k = j_it.index();
299 ju(sizel) = convert_index<StorageIndex>(k);
300 u(sizel) = j_it.value();
301 jr(k) = convert_index<StorageIndex>(sizel);
306 u(ii) = j_it.value();
311 Index jpos = ii + sizeu;
312 ju(jpos) = convert_index<StorageIndex>(k);
313 u(jpos) = j_it.value();
314 jr(k) = convert_index<StorageIndex>(jpos);
317 rownorm += numext::abs2(j_it.value());
327 rownorm =
sqrt(rownorm);
337 Index minrow = ju.segment(jj,sizel-jj).minCoeff(&k);
339 if (minrow != ju(jj))
344 jr(minrow) = convert_index<StorageIndex>(jj);
345 jr(j) = convert_index<StorageIndex>(k);
352 typename FactorType::InnerIterator ki_it(m_lu, minrow);
353 while (ki_it && ki_it.index() < minrow) ++ki_it;
354 eigen_internal_assert(ki_it && ki_it.col()==minrow);
355 Scalar fact = u(jj) / ki_it.value();
358 if(
abs(fact) <= m_droptol)
366 for (; ki_it; ++ki_it)
368 Scalar prod = fact * ki_it.value();
369 Index j = ki_it.index();
378 eigen_internal_assert(sizeu<=n);
384 eigen_internal_assert(sizel<=ii);
386 ju(newpos) = convert_index<StorageIndex>(j);
388 jr(j) = convert_index<StorageIndex>(newpos);
395 ju(len) = convert_index<StorageIndex>(minrow);
402 for(
Index k = 0; k <sizeu; k++) jr(ju(ii+k)) = -1;
408 len = (std::min)(sizel, nnzL);
409 typename Vector::SegmentReturnType ul(u.segment(0, sizel));
410 typename VectorI::SegmentReturnType jul(ju.segment(0, sizel));
411 internal::QuickSplit(ul, jul, len);
415 for(
Index k = 0; k < len; k++)
416 m_lu.insertBackByOuterInnerUnordered(ii,ju(k)) = u(k);
420 if (u(ii) == Scalar(0))
421 u(ii) =
sqrt(m_droptol) * rownorm;
422 m_lu.insertBackByOuterInnerUnordered(ii, ii) = u(ii);
427 for(
Index k = 1; k < sizeu; k++)
429 if(
abs(u(ii+k)) > m_droptol * rownorm )
432 u(ii + len) = u(ii + k);
433 ju(ii + len) = ju(ii + k);
437 len = (std::min)(sizeu, nnzU);
438 typename Vector::SegmentReturnType uu(u.segment(ii+1, sizeu-1));
439 typename VectorI::SegmentReturnType juu(ju.segment(ii+1, sizeu-1));
440 internal::QuickSplit(uu, juu, len);
443 for(
Index k = ii + 1; k < ii + len; k++)
444 m_lu.insertBackByOuterInnerUnordered(ii,ju(k)) = u(k);
447 m_lu.makeCompressed();
449 m_factorizationIsOk =
true;
Definition: Ordering.h:53
Incomplete LU factorization with dual-threshold strategy.
Definition: IncompleteLUT.h:102
void setFillfactor(int fillfactor)
Definition: IncompleteLUT.h:218
IncompleteLUT & compute(const MatrixType &amat)
Definition: IncompleteLUT.h:162
void setDroptol(const RealScalar &droptol)
Definition: IncompleteLUT.h:208
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: IncompleteLUT.h:144
Index cols() const
Definition: SparseMatrix.h:142
Index rows() const
Definition: SparseMatrix.h:140
A base class for sparse solvers.
Definition: SparseSolverBase.h:70
static const last_t last
Definition: IndexedViewHelper.h:44
ComputationInfo
Definition: Constants.h:442
@ NumericalIssue
Definition: Constants.h:446
@ Success
Definition: Constants.h:444
Matrix< Type, Size, 1 > Vector
[c++11]
Definition: Matrix.h:539
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
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:59
const int Dynamic
Definition: Constants.h:24
Definition: IncompleteLUT.h:184
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:235