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)
271 det *= (std::abs)(it.value());
289 eigen_assert(m_factorizationIsOk &&
"The matrix should be factorized first.");
290 Scalar det = Scalar(0.);
291 for (Index j = 0; j < this->cols(); ++j)
293 for (
typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
295 if(it.row() < j)
continue;
298 det += (std::log)((std::abs)(it.value()));
312 eigen_assert(m_factorizationIsOk &&
"The matrix should be factorized first.");
313 return Scalar(m_detPermR);
318 void initperfvalues()
320 m_perfv.panel_size = 1;
322 m_perfv.maxsuper = 128;
325 m_perfv.fillfactor = 20;
330 bool m_isInitialized;
331 bool m_factorizationIsOk;
333 std::string m_lastError;
336 MappedSparseMatrix<Scalar,ColMajor,Index> m_Ustore;
337 PermutationType m_perm_c;
338 PermutationType m_perm_r ;
341 typename Base::GlobalLU_t m_glu;
344 bool m_symmetricmode;
346 internal::perfvalues<Index> m_perfv;
347 RealScalar m_diagpivotthresh;
348 Index m_nnzL, m_nnzU;
352 SparseLU (
const SparseLU& );
369 template <
typename MatrixType,
typename OrderingType>
381 if (m_perm_c.size()) {
384 const Index * outerIndexPtr;
385 if (mat.isCompressed()) outerIndexPtr = mat.outerIndexPtr();
388 Index *outerIndexPtr_t =
new Index[mat.cols()+1];
389 for(Index i = 0; i <= mat.cols(); i++) outerIndexPtr_t[i] = m_mat.outerIndexPtr()[i];
390 outerIndexPtr = outerIndexPtr_t;
392 for (Index i = 0; i < mat.cols(); i++)
394 m_mat.outerIndexPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i];
395 m_mat.innerNonZeroPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i+1] - outerIndexPtr[i];
397 if(!mat.isCompressed())
delete[] outerIndexPtr;
404 if (!m_symmetricmode) {
411 Index m = m_mat.cols();
413 for (Index i = 0; i < m; ++i) iwork(post(i)) = post(m_etree(i));
418 for (Index i = 0; i < m; i++)
419 post_perm.
indices()(i) = post(i);
422 if(m_perm_c.size()) {
423 m_perm_c = post_perm * m_perm_c;
428 m_analysisIsOk =
true;
452 template <
typename MatrixType,
typename OrderingType>
455 using internal::emptyIdxLU;
456 eigen_assert(m_analysisIsOk &&
"analyzePattern() should be called first");
457 eigen_assert((matrix.rows() == matrix.cols()) &&
"Only for squared matrices");
459 typedef typename IndexVector::Scalar Index;
469 const Index * outerIndexPtr;
470 if (matrix.isCompressed()) outerIndexPtr = matrix.outerIndexPtr();
473 Index* outerIndexPtr_t =
new Index[matrix.cols()+1];
474 for(Index i = 0; i <= matrix.cols(); i++) outerIndexPtr_t[i] = m_mat.outerIndexPtr()[i];
475 outerIndexPtr = outerIndexPtr_t;
477 for (Index i = 0; i < matrix.cols(); i++)
479 m_mat.outerIndexPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i];
480 m_mat.innerNonZeroPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i+1] - outerIndexPtr[i];
482 if(!matrix.isCompressed())
delete[] outerIndexPtr;
486 m_perm_c.resize(matrix.cols());
487 for(Index i = 0; i < matrix.cols(); ++i) m_perm_c.indices()(i) = i;
490 Index m = m_mat.rows();
491 Index n = m_mat.cols();
492 Index nnz = m_mat.nonZeros();
493 Index maxpanel = m_perfv.panel_size * m;
496 Index info = Base::memInit(m, n, nnz, lwork, m_perfv.fillfactor, m_perfv.panel_size, m_glu);
499 m_lastError =
"UNABLE TO ALLOCATE WORKING MEMORY\n\n" ;
500 m_factorizationIsOk =
false;
520 tempv.
setZero(internal::LUnumTempV(m, m_perfv.panel_size, m_perfv.maxsuper, m) );
527 if ( m_symmetricmode ==
true )
528 Base::heap_relax_snode(n, m_etree, m_perfv.relax, marker, relax_end);
530 Base::relax_snode(n, m_etree, m_perfv.relax, marker, relax_end);
534 m_perm_r.indices().setConstant(-1);
538 m_glu.supno(0) = emptyIdxLU; m_glu.xsup.setConstant(0);
539 m_glu.xsup(0) = m_glu.xlsub(0) = m_glu.xusub(0) = m_glu.xlusup(0) = Index(0);
551 for (jcol = 0; jcol < n; )
554 Index panel_size = m_perfv.panel_size;
555 for (k = jcol + 1; k < (std::min)(jcol+panel_size, n); k++)
557 if (relax_end(k) != emptyIdxLU)
559 panel_size = k - jcol;
564 panel_size = n - jcol;
567 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);
570 Base::panel_bmod(m, panel_size, jcol, nseg1, dense, tempv, segrep, repfnz, m_glu);
573 for ( jj = jcol; jj< jcol + panel_size; jj++)
581 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);
584 m_lastError =
"UNABLE TO EXPAND MEMORY IN COLUMN_DFS() ";
586 m_factorizationIsOk =
false;
592 info = Base::column_bmod(jj, (nseg - nseg1), dense_k, tempv, segrep_k, repfnz_k, jcol, m_glu);
595 m_lastError =
"UNABLE TO EXPAND MEMORY IN COLUMN_BMOD() ";
597 m_factorizationIsOk =
false;
602 info = Base::copy_to_ucol(jj, nseg, segrep, repfnz_k ,m_perm_r.indices(), dense_k, m_glu);
605 m_lastError =
"UNABLE TO EXPAND MEMORY IN COPY_TO_UCOL() ";
607 m_factorizationIsOk =
false;
612 info = Base::pivotL(jj, m_diagpivotthresh, m_perm_r.indices(), iperm_c.indices(), pivrow, m_glu);
615 m_lastError =
"THE MATRIX IS STRUCTURALLY SINGULAR ... ZERO COLUMN AT ";
616 std::ostringstream returnInfo;
618 m_lastError += returnInfo.str();
620 m_factorizationIsOk =
false;
625 if (pivrow != jj) m_detPermR *= -1;
628 Base::pruneL(jj, m_perm_r.indices(), pivrow, nseg, segrep, repfnz_k, xprune, m_glu);
631 for (i = 0; i < nseg; i++)
634 repfnz_k(irep) = emptyIdxLU;
641 Base::countnz(n, m_nnzL, m_nnzU, m_glu);
643 Base::fixupL(n, m_perm_r.indices(), m_glu);
646 m_Lstore.setInfos(m, n, m_glu.lusup, m_glu.xlusup, m_glu.lsub, m_glu.xlsub, m_glu.supno, m_glu.xsup);
651 m_factorizationIsOk =
true;
654 template<
typename MappedSupernodalType>
655 struct SparseLUMatrixLReturnType : internal::no_assignment_operator
657 typedef typename MappedSupernodalType::Index Index;
658 typedef typename MappedSupernodalType::Scalar Scalar;
659 SparseLUMatrixLReturnType(
const MappedSupernodalType& mapL) : m_mapL(mapL)
661 Index rows() {
return m_mapL.rows(); }
662 Index cols() {
return m_mapL.cols(); }
663 template<
typename Dest>
664 void solveInPlace( MatrixBase<Dest> &X)
const
666 m_mapL.solveInPlace(X);
668 const MappedSupernodalType& m_mapL;
671 template<
typename MatrixLType,
typename MatrixUType>
672 struct SparseLUMatrixUReturnType : internal::no_assignment_operator
674 typedef typename MatrixLType::Index Index;
675 typedef typename MatrixLType::Scalar Scalar;
676 SparseLUMatrixUReturnType(
const MatrixLType& mapL,
const MatrixUType& mapU)
677 : m_mapL(mapL),m_mapU(mapU)
679 Index rows() {
return m_mapL.rows(); }
680 Index cols() {
return m_mapL.cols(); }
682 template<
typename Dest>
void solveInPlace(MatrixBase<Dest> &X)
const
684 Index nrhs = X.cols();
687 for (Index k = m_mapL.nsuper(); k >= 0; k--)
689 Index fsupc = m_mapL.supToCol()[k];
690 Index lda = m_mapL.colIndexPtr()[fsupc+1] - m_mapL.colIndexPtr()[fsupc];
691 Index nsupc = m_mapL.supToCol()[k+1] - fsupc;
692 Index luptr = m_mapL.colIndexPtr()[fsupc];
696 for (Index j = 0; j < nrhs; j++)
698 X(fsupc, j) /= m_mapL.valuePtr()[luptr];
703 Map<const Matrix<Scalar,Dynamic,Dynamic>, 0, OuterStride<> > A( &(m_mapL.valuePtr()[luptr]), nsupc, nsupc, OuterStride<>(lda) );
704 Map< Matrix<Scalar,Dynamic,Dynamic>, 0, OuterStride<> > U (&(X(fsupc,0)), nsupc, nrhs, OuterStride<>(n) );
705 U = A.template triangularView<Upper>().solve(U);
708 for (Index j = 0; j < nrhs; ++j)
710 for (Index jcol = fsupc; jcol < fsupc + nsupc; jcol++)
712 typename MatrixUType::InnerIterator it(m_mapU, jcol);
715 Index irow = it.index();
716 X(irow, j) -= X(jcol, j) * it.value();
722 const MatrixLType& m_mapL;
723 const MatrixUType& m_mapU;
728 template<
typename _MatrixType,
typename Derived,
typename Rhs>
729 struct solve_retval<SparseLU<_MatrixType,Derived>, Rhs>
730 : solve_retval_base<SparseLU<_MatrixType,Derived>, Rhs>
732 typedef SparseLU<_MatrixType,Derived> Dec;
733 EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
735 template<typename Dest>
void evalTo(Dest& dst)
const
737 dec()._solve(rhs(),dst);
741 template<
typename _MatrixType,
typename Derived,
typename Rhs>
742 struct sparse_solve_retval<SparseLU<_MatrixType,Derived>, Rhs>
743 : sparse_solve_retval_base<SparseLU<_MatrixType,Derived>, Rhs>
745 typedef SparseLU<_MatrixType,Derived> Dec;
746 EIGEN_MAKE_SPARSE_SOLVE_HELPERS(Dec,Rhs)
748 template<typename Dest>
void evalTo(Dest& dst)
const
750 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:370
Index cols() const
Definition: SparseMatrix.h:121
const IndicesType & indices() const
Definition: PermutationMatrix.h:358
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:453
Scalar logAbsDeterminant() const
Definition: SparseLU.h:287
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: SparseMatrixBase.h:26
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:150
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:310
void treePostorder(Index n, IndexVector &parent, IndexVector &post)
Post order a tree.
Definition: SparseColEtree.h:178
Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:515