Eigen  5.0.1
 
Loading...
Searching...
No Matches
IncompleteLU.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_INCOMPLETE_LU_H
12#define EIGEN_INCOMPLETE_LU_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19template <typename Scalar_>
20class IncompleteLU : public SparseSolverBase<IncompleteLU<Scalar_> > {
21 protected:
23 using Base::m_isInitialized;
24
25 using Scalar = Scalar_;
26 using Vector = Matrix<Scalar, Dynamic, 1>;
27 using Index = typename Vector::Index;
28 using FactorType = SparseMatrix<Scalar, RowMajor>;
29
30 public:
31 using MatrixType = Matrix<Scalar, Dynamic, Dynamic>;
32
33 IncompleteLU() {}
34
35 template <typename MatrixType>
36 IncompleteLU(const MatrixType& mat) {
37 compute(mat);
38 }
39
40 Index rows() const { return m_lu.rows(); }
41 Index cols() const { return m_lu.cols(); }
42
43 template <typename MatrixType>
44 IncompleteLU& compute(const MatrixType& mat) {
45 m_lu = mat;
46 Index size = mat.cols();
47 Vector diag(size);
48 for (Index i = 0; i < size; ++i) {
49 typename FactorType::InnerIterator k_it(m_lu, i);
50 for (; k_it && k_it.index() < i; ++k_it) {
51 Index k = k_it.index();
52 k_it.valueRef() /= diag(k);
53
54 typename FactorType::InnerIterator j_it(k_it);
55 typename FactorType::InnerIterator kj_it(m_lu, k);
56 while (kj_it && kj_it.index() <= k) ++kj_it;
57 for (++j_it; j_it;) {
58 if (kj_it.index() == j_it.index()) {
59 j_it.valueRef() -= k_it.value() * kj_it.value();
60 ++j_it;
61 ++kj_it;
62 } else if (kj_it.index() < j_it.index())
63 ++kj_it;
64 else
65 ++j_it;
66 }
67 }
68 if (k_it && k_it.index() == i)
69 diag(i) = k_it.value();
70 else
71 diag(i) = 1;
72 }
73 m_isInitialized = true;
74 return *this;
75 }
76
77 template <typename Rhs, typename Dest>
78 void _solve_impl(const Rhs& b, Dest& x) const {
79 x = m_lu.template triangularView<UnitLower>().solve(b);
80 x = m_lu.template triangularView<Upper>().solve(x);
81 }
82
83 protected:
84 FactorType m_lu;
85};
86
87} // end namespace Eigen
88
89#endif // EIGEN_INCOMPLETE_LU_H
A base class for sparse solvers.
Definition SparseSolverBase.h:68