11 #ifndef EIGEN_INCOMPLETE_LUT_H 12 #define EIGEN_INCOMPLETE_LUT_H 15 #include "./InternalHeaderCheck.h" 30 template <
typename VectorV,
typename VectorI>
31 Index 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);
101 template <
typename Scalar_,
typename StorageIndex_ =
int>
105 using Base::m_isInitialized;
108 typedef Scalar_ Scalar;
109 typedef StorageIndex_ StorageIndex;
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);
138 constexpr
Index rows()
const noexcept {
return m_lu.
rows(); }
140 constexpr
Index cols()
const noexcept {
return m_lu.
cols(); }
148 eigen_assert(m_isInitialized &&
"IncompleteLUT is not initialized.");
152 template <
typename MatrixType>
153 void analyzePattern(
const MatrixType& amat);
155 template <
typename MatrixType>
156 void factorize(
const MatrixType& amat);
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;
202 template <
typename Scalar,
typename StorageIndex>
204 this->m_droptol = droptol;
211 template <
typename Scalar,
typename StorageIndex>
213 this->m_fillfactor = fillfactor;
221 template <
typename Scalar,
typename StorageIndex>
223 eigen_assert(m_factorizationIsOk &&
"factorize() should be called first");
224 return m_lu.template triangularView<UnitLower>();
232 template <
typename Scalar,
typename StorageIndex>
234 eigen_assert(m_factorizationIsOk &&
"Factorization must be computed first.");
235 return m_lu.template triangularView<Upper>();
238 template <
typename Scalar,
typename StorageIndex>
239 template <
typename MatrixType_>
252 m_Pinv = m_P.inverse();
253 m_analysisIsOk =
true;
254 m_factorizationIsOk =
false;
255 m_isInitialized =
true;
258 template <
typename Scalar,
typename StorageIndex>
259 template <
typename MatrixType_>
260 void 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;
449 #endif // EIGEN_INCOMPLETE_LUT_H void setDroptol(const RealScalar &droptol)
Definition: IncompleteLUT.h:203
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
Index rows() const
Definition: SparseMatrix.h:159
A base class for sparse solvers.
Definition: SparseSolverBase.h:67
const Solve< IncompleteLUT< Scalar_, StorageIndex_ >, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: SparseSolverBase.h:84
Definition: Ordering.h:48
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
const FactorType matrixU() const
Extraction Method for U-Factor.
Definition: IncompleteLUT.h:233
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
Matrix< Type, Size, 1 > Vector
[c++11] Size×1 vector of type Type.
Definition: Matrix.h:522
Definition: IncompleteLUT.h:183
Definition: Constants.h:442
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
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:667
Incomplete LU factorization with dual-threshold strategy.
Definition: IncompleteLUT.h:102
Definition: Constants.h:440
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
Index cols() const
Definition: SparseMatrix.h:161
IncompleteLUT & compute(const MatrixType &amat)
Definition: IncompleteLUT.h:164
const int Dynamic
Definition: Constants.h:25
ComputationInfo
Definition: Constants.h:438
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: IncompleteLUT.h:147
static constexpr const last_t last
Definition: IndexedViewHelper.h:48
const FactorType matrixL() const
Extraction Method for L-Factor.
Definition: IncompleteLUT.h:222
void setFillfactor(int fillfactor)
Definition: IncompleteLUT.h:212