Eigen  5.0.1-dev+master
 
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 using StorageIndex = typename FactorType::StorageIndex;
33 static constexpr int ColsAtCompileTime = Dynamic;
34 static constexpr int MaxColsAtCompileTime = Dynamic;
35
36 IncompleteLU() {}
37
38 template <typename MatrixType>
39 IncompleteLU(const MatrixType& mat) {
40 compute(mat);
41 }
42
43 Index rows() const { return m_lu.rows(); }
44 Index cols() const { return m_lu.cols(); }
45
46 template <typename MatrixType>
47 IncompleteLU& analyzePattern(const MatrixType&) {
48 return *this;
49 }
50
51 template <typename MatrixType>
52 IncompleteLU& factorize(const MatrixType& mat) {
53 return compute(mat);
54 }
55
56 template <typename MatrixType>
57 IncompleteLU& compute(const MatrixType& mat) {
58 m_lu = mat;
59 Index size = mat.cols();
60 Vector diag(size);
61 for (Index i = 0; i < size; ++i) {
62 typename FactorType::InnerIterator k_it(m_lu, i);
63 for (; k_it && k_it.index() < i; ++k_it) {
64 Index k = k_it.index();
65 k_it.valueRef() /= diag(k);
66
67 typename FactorType::InnerIterator j_it(k_it);
68 typename FactorType::InnerIterator kj_it(m_lu, k);
69 while (kj_it && kj_it.index() <= k) ++kj_it;
70 for (++j_it; j_it && kj_it;) {
71 if (kj_it.index() == j_it.index()) {
72 j_it.valueRef() -= k_it.value() * kj_it.value();
73 ++j_it;
74 ++kj_it;
75 } else if (kj_it.index() < j_it.index())
76 ++kj_it;
77 else
78 ++j_it;
79 }
80 }
81 if (k_it && k_it.index() == i)
82 diag(i) = k_it.value();
83 else
84 diag(i) = 1;
85 }
86 m_isInitialized = true;
87 return *this;
88 }
89
90 ComputationInfo info() const { return Success; }
91
92 template <typename Rhs, typename Dest>
93 void _solve_impl(const Rhs& b, Dest& x) const {
94 x = m_lu.template triangularView<UnitLower>().solve(b);
95 x = m_lu.template triangularView<Upper>().solve(x);
96 }
97
98 protected:
99 FactorType m_lu;
100};
101
102} // end namespace Eigen
103
104#endif // EIGEN_INCOMPLETE_LU_H
typename internal::traits< Derived >::StorageIndex StorageIndex
Definition SparseMatrixBase.h:45
A base class for sparse solvers.
Definition SparseSolverBase.h:68
ComputationInfo
Definition Constants.h:455
@ Success
Definition Constants.h:457