11 #ifndef EIGEN_SPARSE_LU_H
12 #define EIGEN_SPARSE_LU_H
19 template <
typename MatrixType_,
typename OrderingType_ = COLAMDOrdering<
typename MatrixType_::StorageIndex>>
21 template <
typename MappedSparseMatrixType>
22 struct SparseLUMatrixLReturnType;
23 template <
typename MatrixLType,
typename MatrixUType>
24 struct SparseLUMatrixUReturnType;
26 template <
bool Conjugate,
class SparseLUType>
48 template <
typename Rhs,
typename Dest>
50 Dest&
X(X_base.derived());
56 X.col(
j) =
m_sparseLU->colsPermutation() *
B.const_cast_derived().col(
j);
59 m_sparseLU->matrixU().template solveTransposedInPlace<Conjugate>(
X);
62 m_sparseLU->matrixL().template solveTransposedInPlace<Conjugate>(
X);
149 template <
typename MatrixType_,
typename OrderingType_>
233 return transposeView;
303 #ifdef EIGEN_PARSED_BY_DOXYGEN
312 template <
typename Rhs>
337 template <
typename Rhs,
typename Dest>
339 Dest&
X(X_base.derived());
345 X.resize(
B.rows(),
B.cols());
351 this->
matrixL().solveInPlace(X);
352 this->
matrixU().solveInPlace(X);
379 for (
typename SCMatrix::InnerIterator it(
m_Lstore,
j); it; ++it) {
380 if (it.index() ==
j) {
381 det *=
abs(it.value());
406 for (
typename SCMatrix::InnerIterator it(
m_Lstore,
j); it; ++it) {
407 if (it.row() <
j)
continue;
409 det +=
log(
abs(it.value()));
430 for (
typename SCMatrix::InnerIterator it(
m_Lstore,
j); it; ++it) {
431 if (it.index() ==
j) {
434 else if (it.value() == 0)
456 for (
typename SCMatrix::InnerIterator it(
m_Lstore,
j); it; ++it) {
457 if (it.index() ==
j) {
527 template <
typename MatrixType,
typename OrderingType>
536 ord(m_mat, m_perm_c);
539 if (m_perm_c.size()) {
550 IndexVector::Map(outerIndexPtr,
mat.
cols() + 1) = IndexVector::Map(m_mat.outerIndexPtr(),
mat.
cols() + 1);
554 m_mat.outerIndexPtr()[m_perm_c.indices()(
i)] = outerIndexPtr[
i];
555 m_mat.innerNonZeroPtr()[m_perm_c.indices()(
i)] = outerIndexPtr[
i + 1] - outerIndexPtr[
i];
564 if (!m_symmetricmode) {
572 for (
Index i = 0;
i <
m; ++
i) iwork(post(
i)) = post(m_etree(
i));
580 if (m_perm_c.size()) {
581 m_perm_c = post_perm * m_perm_c;
586 m_analysisIsOk =
true;
610 template <
typename MatrixType,
typename OrderingType>
613 eigen_assert(m_analysisIsOk &&
"analyzePattern() should be called first");
616 m_isInitialized =
true;
621 if (m_perm_c.size()) {
625 if (
matrix.isCompressed())
626 outerIndexPtr =
matrix.outerIndexPtr();
629 for (
Index i = 0;
i <=
matrix.cols();
i++) outerIndexPtr_t[
i] = m_mat.outerIndexPtr()[
i];
630 outerIndexPtr = outerIndexPtr_t;
633 m_mat.outerIndexPtr()[m_perm_c.indices()(
i)] = outerIndexPtr[
i];
634 m_mat.innerNonZeroPtr()[m_perm_c.indices()(
i)] = outerIndexPtr[
i + 1] - outerIndexPtr[
i];
636 if (!
matrix.isCompressed())
delete[] outerIndexPtr;
638 m_perm_c.resize(
matrix.cols());
644 Index nnz = m_mat.nonZeros();
645 Index maxpanel = m_perfv.panel_size *
m;
650 Index info = Base::memInit(
m,
n, nnz, lwork, m_perfv.fillfactor, m_perfv.panel_size, m_glu);
652 m_lastError =
"UNABLE TO ALLOCATE WORKING MEMORY\n\n";
653 m_factorizationIsOk =
false;
685 if (m_symmetricmode ==
true)
691 m_perm_r.indices().setConstant(-1);
696 m_glu.xsup.setConstant(0);
697 m_glu.xsup(0) = m_glu.xlsub(0) = m_glu.xusub(0) = m_glu.xlusup(0) =
Index(0);
708 for (jcol = 0; jcol <
n;) {
710 Index panel_size = m_perfv.panel_size;
711 for (
k = jcol + 1;
k < (
std::min)(jcol + panel_size,
n);
k++) {
713 panel_size =
k - jcol;
717 if (
k ==
n) panel_size =
n - jcol;
720 Base::panel_dfs(
m, panel_size, jcol, m_mat, m_perm_r.indices(), nseg1, dense, panel_lsub, segrep, repfnz, xprune,
721 marker, parent, xplore, m_glu);
724 Base::panel_bmod(
m, panel_size, jcol, nseg1, dense, tempv, segrep, repfnz, m_glu);
727 for (jj = jcol; jj < jcol + panel_size; jj++) {
735 info = Base::column_dfs(
m, jj, m_perm_r.indices(), m_perfv.maxsuper, nseg, panel_lsubk, segrep, repfnz_k, xprune,
736 marker, parent, xplore, m_glu);
738 m_lastError =
"UNABLE TO EXPAND MEMORY IN COLUMN_DFS() ";
740 m_factorizationIsOk =
false;
747 info = Base::column_bmod(jj, (nseg - nseg1), dense_k, tempv, segrep_k, repfnz_k, jcol, m_glu);
749 m_lastError =
"UNABLE TO EXPAND MEMORY IN COLUMN_BMOD() ";
751 m_factorizationIsOk =
false;
757 info = Base::copy_to_ucol(jj, nseg, segrep, repfnz_k, m_perm_r.indices(), dense_k, m_glu);
759 m_lastError =
"UNABLE TO EXPAND MEMORY IN COPY_TO_UCOL() ";
761 m_factorizationIsOk =
false;
767 info = Base::pivotL(jj, m_diagpivotthresh, m_perm_r.indices(), iperm_c.
indices(), pivrow, m_glu);
769 m_lastError =
"THE MATRIX IS STRUCTURALLY SINGULAR";
771 std::ostringstream returnInfo;
772 returnInfo <<
" ... ZERO COLUMN AT ";
774 m_lastError += returnInfo.str();
777 m_factorizationIsOk =
false;
784 if (pivrow != jj) m_detPermR = -m_detPermR;
787 Base::pruneL(jj, m_perm_r.indices(), pivrow, nseg, segrep, repfnz_k, xprune, m_glu);
790 for (
i = 0;
i < nseg;
i++) {
798 m_detPermR = m_perm_r.determinant();
799 m_detPermC = m_perm_c.determinant();
807 m_Lstore.setInfos(
m,
n, m_glu.lusup, m_glu.xlusup, m_glu.lsub, m_glu.xlsub, m_glu.supno, m_glu.xsup);
813 m_factorizationIsOk =
true;
816 template <
typename MappedSupernodalType>
822 template <
typename Dest>
826 template <
bool Conjugate,
typename Dest>
828 m_mapL.template solveTransposedInPlace<Conjugate>(
X);
832 ArrayXi colCount = ArrayXi::Ones(
cols());
834 typename MappedSupernodalType::InnerIterator iter(
m_mapL,
i);
835 for (; iter; ++iter) {
836 if (iter.row() > iter.col()) {
837 colCount(iter.col())++;
845 typename MappedSupernodalType::InnerIterator iter(
m_mapL,
i);
846 for (; iter; ++iter) {
847 if (iter.row() > iter.col()) {
848 sL.
insert(iter.row(), iter.col()) = iter.value();
859 template <
typename MatrixLType,
typename MatrixUType>
866 template <
typename Dest>
878 X(fsupc,
j) /=
m_mapL.valuePtr()[luptr];
884 typename Dest::RowsBlockXpr
U =
X.derived().middleRows(fsupc, nsupc);
885 U =
A.template triangularView<Upper>().solve(
U);
889 for (
Index jcol = fsupc; jcol < fsupc + nsupc; jcol++) {
890 typename MatrixUType::InnerIterator it(
m_mapU, jcol);
892 Index irow = it.index();
893 X(irow,
j) -=
X(jcol,
j) * it.value();
900 template <
bool Conjugate,
typename Dest>
912 for (
Index jcol = fsupc; jcol < fsupc + nsupc; jcol++) {
913 typename MatrixUType::InnerIterator it(
m_mapU, jcol);
915 Index irow = it.index();
927 typename Dest::RowsBlockXpr
U =
X.derived().middleRows(fsupc, nsupc);
929 U =
A.adjoint().template triangularView<Lower>().solve(
U);
931 U =
A.transpose().template triangularView<Lower>().solve(
U);
939 typename MatrixLType::InnerIterator iter(
m_mapL,
i);
940 for (; iter; ++iter) {
941 if (iter.row() <= iter.col()) {
942 rowCount(iter.row())++;
950 typename MatrixLType::InnerIterator iter(
m_mapL,
i);
951 for (; iter; ++iter) {
952 if (iter.row() <= iter.col()) {
953 sU.
insert(iter.row(), iter.col()) = iter.value();
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
AnnoyingScalar conj(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:133
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Eigen::SparseMatrix< double > mat
Definition: EigenUnitTest.cpp:10
#define eigen_assert(x)
Definition: Macros.h:910
#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER)
Definition: Memory.h:806
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
SCALAR Scalar
Definition: bench_gemm.cpp:45
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Definition: ForwardDeclarations.h:102
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Convenience specialization of Stride to specify only an outer stride See class Map for some examples.
Definition: Stride.h:104
InverseReturnType inverse() const
Definition: PermutationMatrix.h:172
const IndicesType & indices() const
Definition: PermutationMatrix.h:334
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:569
EIGEN_DEVICE_FUNC Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:365
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Definition: PlainObjectBase.h:294
Pseudo expression representing a solving operation.
Definition: Solve.h:62
Definition: SparseLU.h:27
SparseLUType::MatrixType MatrixType
Definition: SparseLU.h:35
Index cols() const
Definition: SparseLU.h:69
SparseLUType * m_sparseLU
Definition: SparseLU.h:72
void setSparseLU(SparseLUType *sparseLU)
Definition: SparseLU.h:46
SparseSolverBase< SparseLUTransposeView< Conjugate, SparseLUType > > APIBase
Definition: SparseLU.h:29
@ MaxColsAtCompileTime
Definition: SparseLU.h:38
@ ColsAtCompileTime
Definition: SparseLU.h:38
SparseLUType::Scalar Scalar
Definition: SparseLU.h:33
bool _solve_impl(const MatrixBase< Rhs > &B, MatrixBase< Dest > &X_base) const
Definition: SparseLU.h:49
SparseLUTransposeView & operator=(const SparseLUTransposeView &)
void setIsInitialized(const bool isInitialized)
Definition: SparseLU.h:45
SparseLUTransposeView()
Definition: SparseLU.h:40
Index rows() const
Definition: SparseLU.h:68
SparseLUType::OrderingType OrderingType
Definition: SparseLU.h:36
SparseLUTransposeView(const SparseLUTransposeView &view)
Definition: SparseLU.h:41
SparseLUType::StorageIndex StorageIndex
Definition: SparseLU.h:34
Sparse supernodal LU factorization for general matrices.
Definition: SparseLU.h:151
SparseLU(const SparseLU &)
std::string m_lastError
Definition: SparseLU.h:488
void setPivotThreshold(const RealScalar &thresh)
Definition: SparseLU.h:301
Index cols() const
Give the number of columns.
Definition: SparseLU.h:262
Scalar logAbsDeterminant() const
Give the natural log of the absolute determinant.
Definition: SparseLU.h:399
Matrix< Scalar, Dynamic, 1 > ScalarVector
Definition: SparseLU.h:166
Index rows() const
Give the number of rows.
Definition: SparseLU.h:259
~SparseLU()
Definition: SparseLU.h:192
Index nnzU() const
Give the number of non zero in matrix U.
Definition: SparseLU.h:471
MatrixType::Scalar Scalar
Definition: SparseLU.h:161
SparseSolverBase< SparseLU< MatrixType_, OrderingType_ > > APIBase
Definition: SparseLU.h:153
bool m_factorizationIsOk
Definition: SparseLU.h:486
SparseLUMatrixUReturnType< SCMatrix, Map< SparseMatrix< Scalar, ColMajor, StorageIndex > > > matrixU() const
Give the MatrixU.
Definition: SparseLU.h:284
MatrixType::RealScalar RealScalar
Definition: SparseLU.h:162
PermutationType m_perm_c
Definition: SparseLU.h:492
void factorize(const MatrixType &matrix)
Factorize the matrix to get the solver ready.
Definition: SparseLU.h:611
Index m_detPermR
Definition: SparseLU.h:504
const PermutationType & rowsPermutation() const
Give the row matrix permutation.
Definition: SparseLU.h:293
NCMatrix m_mat
Definition: SparseLU.h:489
Map< SparseMatrix< Scalar, ColMajor, StorageIndex > > m_Ustore
Definition: SparseLU.h:491
std::string lastErrorMessage() const
Give a human readable error.
Definition: SparseLU.h:335
Matrix< StorageIndex, Dynamic, 1 > IndexVector
Definition: SparseLU.h:167
SparseLUMatrixLReturnType< SCMatrix > matrixL() const
Give the matrixL.
Definition: SparseLU.h:275
SCMatrix m_Lstore
Definition: SparseLU.h:490
Index m_nnzU
Definition: SparseLU.h:503
void compute(const MatrixType &matrix)
Analyze and factorize the matrix so the solver is ready to solve.
Definition: SparseLU.h:210
const SparseLUTransposeView< false, SparseLU< MatrixType_, OrderingType_ > > transpose()
Return a solver for the transposed matrix.
Definition: SparseLU.h:229
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: SparseLU.h:326
bool m_analysisIsOk
Definition: SparseLU.h:487
Scalar signDeterminant()
Give the sign of the determinant.
Definition: SparseLU.h:423
SparseMatrix< Scalar, ColMajor, StorageIndex > NCMatrix
Definition: SparseLU.h:164
void initperfvalues()
Definition: SparseLU.h:475
@ ColsAtCompileTime
Definition: SparseLU.h:171
@ MaxColsAtCompileTime
Definition: SparseLU.h:171
bool _solve_impl(const MatrixBase< Rhs > &B, MatrixBase< Dest > &X_base) const
Definition: SparseLU.h:338
void simplicialfactorize(const MatrixType &matrix)
bool m_symmetricmode
Definition: SparseLU.h:499
SparseLU()
Basic constructor of the solver.
Definition: SparseLU.h:178
Scalar absDeterminant()
Give the absolute value of the determinant.
Definition: SparseLU.h:371
internal::perfvalues m_perfv
Definition: SparseLU.h:501
OrderingType_ OrderingType
Definition: SparseLU.h:160
Index m_detPermC
Definition: SparseLU.h:504
internal::MappedSuperNodalMatrix< Scalar, StorageIndex > SCMatrix
Definition: SparseLU.h:165
void analyzePattern(const MatrixType &matrix)
Compute the column permutation.
Definition: SparseLU.h:528
Index nnzL() const
Give the number of non zero in matrix L.
Definition: SparseLU.h:468
Index m_nnzL
Definition: SparseLU.h:503
PermutationMatrix< Dynamic, Dynamic, StorageIndex > PermutationType
Definition: SparseLU.h:168
MatrixType_ MatrixType
Definition: SparseLU.h:159
IndexVector m_etree
Definition: SparseLU.h:494
PermutationType m_perm_r
Definition: SparseLU.h:493
void isSymmetric(bool sym)
Let you set that the pattern of the input matrix is symmetric.
Definition: SparseLU.h:265
const PermutationType & colsPermutation() const
Give the column matrix permutation.
Definition: SparseLU.h:299
Base::GlobalLU_t m_glu
Definition: SparseLU.h:496
RealScalar m_diagpivotthresh
Definition: SparseLU.h:502
SparseLU(const MatrixType &matrix)
Constructor of the solver already based on a specific matrix.
Definition: SparseLU.h:186
Scalar determinant()
Give the determinant.
Definition: SparseLU.h:449
const SparseLUTransposeView< true, SparseLU< MatrixType_, OrderingType_ > > adjoint()
Return a solver for the adjointed matrix.
Definition: SparseLU.h:250
MatrixType::StorageIndex StorageIndex
Definition: SparseLU.h:163
ComputationInfo m_info
Definition: SparseLU.h:485
internal::SparseLUImpl< Scalar, StorageIndex > Base
Definition: SparseLU.h:169
Index cols() const
Definition: SparseMatrix.h:161
void makeCompressed()
Definition: SparseMatrix.h:589
bool isCompressed() const
Definition: SparseCompressedBase.h:114
Index rows() const
Definition: SparseMatrix.h:159
const StorageIndex * outerIndexPtr() const
Definition: SparseMatrix.h:189
void reserve(Index reserveSize)
Definition: SparseMatrix.h:315
Scalar & insert(Index row, Index col)
Definition: SparseMatrix.h:1586
A base class for sparse solvers.
Definition: SparseSolverBase.h:67
const Solve< SparseLU< MatrixType_, OrderingType_ >, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: SparseSolverBase.h:84
void _solve_impl(const SparseMatrixBase< Rhs > &b, SparseMatrixBase< Dest > &dest) const
Definition: SparseSolverBase.h:104
bool m_isInitialized
Definition: SparseSolverBase.h:110
Expression of a fixed-size or dynamic-size sub-vector.
Definition: VectorBlock.h:58
Definition: SparseLUImpl.h:23
Definition: XprHelper.h:134
Definition: matrices.h:74
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85
#define min(a, b)
Definition: datatypes.h:22
ComputationInfo
Definition: Constants.h:438
@ NumericalIssue
Definition: Constants.h:442
@ Success
Definition: Constants.h:440
const unsigned int RowMajorBit
Definition: Constants.h:70
#define X
Definition: icosphere.cpp:20
const char const int const RealScalar const RealScalar const int * lda
Definition: level2_cplx_impl.h:20
int * m
Definition: level2_cplx_impl.h:294
int info
Definition: level2_cplx_impl.h:39
char char char int int * k
Definition: level2_impl.h:374
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 log(const bfloat16 &a)
Definition: BFloat16.h:618
Index LUnumTempV(Index &m, Index &w, Index &t, Index &b)
Definition: SparseLU_Memory.h:42
@ LUNoMarker
Definition: SparseLU_Memory.h:40
@ emptyIdxLU
Definition: SparseLU_Memory.h:41
int coletree(const MatrixType &mat, IndexVector &parent, IndexVector &firstRowElt, typename MatrixType::StorageIndex *perm=0)
Definition: SparseColEtree.h:61
void treePostorder(typename IndexVector::Scalar n, IndexVector &parent, IndexVector &post)
Post order a tree.
Definition: SparseColEtree.h:168
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const AutoDiffScalar< DerType > & conj(const AutoDiffScalar< DerType > &x)
Definition: AutoDiffScalar.h:482
double U
Swimming speed.
Definition: two_d_variable_diff_adapt.cc:53
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
double Zero
Definition: pseudosolid_node_update_elements.cc:35
void fixupL(const int, const int *, GlobalLU_t *)
void countnz(const int, int *, int *, int *, GlobalLU_t *)
void relax_snode(const int, int *, const int, int *, int *)
void heap_relax_snode(const int, int *, const int, int *, int *)
Definition: SparseLU.h:817
void solveTransposedInPlace(MatrixBase< Dest > &X) const
Definition: SparseLU.h:827
const MappedSupernodalType & m_mapL
Definition: SparseLU.h:856
Index rows() const
Definition: SparseLU.h:820
Index cols() const
Definition: SparseLU.h:821
SparseLUMatrixLReturnType(const MappedSupernodalType &mapL)
Definition: SparseLU.h:819
void solveInPlace(MatrixBase< Dest > &X) const
Definition: SparseLU.h:823
SparseMatrix< Scalar, ColMajor, Index > toSparse() const
Definition: SparseLU.h:831
MappedSupernodalType::Scalar Scalar
Definition: SparseLU.h:818
Definition: SparseLU.h:860
const MatrixUType & m_mapU
Definition: SparseLU.h:964
Index cols() const
Definition: SparseLU.h:864
SparseMatrix< Scalar, RowMajor, Index > toSparse()
Definition: SparseLU.h:936
SparseLUMatrixUReturnType(const MatrixLType &mapL, const MatrixUType &mapU)
Definition: SparseLU.h:862
Index rows() const
Definition: SparseLU.h:863
void solveInPlace(MatrixBase< Dest > &X) const
Definition: SparseLU.h:867
const MatrixLType & m_mapL
Definition: SparseLU.h:963
void solveTransposedInPlace(MatrixBase< Dest > &X) const
Definition: SparseLU.h:901
MatrixLType::Scalar Scalar
Definition: SparseLU.h:861
Definition: SparseLU_Structs.h:80
Definition: SparseLU_Structs.h:99
Index relax
Definition: SparseLU_Structs.h:101
Index panel_size
Definition: SparseLU_Structs.h:100
Index maxsuper
Definition: SparseLU_Structs.h:104
Index rowblk
Definition: SparseLU_Structs.h:105
Index colblk
Definition: SparseLU_Structs.h:106
Index fillfactor
Definition: SparseLU_Structs.h:107
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2