10 #ifndef EIGEN_SKYLINEINPLACELU_H
11 #define EIGEN_SKYLINEINPLACELU_H
13 #include "./InternalHeaderCheck.h"
26 template<
typename MatrixType>
29 typedef typename MatrixType::Scalar Scalar;
30 typedef typename MatrixType::Index Index;
39 : m_flags(
flags), m_status(0), m_lu(matrix) {
40 m_precision = RealScalar(0.1) * Eigen::dummy_precision<RealScalar > ();
41 m_lu.IsRowMajor ? computeRowMajor() :
compute();
82 void setOrderingMethod(
int m) {
86 int orderingMethod()
const {
92 void computeRowMajor();
100 template<
typename BDerived,
typename XDerived>
101 bool solve(
const MatrixBase<BDerived> &b, MatrixBase<XDerived>* x,
102 const int transposed = 0)
const;
110 RealScalar m_precision;
112 mutable int m_status;
120 template<
typename MatrixType>
123 const size_t rows = m_lu.rows();
124 const size_t cols = m_lu.cols();
126 eigen_assert(rows == cols &&
"We do not (yet) support rectangular LU.");
127 eigen_assert(!m_lu.IsRowMajor &&
"LU decomposition does not work with rowMajor Storage");
129 for (Index row = 0; row < rows; row++) {
130 const double pivot = m_lu.coeffDiag(row);
133 const Index& col = row;
134 for (
typename MatrixType::InnerLowerIterator lIt(m_lu, col); lIt; ++lIt) {
135 lIt.valueRef() /= pivot;
139 typename MatrixType::InnerLowerIterator lIt(m_lu, col);
140 for (Index rrow = row + 1; rrow < m_lu.rows(); rrow++) {
141 typename MatrixType::InnerUpperIterator uItPivot(m_lu, row);
142 typename MatrixType::InnerUpperIterator uIt(m_lu, rrow);
143 const double coef = lIt.value();
145 uItPivot += (rrow - row - 1);
148 for (++uItPivot; uIt && uItPivot;) {
149 uIt.valueRef() -= uItPivot.value() * coef;
158 typename MatrixType::InnerLowerIterator lIt3(m_lu, col);
159 for (Index rrow = row + 1; rrow < m_lu.rows(); rrow++) {
160 typename MatrixType::InnerUpperIterator uItPivot(m_lu, row);
161 const double coef = lIt3.value();
164 for (Index i = 0; i < rrow - row - 1; i++) {
165 m_lu.coeffRefLower(rrow, row + i + 1) -= uItPivot.value() * coef;
171 typename MatrixType::InnerLowerIterator lIt2(m_lu, col);
172 for (Index rrow = row + 1; rrow < m_lu.rows(); rrow++) {
174 typename MatrixType::InnerUpperIterator uItPivot(m_lu, row);
175 typename MatrixType::InnerUpperIterator uIt(m_lu, rrow);
176 const double coef = lIt2.value();
178 uItPivot += (rrow - row - 1);
179 m_lu.coeffRefDiag(rrow) -= uItPivot.value() * coef;
185 template<
typename MatrixType>
187 const size_t rows = m_lu.rows();
188 const size_t cols = m_lu.cols();
190 eigen_assert(rows == cols &&
"We do not (yet) support rectangular LU.");
191 eigen_assert(m_lu.IsRowMajor &&
"You're trying to apply rowMajor decomposition on a ColMajor matrix !");
193 for (
Index row = 0; row < rows; row++) {
194 typename MatrixType::InnerLowerIterator llIt(m_lu, row);
197 for (
Index col = llIt.col(); col < row; col++) {
198 if (m_lu.coeffExistLower(row, col)) {
199 const double diag = m_lu.coeffDiag(col);
201 typename MatrixType::InnerLowerIterator lIt(m_lu, row);
202 typename MatrixType::InnerUpperIterator uIt(m_lu, col);
205 const Index offset = lIt.col() - uIt.row();
208 Index stop = offset > 0 ? col - lIt.col() : col - uIt.row();
212 Map<VectorXd > rowVal(lIt.valuePtr() + (offset > 0 ? 0 : -offset), stop);
213 Map<VectorXd > colVal(uIt.valuePtr() + (offset > 0 ? offset : 0), stop);
216 Scalar newCoeff = m_lu.coeffLower(row, col) - rowVal.dot(colVal);
222 Scalar newCoeff = m_lu.coeffLower(row, col);
224 for (
Index k = 0; k < stop; ++k) {
225 const Scalar tmp = newCoeff;
226 newCoeff = tmp - lIt.value() * uIt.value();
232 m_lu.coeffRefLower(row, col) = newCoeff / diag;
237 const Index col = row;
238 typename MatrixType::InnerUpperIterator uuIt(m_lu, col);
239 for (
Index rrow = uuIt.row(); rrow < col; rrow++) {
241 typename MatrixType::InnerLowerIterator lIt(m_lu, rrow);
242 typename MatrixType::InnerUpperIterator uIt(m_lu, col);
243 const Index offset = lIt.col() - uIt.row();
245 Index stop = offset > 0 ? rrow - lIt.col() : rrow - uIt.row();
248 Map<VectorXd > rowVal(lIt.valuePtr() + (offset > 0 ? 0 : -offset), stop);
249 Map<VectorXd > colVal(uIt.valuePtr() + (offset > 0 ? offset : 0), stop);
251 Scalar newCoeff = m_lu.coeffUpper(rrow, col) - rowVal.dot(colVal);
257 Scalar newCoeff = m_lu.coeffUpper(rrow, col);
258 for (
Index k = 0; k < stop; ++k) {
259 const Scalar tmp = newCoeff;
260 newCoeff = tmp - lIt.value() * uIt.value();
266 m_lu.coeffRefUpper(rrow, col) = newCoeff;
271 typename MatrixType::InnerLowerIterator lIt(m_lu, row);
272 typename MatrixType::InnerUpperIterator uIt(m_lu, row);
274 const Index offset = lIt.col() - uIt.row();
277 Index stop = offset > 0 ? lIt.size() : uIt.size();
279 Map<VectorXd > rowVal(lIt.valuePtr() + (offset > 0 ? 0 : -offset), stop);
280 Map<VectorXd > colVal(uIt.valuePtr() + (offset > 0 ? offset : 0), stop);
281 Scalar newCoeff = m_lu.coeffDiag(row) - rowVal.dot(colVal);
287 Scalar newCoeff = m_lu.coeffDiag(row);
288 for (
Index k = 0; k < stop; ++k) {
289 const Scalar tmp = newCoeff;
290 newCoeff = tmp - lIt.value() * uIt.value();
295 m_lu.coeffRefDiag(row) = newCoeff;
307 template<
typename MatrixType>
308 template<
typename BDerived,
typename XDerived>
310 const size_t rows = m_lu.rows();
311 const size_t cols = m_lu.cols();
314 for (Index row = 0; row < rows; row++) {
315 x->coeffRef(row) = b.coeff(row);
316 Scalar newVal = x->coeff(row);
317 typename MatrixType::InnerLowerIterator lIt(m_lu, row);
319 Index col = lIt.col();
320 while (lIt.col() < row) {
322 newVal -= x->coeff(col++) * lIt.value();
326 x->coeffRef(row) = newVal;
330 for (Index col = rows - 1; col > 0; col--) {
331 x->coeffRef(col) = x->coeff(col) / m_lu.coeffDiag(col);
333 const Scalar x_col = x->coeff(col);
335 typename MatrixType::InnerUpperIterator uIt(m_lu, col);
340 x->coeffRef(uIt.row()) -= x_col * uIt.value();
347 x->coeffRef(0) = x->coeff(0) / m_lu.coeffDiag(0);
Inplace LU decomposition of a skyline matrix and associated features.
Definition: SkylineInplaceLU.h:27
RealScalar precision() const
Definition: SkylineInplaceLU.h:61
void setPrecision(RealScalar v)
Definition: SkylineInplaceLU.h:54
bool solve(const MatrixBase< BDerived > &b, MatrixBase< XDerived > *x, const int transposed=0) const
Definition: SkylineInplaceLU.h:309
void compute()
Definition: SkylineInplaceLU.h:122
int flags() const
Definition: SkylineInplaceLU.h:78
bool succeeded(void) const
Definition: SkylineInplaceLU.h:105
SkylineInplaceLU(MatrixType &matrix, int flags=0)
Definition: SkylineInplaceLU.h:38
void setFlags(int f)
Definition: SkylineInplaceLU.h:73
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index