12 #ifndef EIGEN_SPARSE_LU_H 13 #define EIGEN_SPARSE_LU_H 17 template <
typename _MatrixType,
typename _OrderingType = COLAMDOrdering<
typename _MatrixType::StorageIndex> >
class SparseLU;
73 template <
typename _MatrixType,
typename _OrderingType>
78 using APIBase::m_isInitialized;
80 using APIBase::_solve_impl;
82 typedef _MatrixType MatrixType;
83 typedef _OrderingType OrderingType;
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>
192 #endif // EIGEN_PARSED_BY_DOXYGEN 204 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
216 template<
typename Rhs,
typename Dest>
219 Dest& X(X_base.derived());
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;
378 PermutationType m_perm_c;
379 PermutationType m_perm_r ;
385 bool m_symmetricmode;
388 RealScalar m_diagpivotthresh;
389 Index m_nnzL, m_nnzU;
390 Index m_detPermR, m_detPermC;
410 template <
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++)
447 if (!m_symmetricmode) {
456 for (
Index i = 0; i < m; ++i) iwork(post(i)) = post(m_etree(i));
461 for (
Index i = 0; i < m; i++)
462 post_perm.
indices()(i) = post(i);
465 if(m_perm_c.
size()) {
466 m_perm_c = post_perm * m_perm_c;
471 m_analysisIsOk =
true;
495 template <
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();
518 StorageIndex* outerIndexPtr_t =
new StorageIndex[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++)
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;
538 Index maxpanel = m_perfv.panel_size * m;
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);
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;
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;
677 for (i = 0; i < nseg; i++)
680 repfnz_k(irep) = emptyIdxLU;
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>
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);
716 const MappedSupernodalType& m_mapL;
719 template<
typename MatrixLType,
typename MatrixUType>
722 typedef typename MatrixLType::Scalar Scalar;
724 : m_mapL(mapL),m_mapU(mapU)
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];
738 Index nsupc = m_mapL.supToCol()[k+1] - fsupc;
739 Index luptr = m_mapL.colIndexPtr()[fsupc];
743 for (
Index j = 0; j < nrhs; j++)
745 X(fsupc, j) /= m_mapL.valuePtr()[luptr];
752 U =
A.template triangularView<Upper>().
solve(U);
755 for (
Index j = 0; j < nrhs; ++j)
757 for (
Index jcol = fsupc; jcol < fsupc + nsupc; jcol++)
759 typename MatrixUType::InnerIterator it(m_mapU, jcol);
762 Index irow = it.index();
763 X(irow, j) -= X(jcol, j) * it.
value();
769 const MatrixLType& m_mapL;
770 const MatrixUType& m_mapU;
EIGEN_DEVICE_FUNC ColXpr col(Index i)
Definition: DenseBase.h:839
Index column_bmod(const Index jcol, const Index nseg, BlockScalarVector dense, ScalarVector &tempv, BlockIndexVector segrep, BlockIndexVector repfnz, Index fpanelc, GlobalLU_t &glu)
Performs numeric block updates (sup-col) in topological order.
Definition: SparseLU_column_bmod.h:53
Index copy_to_ucol(const Index jcol, const Index nseg, IndexVector &segrep, BlockIndexVector repfnz, IndexVector &perm_r, BlockScalarVector dense, GlobalLU_t &glu)
Performs numeric block updates (sup-col) in topological order.
Definition: SparseLU_copy_to_ucol.h:50
int coletree(const MatrixType &mat, IndexVector &parent, IndexVector &firstRowElt, typename MatrixType::StorageIndex *perm=0)
Compute the column elimination tree of a sparse matrix.
Definition: SparseColEtree.h:61
void uncompress()
Turns the matrix into the uncompressed mode.
Definition: SparseMatrix.h:491
const PermutationType & rowsPermutation() const
Definition: SparseLU.h:165
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
Definition: PlainObjectBase.h:249
InverseReturnType inverse() const
Definition: PermutationMatrix.h:196
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:88
A base class for sparse solvers.
Definition: SparseSolverBase.h:67
Definition: SparseLU.h:19
const Solve< SparseLU< _MatrixType, _OrderingType >, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: SparseSolverBase.h:88
void relax_snode(const Index n, IndexVector &et, const Index relax_columns, IndexVector &descendants, IndexVector &relax_end)
Identify the initial relaxed supernodes.
Definition: SparseLU_relax_snode.h:47
void pruneL(const Index jcol, const IndexVector &perm_r, const Index pivrow, const Index nseg, const IndexVector &segrep, BlockIndexVector repfnz, IndexVector &xprune, GlobalLU_t &glu)
Prunes the L-structure.
Definition: SparseLU_pruneL.h:53
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
void heap_relax_snode(const Index n, IndexVector &et, const Index relax_columns, IndexVector &descendants, IndexVector &relax_end)
Identify the initial relaxed supernodes.
Definition: SparseLU_heap_relax_snode.h:46
EIGEN_DEVICE_FUNC CoeffReturnType value() const
Definition: DenseBase.h:470
void fixupL(const Index n, const IndexVector &perm_r, GlobalLU_t &glu)
Fix up the data storage lsub for L-subscripts.
Definition: SparseLU_Utils.h:52
Index cols() const
Definition: SparseMatrix.h:134
void countnz(const Index n, Index &nnzL, Index &nnzU, GlobalLU_t &glu)
Count Nonzero elements in the factors.
Definition: SparseLU_Utils.h:21
void factorize(const MatrixType &matrix)
Definition: SparseLU.h:496
Index nonZeros() const
Definition: SparseCompressedBase.h:56
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition: Constants.h:61
void panel_bmod(const Index m, const Index w, const Index jcol, const Index nseg, ScalarVector &dense, ScalarVector &tempv, IndexVector &segrep, IndexVector &repfnz, GlobalLU_t &glu)
Performs numeric block updates (sup-panel) in topological order.
Definition: SparseLU_panel_bmod.h:56
Definition: XprHelper.h:89
Sparse supernodal LU factorization for general matrices.
Definition: SparseLU.h:17
void panel_dfs(const Index m, const Index w, const Index jcol, MatrixType &A, IndexVector &perm_r, Index &nseg, ScalarVector &dense, IndexVector &panel_lsub, IndexVector &segrep, IndexVector &repfnz, IndexVector &xprune, IndexVector &marker, IndexVector &parent, IndexVector &xplore, GlobalLU_t &glu)
Performs a symbolic factorization on a panel of columns [jcol, jcol+w)
Definition: SparseLU_panel_dfs.h:219
void setPivotThreshold(const RealScalar &thresh)
Set the threshold used for a diagonal entry to be an acceptable pivot.
Definition: SparseLU.h:178
Scalar absDeterminant()
Definition: SparseLU.h:253
Definition: SparseLU_Structs.h:77
Index size() const
Definition: PermutationMatrix.h:108
SparseLUMatrixLReturnType< SCMatrix > matrixL() const
Definition: SparseLU.h:146
Expression of a fixed-size or dynamic-size sub-vector.
Definition: ForwardDeclarations.h:87
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Resizes *this to a rows x cols matrix.
Definition: PlainObjectBase.h:273
const IndicesType & indices() const
const version of indices().
Definition: PermutationMatrix.h:388
void compute(const MatrixType &matrix)
Compute the symbolic and numeric factorization of the input sparse matrix.
Definition: SparseLU.h:124
const StorageIndex * innerNonZeroPtr() const
Definition: SparseMatrix.h:171
void isSymmetric(bool sym)
Indicate that the pattern of the input matrix is symmetric.
Definition: SparseLU.h:135
The provided data did not satisfy the prerequisites.
Definition: Constants.h:434
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Index pivotL(const Index jcol, const RealScalar &diagpivotthresh, IndexVector &perm_r, IndexVector &iperm_c, Index &pivrow, GlobalLU_t &glu)
Performs the numerical pivotin on the current column of L, and the CDIV operation.
Definition: SparseLU_pivotL.h:60
Index rows() const
Definition: SparseMatrix.h:132
void setInfos(Index m, Index n, ScalarVector &nzval, IndexVector &nzval_colptr, IndexVector &rowind, IndexVector &rowind_colptr, IndexVector &col_to_sup, IndexVector &sup_to_col)
Set appropriate pointers for the lower triangular supernodal matrix These infos are available at the ...
Definition: SparseLU_SupernodalMatrix.h:61
Scalar determinant()
Definition: SparseLU.h:337
Index column_dfs(const Index m, const Index jcol, IndexVector &perm_r, Index maxsuper, Index &nseg, BlockIndexVector lsub_col, IndexVector &segrep, BlockIndexVector repfnz, IndexVector &xprune, IndexVector &marker, IndexVector &parent, IndexVector &xplore, GlobalLU_t &glu)
Performs a symbolic factorization on column jcol and decide the supernode boundary.
Definition: SparseLU_column_dfs.h:93
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Resizes to the given size, and sets all coefficients in this expression to zero.
Definition: CwiseNullaryOp.h:515
Computation was successful.
Definition: Constants.h:432
EIGEN_DEVICE_FUNC Derived & setConstant(Index size, const Scalar &val)
Resizes to the given size, and sets all coefficients in this expression to the given value val...
Definition: CwiseNullaryOp.h:341
Base class for sparseLU.
Definition: SparseLUImpl.h:20
Definition: SparseLU.h:18
SparseLUMatrixUReturnType< SCMatrix, MappedSparseMatrix< Scalar, ColMajor, StorageIndex > > matrixU() const
Definition: SparseLU.h:156
const StorageIndex * outerIndexPtr() const
Definition: SparseMatrix.h:162
Index memInit(Index m, Index n, Index annz, Index lwork, Index fillratio, Index panel_size, GlobalLU_t &glu)
Allocate various working space for the numerical factorization phase.
Definition: SparseLU_Memory.h:151
std::string lastErrorMessage() const
Definition: SparseLU.h:211
Scalar logAbsDeterminant() const
Definition: SparseLU.h:283
void treePostorder(typename IndexVector::Scalar n, IndexVector &parent, IndexVector &post)
Post order a tree.
Definition: SparseColEtree.h:178
const PermutationType & colsPermutation() const
Definition: SparseLU.h:173
Pseudo expression representing a solving operation.
Definition: Solve.h:62
void analyzePattern(const MatrixType &matrix)
Compute the column permutation to minimize the fill-in.
Definition: SparseLU.h:411
Definition: SparseLU_Structs.h:96
Convenience specialization of Stride to specify only an outer stride See class Map for some examples...
Definition: Stride.h:101
void resize(Index newSize)
Resizes to given size.
Definition: PermutationMatrix.h:136
ComputationInfo
Enum for reporting the status of a computation.
Definition: Constants.h:430
Index determinant() const
Definition: PermutationMatrix.h:253
Scalar signDeterminant()
Definition: SparseLU.h:309
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: SparseLU.h:202