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 };
119 : m_droptol(NumTraits<Scalar>::dummy_precision()),
121 m_analysisIsOk(
false),
122 m_factorizationIsOk(
false) {}
124 template <
typename MatrixType>
125 explicit IncompleteLUT(
const MatrixType& mat,
const RealScalar& droptol = NumTraits<Scalar>::dummy_precision(),
127 : m_droptol(droptol), m_fillfactor(fillfactor), m_analysisIsOk(
false), m_factorizationIsOk(
false) {
128 eigen_assert(fillfactor != 0);
133 const FactorType
matrixL()
const;
136 const FactorType
matrixU()
const;
138 EIGEN_CONSTEXPR
Index rows()
const EIGEN_NOEXCEPT {
return m_lu.rows(); }
140 EIGEN_CONSTEXPR
Index cols()
const EIGEN_NOEXCEPT {
return m_lu.cols(); }
148 eigen_assert(m_isInitialized &&
"IncompleteLUT is not initialized.");
152 template <
typename MatrixType>
155 template <
typename MatrixType>
163 template <
typename MatrixType>
165 analyzePattern(amat);
173 template <
typename Rhs,
typename Dest>
174 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);
184 inline bool operator()(
const Index& row,
const Index& col,
const Scalar&)
const {
return row != col; }
189 RealScalar m_droptol;
192 bool m_factorizationIsOk;
202template <
typename Scalar,
typename StorageIndex>
204 this->m_droptol = droptol;
211template <
typename Scalar,
typename StorageIndex>
213 this->m_fillfactor = fillfactor;
221template <
typename Scalar,
typename StorageIndex>
223 eigen_assert(m_factorizationIsOk &&
"factorize() should be called first");
224 return m_lu.template triangularView<UnitLower>();
232template <
typename Scalar,
typename StorageIndex>
234 eigen_assert(m_factorizationIsOk &&
"Factorization must be computed first.");
235 return m_lu.template triangularView<Upper>();
238template <
typename Scalar,
typename StorageIndex>
239template <
typename MatrixType_>
240void IncompleteLUT<Scalar, StorageIndex>::analyzePattern(
const MatrixType_& amat) {
252 m_Pinv = m_P.inverse();
253 m_analysisIsOk =
true;
254 m_factorizationIsOk =
false;
255 m_isInitialized =
true;
258template <
typename Scalar,
typename StorageIndex>
259template <
typename MatrixType_>
260void IncompleteLUT<Scalar, StorageIndex>::factorize(
const MatrixType_& amat) {
261 using internal::convert_index;
266 eigen_assert((amat.rows() == amat.cols()) &&
"The factorization should be done on a square matrix");
267 Index n = amat.cols();
275 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
276 SparseMatrix<Scalar, RowMajor, StorageIndex> mat;
277 mat = amat.twistedBy(m_Pinv);
285 Index fill_in = (amat.nonZeros() * m_fillfactor) / n + 1;
286 if (fill_in > n) fill_in = n;
289 Index nnzL = fill_in / 2;
291 m_lu.reserve(n * (nnzL + nnzU + 1));
294 for (Index ii = 0; ii < n; ii++) {
299 ju(ii) = convert_index<StorageIndex>(ii);
301 jr(ii) = convert_index<StorageIndex>(ii);
302 RealScalar rownorm = 0;
304 typename FactorType::InnerIterator j_it(mat, ii);
305 for (; j_it; ++j_it) {
306 Index k = j_it.index();
309 ju(sizel) = convert_index<StorageIndex>(k);
310 u(sizel) = j_it.value();
311 jr(k) = convert_index<StorageIndex>(sizel);
313 }
else if (k == ii) {
314 u(ii) = j_it.value();
317 Index jpos = ii + sizeu;
318 ju(jpos) = convert_index<StorageIndex>(k);
319 u(jpos) = j_it.value();
320 jr(k) = convert_index<StorageIndex>(jpos);
323 rownorm += numext::abs2(j_it.value());
332 rownorm =
sqrt(rownorm);
341 Index minrow = ju.segment(jj, sizel - jj).minCoeff(&k);
343 if (minrow != ju(jj)) {
347 jr(minrow) = convert_index<StorageIndex>(jj);
348 jr(j) = convert_index<StorageIndex>(k);
355 typename FactorType::InnerIterator ki_it(m_lu, minrow);
356 while (ki_it && ki_it.index() < minrow) ++ki_it;
357 eigen_internal_assert(ki_it && ki_it.col() == minrow);
358 Scalar fact = u(jj) / ki_it.value();
361 if (
abs(fact) <= m_droptol) {
368 for (; ki_it; ++ki_it) {
369 Scalar prod = fact * ki_it.value();
370 Index j = ki_it.index();
379 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);
394 ju(len) = convert_index<StorageIndex>(minrow);
401 for (
Index k = 0; k < sizeu; k++) jr(ju(ii + k)) = -1;
407 len = (std::min)(sizel, nnzL);
408 typename Vector::SegmentReturnType ul(u.segment(0, sizel));
409 typename VectorI::SegmentReturnType jul(ju.segment(0, sizel));
410 internal::QuickSplit(ul, jul, len);
414 for (
Index k = 0; k < len; k++) m_lu.insertBackByOuterInnerUnordered(ii, ju(k)) = u(k);
418 if (u(ii) ==
Scalar(0)) u(ii) =
sqrt(m_droptol) * rownorm;
419 m_lu.insertBackByOuterInnerUnordered(ii, ii) = u(ii);
424 for (
Index k = 1; k < sizeu; k++) {
425 if (
abs(u(ii + k)) > m_droptol * rownorm) {
427 u(ii + len) = u(ii + k);
428 ju(ii + len) = ju(ii + k);
432 len = (std::min)(sizeu, nnzU);
433 typename Vector::SegmentReturnType uu(u.segment(ii + 1, sizeu - 1));
434 typename VectorI::SegmentReturnType juu(ju.segment(ii + 1, sizeu - 1));
435 internal::QuickSplit(uu, juu, len);
438 for (
Index k = ii + 1; k < ii + len; k++) m_lu.insertBackByOuterInnerUnordered(ii, ju(k)) = u(k);
441 m_lu.makeCompressed();
443 m_factorizationIsOk =
true;
void setFillfactor(int fillfactor)
Definition IncompleteLUT.h:212
IncompleteLUT & compute(const MatrixType &amat)
Definition IncompleteLUT.h:164
const FactorType matrixL() const
Extraction Method for L-Factor.
Definition IncompleteLUT.h:222
const FactorType matrixU() const
Extraction Method for U-Factor.
Definition IncompleteLUT.h:233
void setDroptol(const RealScalar &droptol)
Definition IncompleteLUT.h:203
ComputationInfo info() const
Reports whether previous computation was successful.
Definition IncompleteLUT.h:147
The matrix class, also used for vectors and row-vectors.
Definition ForwardDeclarations.h:70
Permutation matrix.
Definition ForwardDeclarations.h:136
A versatible sparse matrix representation.
Definition SparseUtil.h:47
SparseSolverBase()
Definition SparseSolverBase.h:70
ComputationInfo
Definition Constants.h:438
@ NumericalIssue
Definition Constants.h:442
@ Success
Definition Constants.h:440
Matrix< Type, Size, 1 > Vector
[c++11] Size×1 vector of type Type.
Definition Matrix.h:522
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)
std::enable_if_t< std::is_base_of< DenseBase< std::decay_t< DerivedA > >, std::decay_t< DerivedA > >::value &&std::is_base_of< DenseBase< std::decay_t< DerivedB > >, std::decay_t< DerivedB > >::value, void > swap(DerivedA &&a, DerivedB &&b)
Definition DenseBase.h:655
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:82
const int Dynamic
Definition Constants.h:25
Definition IncompleteLUT.h:183