12 #ifndef EIGEN_SPARSE_LU_H
13 #define EIGEN_SPARSE_LU_H
17 template <
typename _MatrixType,
typename _OrderingType = COLAMDOrdering<
typename _MatrixType::Index> >
class SparseLU;
18 template <
typename MappedSparseMatrixType>
struct SparseLUMatrixLReturnType;
19 template <
typename MatrixLType,
typename MatrixUType>
struct SparseLUMatrixUReturnType;
72 template <
typename _MatrixType,
typename _OrderingType>
73 class SparseLU :
public internal::SparseLUImpl<typename _MatrixType::Scalar, typename _MatrixType::Index>
76 typedef _MatrixType MatrixType;
77 typedef _OrderingType OrderingType;
78 typedef typename MatrixType::Scalar Scalar;
79 typedef typename MatrixType::RealScalar RealScalar;
80 typedef typename MatrixType::Index Index;
82 typedef internal::MappedSuperNodalMatrix<Scalar, Index> SCMatrix;
86 typedef internal::SparseLUImpl<Scalar, Index> Base;
89 SparseLU():m_isInitialized(true),m_lastError(
""),m_Ustore(0,0,0,0,0,0),m_symmetricmode(false),m_diagpivotthresh(1.0),m_detPermR(1)
93 SparseLU(
const MatrixType& matrix):m_isInitialized(true),m_lastError(
""),m_Ustore(0,0,0,0,0,0),m_symmetricmode(false),m_diagpivotthresh(1.0),m_detPermR(1)
105 void factorize (
const MatrixType& matrix);
106 void simplicialfactorize(
const MatrixType& matrix);
120 inline Index rows()
const {
return m_mat.
rows(); }
121 inline Index cols()
const {
return m_mat.
cols(); }
125 m_symmetricmode = sym;
134 SparseLUMatrixLReturnType<SCMatrix>
matrixL()
const
136 return SparseLUMatrixLReturnType<SCMatrix>(m_Lstore);
144 SparseLUMatrixUReturnType<SCMatrix,MappedSparseMatrix<Scalar,ColMajor,Index> >
matrixU()
const
146 return SparseLUMatrixUReturnType<SCMatrix, MappedSparseMatrix<Scalar,ColMajor,Index> >(m_Lstore, m_Ustore);
168 m_diagpivotthresh = thresh;
177 template<
typename Rhs>
180 eigen_assert(m_factorizationIsOk &&
"SparseLU is not initialized.");
181 eigen_assert(rows()==B.rows()
182 &&
"SparseLU::solve(): invalid number of rows of the right hand side matrix B");
183 return internal::solve_retval<SparseLU, Rhs>(*
this, B.derived());
190 template<
typename Rhs>
193 eigen_assert(m_factorizationIsOk &&
"SparseLU is not initialized.");
194 eigen_assert(rows()==B.
rows()
195 &&
"SparseLU::solve(): invalid number of rows of the right hand side matrix B");
196 return internal::sparse_solve_retval<SparseLU, Rhs>(*
this, B.
derived());
209 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
221 template<
typename Rhs,
typename Dest>
224 Dest& X(X_base.derived());
225 eigen_assert(m_factorizationIsOk &&
"The matrix should be factorized first");
227 THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
231 X.resize(B.rows(),B.cols());
234 for(Index j = 0; j < B.cols(); ++j)
238 this->
matrixL().solveInPlace(X);
239 this->
matrixU().solveInPlace(X);
242 for (Index j = 0; j < B.cols(); ++j)
260 eigen_assert(m_factorizationIsOk &&
"The matrix should be factorized first.");
262 Scalar det = Scalar(1.);
265 for (Index j = 0; j < this->cols(); ++j)
267 for (
typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
272 det *= abs(it.value());
290 eigen_assert(m_factorizationIsOk &&
"The matrix should be factorized first.");
291 Scalar det = Scalar(0.);
292 for (Index j = 0; j < this->cols(); ++j)
294 for (
typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
296 if(it.row() < j)
continue;
299 using std::log;
using std::abs;
300 det += log(abs(it.value()));
314 eigen_assert(m_factorizationIsOk &&
"The matrix should be factorized first.");
319 for (Index j = 0; j < this->cols(); ++j)
321 for (
typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
327 else if(it.value()==0)
333 return det * m_detPermR * m_detPermC;
342 eigen_assert(m_factorizationIsOk &&
"The matrix should be factorized first.");
344 Scalar det = Scalar(1.);
347 for (Index j = 0; j < this->cols(); ++j)
349 for (
typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
358 return det * Scalar(m_detPermR * m_detPermC);
363 void initperfvalues()
365 m_perfv.panel_size = 16;
367 m_perfv.maxsuper = 128;
370 m_perfv.fillfactor = 20;
375 bool m_isInitialized;
376 bool m_factorizationIsOk;
378 std::string m_lastError;
381 MappedSparseMatrix<Scalar,ColMajor,Index> m_Ustore;
382 PermutationType m_perm_c;
383 PermutationType m_perm_r ;
386 typename Base::GlobalLU_t m_glu;
389 bool m_symmetricmode;
391 internal::perfvalues<Index> m_perfv;
392 RealScalar m_diagpivotthresh;
393 Index m_nnzL, m_nnzU;
394 Index m_detPermR, m_detPermC;
397 SparseLU (
const SparseLU& );
414 template <
typename MatrixType,
typename OrderingType>
426 if (m_perm_c.size()) {
429 const Index * outerIndexPtr;
430 if (mat.isCompressed()) outerIndexPtr = mat.outerIndexPtr();
433 Index *outerIndexPtr_t =
new Index[mat.cols()+1];
434 for(Index i = 0; i <= mat.cols(); i++) outerIndexPtr_t[i] = m_mat.outerIndexPtr()[i];
435 outerIndexPtr = outerIndexPtr_t;
437 for (Index i = 0; i < mat.cols(); i++)
439 m_mat.outerIndexPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i];
440 m_mat.innerNonZeroPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i+1] - outerIndexPtr[i];
442 if(!mat.isCompressed())
delete[] outerIndexPtr;
449 if (!m_symmetricmode) {
456 Index m = m_mat.cols();
458 for (Index i = 0; i < m; ++i) iwork(post(i)) = post(m_etree(i));
463 for (Index i = 0; i < m; i++)
464 post_perm.
indices()(i) = post(i);
467 if(m_perm_c.size()) {
468 m_perm_c = post_perm * m_perm_c;
473 m_analysisIsOk =
true;
497 template <
typename MatrixType,
typename OrderingType>
500 using internal::emptyIdxLU;
501 eigen_assert(m_analysisIsOk &&
"analyzePattern() should be called first");
502 eigen_assert((matrix.rows() == matrix.cols()) &&
"Only for squared matrices");
504 typedef typename IndexVector::Scalar Index;
514 const Index * outerIndexPtr;
515 if (matrix.isCompressed()) outerIndexPtr = matrix.outerIndexPtr();
518 Index* outerIndexPtr_t =
new Index[matrix.cols()+1];
519 for(Index i = 0; i <= matrix.cols(); i++) outerIndexPtr_t[i] = m_mat.outerIndexPtr()[i];
520 outerIndexPtr = outerIndexPtr_t;
522 for (Index i = 0; i < matrix.cols(); i++)
524 m_mat.outerIndexPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i];
525 m_mat.innerNonZeroPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i+1] - outerIndexPtr[i];
527 if(!matrix.isCompressed())
delete[] outerIndexPtr;
531 m_perm_c.resize(matrix.cols());
532 for(Index i = 0; i < matrix.cols(); ++i) m_perm_c.indices()(i) = i;
535 Index m = m_mat.rows();
536 Index n = m_mat.cols();
537 Index nnz = m_mat.nonZeros();
538 Index maxpanel = m_perfv.panel_size * m;
541 Index info = Base::memInit(m, n, nnz, lwork, m_perfv.fillfactor, m_perfv.panel_size, m_glu);
544 m_lastError =
"UNABLE TO ALLOCATE WORKING MEMORY\n\n" ;
545 m_factorizationIsOk =
false;
565 tempv.
setZero(internal::LUnumTempV(m, m_perfv.panel_size, m_perfv.maxsuper, m) );
572 if ( m_symmetricmode ==
true )
573 Base::heap_relax_snode(n, m_etree, m_perfv.relax, marker, relax_end);
575 Base::relax_snode(n, m_etree, m_perfv.relax, marker, relax_end);
579 m_perm_r.indices().setConstant(-1);
583 m_glu.supno(0) = emptyIdxLU; m_glu.xsup.setConstant(0);
584 m_glu.xsup(0) = m_glu.xlsub(0) = m_glu.xusub(0) = m_glu.xlusup(0) = Index(0);
596 for (jcol = 0; jcol < n; )
599 Index panel_size = m_perfv.panel_size;
600 for (k = jcol + 1; k < (std::min)(jcol+panel_size, n); k++)
602 if (relax_end(k) != emptyIdxLU)
604 panel_size = k - jcol;
609 panel_size = n - jcol;
612 Base::panel_dfs(m, panel_size, jcol, m_mat, m_perm_r.indices(), nseg1, dense, panel_lsub, segrep, repfnz, xprune, marker, parent, xplore, m_glu);
615 Base::panel_bmod(m, panel_size, jcol, nseg1, dense, tempv, segrep, repfnz, m_glu);
618 for ( jj = jcol; jj< jcol + panel_size; jj++)
626 info = Base::column_dfs(m, jj, m_perm_r.indices(), m_perfv.maxsuper, nseg, panel_lsubk, segrep, repfnz_k, xprune, marker, parent, xplore, m_glu);
629 m_lastError =
"UNABLE TO EXPAND MEMORY IN COLUMN_DFS() ";
631 m_factorizationIsOk =
false;
637 info = Base::column_bmod(jj, (nseg - nseg1), dense_k, tempv, segrep_k, repfnz_k, jcol, m_glu);
640 m_lastError =
"UNABLE TO EXPAND MEMORY IN COLUMN_BMOD() ";
642 m_factorizationIsOk =
false;
647 info = Base::copy_to_ucol(jj, nseg, segrep, repfnz_k ,m_perm_r.indices(), dense_k, m_glu);
650 m_lastError =
"UNABLE TO EXPAND MEMORY IN COPY_TO_UCOL() ";
652 m_factorizationIsOk =
false;
657 info = Base::pivotL(jj, m_diagpivotthresh, m_perm_r.indices(), iperm_c.indices(), pivrow, m_glu);
660 m_lastError =
"THE MATRIX IS STRUCTURALLY SINGULAR ... ZERO COLUMN AT ";
661 std::ostringstream returnInfo;
663 m_lastError += returnInfo.str();
665 m_factorizationIsOk =
false;
671 if (pivrow != jj) m_detPermR = -m_detPermR;
674 Base::pruneL(jj, m_perm_r.indices(), pivrow, nseg, segrep, repfnz_k, xprune, m_glu);
677 for (i = 0; i < nseg; i++)
680 repfnz_k(irep) = emptyIdxLU;
686 m_detPermR = m_perm_r.determinant();
687 m_detPermC = m_perm_c.determinant();
690 Base::countnz(n, m_nnzL, m_nnzU, m_glu);
692 Base::fixupL(n, m_perm_r.indices(), m_glu);
695 m_Lstore.setInfos(m, n, m_glu.lusup, m_glu.xlusup, m_glu.lsub, m_glu.xlsub, m_glu.supno, m_glu.xsup);
700 m_factorizationIsOk =
true;
703 template<
typename MappedSupernodalType>
704 struct SparseLUMatrixLReturnType : internal::no_assignment_operator
706 typedef typename MappedSupernodalType::Index Index;
707 typedef typename MappedSupernodalType::Scalar Scalar;
708 SparseLUMatrixLReturnType(
const MappedSupernodalType& mapL) : m_mapL(mapL)
710 Index rows() {
return m_mapL.rows(); }
711 Index cols() {
return m_mapL.cols(); }
712 template<
typename Dest>
713 void solveInPlace( MatrixBase<Dest> &X)
const
715 m_mapL.solveInPlace(X);
717 const MappedSupernodalType& m_mapL;
720 template<
typename MatrixLType,
typename MatrixUType>
721 struct SparseLUMatrixUReturnType : internal::no_assignment_operator
723 typedef typename MatrixLType::Index Index;
724 typedef typename MatrixLType::Scalar Scalar;
725 SparseLUMatrixUReturnType(
const MatrixLType& mapL,
const MatrixUType& mapU)
726 : m_mapL(mapL),m_mapU(mapU)
728 Index rows() {
return m_mapL.rows(); }
729 Index cols() {
return m_mapL.cols(); }
731 template<
typename Dest>
void solveInPlace(MatrixBase<Dest> &X)
const
733 Index nrhs = X.cols();
736 for (Index k = m_mapL.nsuper(); k >= 0; k--)
738 Index fsupc = m_mapL.supToCol()[k];
739 Index lda = m_mapL.colIndexPtr()[fsupc+1] - m_mapL.colIndexPtr()[fsupc];
740 Index nsupc = m_mapL.supToCol()[k+1] - fsupc;
741 Index luptr = m_mapL.colIndexPtr()[fsupc];
745 for (Index j = 0; j < nrhs; j++)
747 X(fsupc, j) /= m_mapL.valuePtr()[luptr];
752 Map<const Matrix<Scalar,Dynamic,Dynamic, ColMajor>, 0, OuterStride<> > A( &(m_mapL.valuePtr()[luptr]), nsupc, nsupc, OuterStride<>(lda) );
753 Map< Matrix<Scalar,Dynamic,Dynamic, ColMajor>, 0, OuterStride<> > U (&(X(fsupc,0)), nsupc, nrhs, OuterStride<>(n) );
754 U = A.template triangularView<Upper>().solve(U);
757 for (Index j = 0; j < nrhs; ++j)
759 for (Index jcol = fsupc; jcol < fsupc + nsupc; jcol++)
761 typename MatrixUType::InnerIterator it(m_mapU, jcol);
764 Index irow = it.index();
765 X(irow, j) -= X(jcol, j) * it.value();
771 const MatrixLType& m_mapL;
772 const MatrixUType& m_mapU;
777 template<
typename _MatrixType,
typename Derived,
typename Rhs>
778 struct solve_retval<SparseLU<_MatrixType,Derived>, Rhs>
779 : solve_retval_base<SparseLU<_MatrixType,Derived>, Rhs>
781 typedef SparseLU<_MatrixType,Derived> Dec;
782 EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
784 template<typename Dest>
void evalTo(Dest& dst)
const
786 dec()._solve(rhs(),dst);
790 template<
typename _MatrixType,
typename Derived,
typename Rhs>
791 struct sparse_solve_retval<SparseLU<_MatrixType,Derived>, Rhs>
792 : sparse_solve_retval_base<SparseLU<_MatrixType,Derived>, Rhs>
794 typedef SparseLU<_MatrixType,Derived> Dec;
795 EIGEN_MAKE_SPARSE_SOLVE_HELPERS(Dec,Rhs)
797 template<typename Dest>
void evalTo(Dest& dst)
const
799 this->defaultEvalTo(dst);
SparseLUMatrixLReturnType< SCMatrix > matrixL() const
Definition: SparseLU.h:134
Index rows() const
Definition: SparseMatrix.h:119
void analyzePattern(const MatrixType &matrix)
Definition: SparseLU.h:415
Index cols() const
Definition: SparseMatrix.h:121
const IndicesType & indices() const
Definition: PermutationMatrix.h:387
Transpose< PermutationBase > inverse() const
Definition: PermutationMatrix.h:201
Definition: Constants.h:378
const internal::sparse_solve_retval< SparseLU, Rhs > solve(const SparseMatrixBase< Rhs > &B) const
Definition: SparseLU.h:191
Scalar absDeterminant()
Definition: SparseLU.h:258
void factorize(const MatrixType &matrix)
Definition: SparseLU.h:498
Scalar logAbsDeterminant() const
Definition: SparseLU.h:288
ColXpr col(Index i)
Definition: DenseBase.h:733
const PermutationType & rowsPermutation() const
Definition: SparseLU.h:153
Sparse supernodal LU factorization for general matrices.
Definition: SparseLU.h:17
const PermutationType & colsPermutation() const
Definition: SparseLU.h:161
int coletree(const MatrixType &mat, IndexVector &parent, IndexVector &firstRowElt, typename MatrixType::Index *perm=0)
Definition: SparseColEtree.h:61
Expression of a fixed-size or dynamic-size sub-vector.
Definition: ForwardDeclarations.h:83
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:239
Scalar determinant()
Definition: SparseLU.h:340
Derived & derived()
Definition: EigenBase.h:34
const internal::solve_retval< SparseLU, Rhs > solve(const MatrixBase< Rhs > &B) const
Definition: SparseLU.h:178
void isSymmetric(bool sym)
Definition: SparseLU.h:123
void compute(const MatrixType &matrix)
Definition: SparseLU.h:112
Derived & setConstant(Index size, const Scalar &value)
Definition: CwiseNullaryOp.h:348
SparseLUMatrixUReturnType< SCMatrix, MappedSparseMatrix< Scalar, ColMajor, Index > > matrixU() const
Definition: SparseLU.h:144
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: SparseLU.h:207
Definition: Constants.h:376
const unsigned int RowMajorBit
Definition: Constants.h:53
void resize(Index nbRows, Index nbCols)
Definition: PlainObjectBase.h:235
std::string lastErrorMessage() const
Definition: SparseLU.h:216
Index rows() const
Definition: SparseMatrixBase.h:159
void setPivotThreshold(const RealScalar &thresh)
Definition: SparseLU.h:166
ComputationInfo
Definition: Constants.h:374
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Scalar signDeterminant()
Definition: SparseLU.h:312
void treePostorder(Index n, IndexVector &parent, IndexVector &post)
Post order a tree.
Definition: SparseColEtree.h:178
Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:515