12#ifndef EIGEN_SPARSE_LU_H
13#define EIGEN_SPARSE_LU_H
17template <
typename _MatrixType,
typename _OrderingType = COLAMDOrdering<
typename _MatrixType::StorageIndex> >
class SparseLU;
18template <
typename MappedSparseMatrixType>
struct SparseLUMatrixLReturnType;
19template <
typename MatrixLType,
typename MatrixUType>
struct SparseLUMatrixUReturnType;
73template <
typename _MatrixType,
typename _OrderingType>
78 using APIBase::m_isInitialized;
80 using APIBase::_solve_impl;
82 typedef _MatrixType MatrixType;
84 typedef typename MatrixType::Scalar Scalar;
85 typedef typename MatrixType::RealScalar RealScalar;
86 typedef typename MatrixType::StorageIndex StorageIndex;
95 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
96 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
100 SparseLU():m_lastError(
""),m_Ustore(0,0,0,0,0,0),m_symmetricmode(
false),m_diagpivotthresh(1.0),m_detPermR(1)
104 explicit SparseLU(
const MatrixType& matrix)
105 : m_lastError(
""),m_Ustore(0,0,0,0,0,0),m_symmetricmode(
false),m_diagpivotthresh(1.0),m_detPermR(1)
117 void factorize (
const MatrixType& matrix);
118 void simplicialfactorize(
const MatrixType& matrix);
132 inline Index rows()
const {
return m_mat.
rows(); }
133 inline Index cols()
const {
return m_mat.
cols(); }
137 m_symmetricmode =
sym;
180 m_diagpivotthresh =
thresh;
183#ifdef EIGEN_PARSED_BY_DOXYGEN
190 template<
typename Rhs>
204 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
216 template<
typename Rhs,
typename Dest>
220 eigen_assert(m_factorizationIsOk &&
"The matrix should be factorized first");
222 THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
226 X.resize(B.rows(),B.cols());
229 for(Index
j = 0;
j < B.cols(); ++
j)
233 this->
matrixL().solveInPlace(X);
234 this->
matrixU().solveInPlace(X);
237 for (Index
j = 0;
j < B.cols(); ++
j)
256 eigen_assert(m_factorizationIsOk &&
"The matrix should be factorized first.");
258 Scalar
det = Scalar(1.);
261 for (Index
j = 0;
j < this->cols(); ++
j)
263 for (
typename SCMatrix::InnerIterator it(m_Lstore,
j); it; ++it)
267 det *= abs(it.value());
288 eigen_assert(m_factorizationIsOk &&
"The matrix should be factorized first.");
289 Scalar
det = Scalar(0.);
290 for (Index
j = 0;
j < this->cols(); ++
j)
292 for (
typename SCMatrix::InnerIterator it(m_Lstore,
j); it; ++it)
294 if(it.row() <
j)
continue;
297 det += log(abs(it.value()));
311 eigen_assert(m_factorizationIsOk &&
"The matrix should be factorized first.");
316 for (Index
j = 0;
j < this->cols(); ++
j)
318 for (
typename SCMatrix::InnerIterator it(m_Lstore,
j); it; ++it)
324 else if(it.value()==0)
330 return det * m_detPermR * m_detPermC;
339 eigen_assert(m_factorizationIsOk &&
"The matrix should be factorized first.");
341 Scalar
det = Scalar(1.);
344 for (Index
j = 0;
j < this->cols(); ++
j)
346 for (
typename SCMatrix::InnerIterator it(m_Lstore,
j); it; ++it)
355 return (m_detPermR * m_detPermC) > 0 ?
det : -
det;
360 void initperfvalues()
362 m_perfv.panel_size = 16;
364 m_perfv.maxsuper = 128;
367 m_perfv.fillfactor = 20;
372 bool m_factorizationIsOk;
374 std::string m_lastError;
377 MappedSparseMatrix<Scalar,ColMajor,StorageIndex> m_Ustore;
378 PermutationType m_perm_c;
379 PermutationType m_perm_r ;
382 typename Base::GlobalLU_t m_glu;
385 bool m_symmetricmode;
387 internal::perfvalues m_perfv;
388 RealScalar m_diagpivotthresh;
389 Index m_nnzL, m_nnzU;
390 Index m_detPermR, m_detPermC;
393 SparseLU (
const SparseLU& );
410template <
typename MatrixType,
typename OrderingType>
428 ei_declare_aligned_stack_constructed_variable(StorageIndex,outerIndexPtr,
mat.cols()+1,
mat.isCompressed()?
const_cast<StorageIndex*
>(
mat.outerIndexPtr()):0);
431 if(!
mat.isCompressed())
432 IndexVector::Map(outerIndexPtr,
mat.cols()+1) = IndexVector::Map(m_mat.outerIndexPtr(),
mat.cols()+1);
435 for (Index i = 0; i <
mat.cols(); i++)
437 m_mat.outerIndexPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i];
438 m_mat.innerNonZeroPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i+1] - outerIndexPtr[i];
447 if (!m_symmetricmode) {
450 internal::treePostorder(StorageIndex(m_mat.cols()), m_etree,
post);
454 Index m = m_mat.cols();
456 for (Index i = 0; i < m; ++i)
iwork(
post(i)) =
post(m_etree(i));
461 for (Index i = 0; i < m; i++)
465 if(m_perm_c.size()) {
471 m_analysisIsOk =
true;
495template <
typename MatrixType,
typename OrderingType>
498 using internal::emptyIdxLU;
499 eigen_assert(m_analysisIsOk &&
"analyzePattern() should be called first");
500 eigen_assert((matrix.rows() == matrix.cols()) &&
"Only for squared matrices");
502 typedef typename IndexVector::Scalar StorageIndex;
504 m_isInitialized =
true;
514 const StorageIndex * outerIndexPtr;
515 if (matrix.isCompressed()) outerIndexPtr = matrix.outerIndexPtr();
519 for(Index i = 0; i <= matrix.cols(); i++)
outerIndexPtr_t[i] = m_mat.outerIndexPtr()[i];
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(StorageIndex 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 )
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);
599 Index panel_size = m_perfv.panel_size;
600 for (k =
jcol + 1; k < (std::min)(
jcol+panel_size, n); k++)
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);
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;
640 m_lastError =
"UNABLE TO EXPAND MEMORY IN COLUMN_BMOD() ";
642 m_factorizationIsOk =
false;
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 ";
665 m_factorizationIsOk =
false;
671 if (
pivrow !=
jj) m_detPermR = -m_detPermR;
677 for (i = 0; i <
nseg; i++)
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;
703template<
typename MappedSupernodalType>
706 typedef typename MappedSupernodalType::Scalar Scalar;
709 Index rows() {
return m_mapL.rows(); }
710 Index cols() {
return m_mapL.cols(); }
711 template<
typename Dest>
714 m_mapL.solveInPlace(X);
719template<
typename MatrixLType,
typename MatrixUType>
722 typedef typename MatrixLType::Scalar Scalar;
726 Index rows() {
return m_mapL.rows(); }
727 Index cols() {
return m_mapL.cols(); }
731 Index
nrhs = X.cols();
734 for (Index k = m_mapL.nsuper(); k >= 0; k--)
736 Index
fsupc = m_mapL.supToCol()[k];
737 Index lda = m_mapL.colIndexPtr()[
fsupc+1] - m_mapL.colIndexPtr()[
fsupc];
743 for (Index
j = 0;
j <
nrhs;
j++)
755 for (Index
j = 0;
j <
nrhs; ++
j)
759 typename MatrixUType::InnerIterator it(m_mapU,
jcol);
762 Index
irow = it.index();
770 const MatrixUType& m_mapU;
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Resizes to the given size, and sets all coefficients in this expression to zero.
Definition CwiseNullaryOp.h:520
Pseudo expression representing a solving operation.
Definition Solve.h:63
Sparse supernodal LU factorization for general matrices.
Definition SparseLU.h:75
Scalar determinant()
Definition SparseLU.h:337
Scalar absDeterminant()
Definition SparseLU.h:253
const PermutationType & colsPermutation() const
Definition SparseLU.h:173
void factorize(const MatrixType &matrix)
Definition SparseLU.h:496
SparseLUMatrixLReturnType< SCMatrix > matrixL() const
Definition SparseLU.h:146
std::string lastErrorMessage() const
Definition SparseLU.h:211
Scalar signDeterminant()
Definition SparseLU.h:309
Scalar logAbsDeterminant() const
Definition SparseLU.h:283
void setPivotThreshold(const RealScalar &thresh)
Set the threshold used for a diagonal entry to be an acceptable pivot.
Definition SparseLU.h:178
void compute(const MatrixType &matrix)
Compute the symbolic and numeric factorization of the input sparse matrix.
Definition SparseLU.h:124
void analyzePattern(const MatrixType &matrix)
Compute the column permutation to minimize the fill-in.
Definition SparseLU.h:411
ComputationInfo info() const
Reports whether previous computation was successful.
Definition SparseLU.h:202
const PermutationType & rowsPermutation() const
Definition SparseLU.h:165
SparseLUMatrixUReturnType< SCMatrix, MappedSparseMatrix< Scalar, ColMajor, StorageIndex > > matrixU() const
Definition SparseLU.h:156
void isSymmetric(bool sym)
Indicate that the pattern of the input matrix is symmetric.
Definition SparseLU.h:135
Index rows() const
Definition SparseMatrix.h:131
Index cols() const
Definition SparseMatrix.h:133
A base class for sparse solvers.
Definition SparseSolverBase.h:54
const Solve< SparseLU< _MatrixType, _OrderingType >, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition SparseSolverBase.h:74
a class to manipulate the L supernodal factor from the SparseLU factorization
Definition SparseLU_SupernodalMatrix.h:34
Base class for sparseLU.
Definition SparseLUImpl.h:21
Definition XprHelper.h:50
ComputationInfo
Enum for reporting the status of a computation.
Definition Constants.h:430
@ NumericalIssue
The provided data did not satisfy the prerequisites.
Definition Constants.h:434
@ Success
Computation was successful.
Definition Constants.h:432
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
Definition SparseLU.h:705
Definition SparseLU.h:721