Eigen::SimplicialCholeskyBase< Derived > Class Template Reference

A base class for direct sparse Cholesky factorizations. More...

+ Inheritance diagram for Eigen::SimplicialCholeskyBase< Derived >:

Classes

struct  keep_diag
 

Public Types

enum  { UpLo }
 
enum  {
  ColsAtCompileTime ,
  MaxColsAtCompileTime
}
 
typedef SparseMatrix< Scalar, ColMajor, StorageIndexCholMatrixType
 
typedef CholMatrixType const * ConstCholMatrixPtr
 
typedef internal::traits< Derived >::MatrixType MatrixType
 
typedef internal::traits< Derived >::OrderingType OrderingType
 
typedef MatrixType::RealScalar RealScalar
 
typedef MatrixType::Scalar Scalar
 
typedef MatrixType::StorageIndex StorageIndex
 
typedef Matrix< StorageIndex, Dynamic, 1 > VectorI
 
typedef Matrix< Scalar, Dynamic, 1 > VectorType
 

Public Member Functions

Index cols () const
 
Derived & derived ()
 
Derived & derived ()
 
const Derived & derived () const
 
const Derived & derived () const
 
ComputationInfo info () const
 Reports whether previous computation was successful. More...
 
const PermutationMatrix< Dynamic, Dynamic, StorageIndex > & permutationP () const
 
const PermutationMatrix< Dynamic, Dynamic, StorageIndex > & permutationPinv () const
 
Index rows () const
 
Derived & setShift (const RealScalar &offset, const RealScalar &scale=1)
 
 SimplicialCholeskyBase ()
 
 SimplicialCholeskyBase (const MatrixType &matrix)
 
 ~SimplicialCholeskyBase ()
 
- Public Member Functions inherited from Eigen::SparseSolverBase< Derived >
Derived & derived ()
 
const Derived & derived () const
 
template<typename Rhs >
const Solve< Derived, Rhs > solve (const MatrixBase< Rhs > &b) const
 
template<typename Rhs >
const Solve< Derived, Rhs > solve (const SparseMatrixBase< Rhs > &b) const
 
 SparseSolverBase ()
 
 SparseSolverBase (SparseSolverBase &&other)
 
 ~SparseSolverBase ()
 

Protected Member Functions

void analyzePattern (const MatrixType &a, bool doLDLT)
 
void analyzePattern_preordered (const CholMatrixType &a, bool doLDLT)
 
template<bool DoLDLT>
void compute (const MatrixType &matrix)
 
template<bool DoLDLT>
void factorize (const MatrixType &a)
 
template<bool DoLDLT>
void factorize_preordered (const CholMatrixType &a)
 
void ordering (const MatrixType &a, ConstCholMatrixPtr &pmat, CholMatrixType &ap)
 

Protected Attributes

bool m_analysisIsOk
 
VectorType m_diag
 
bool m_factorizationIsOk
 
ComputationInfo m_info
 
CholMatrixType m_matrix
 
VectorI m_nonZerosPerCol
 
PermutationMatrix< Dynamic, Dynamic, StorageIndexm_P
 
VectorI m_parent
 
PermutationMatrix< Dynamic, Dynamic, StorageIndexm_Pinv
 
RealScalar m_shiftOffset
 
RealScalar m_shiftScale
 
- Protected Attributes inherited from Eigen::SparseSolverBase< Derived >
bool m_isInitialized
 

Private Types

typedef SparseSolverBase< Derived > Base
 

Private Attributes

bool m_isInitialized
 

Detailed Description

template<typename Derived>
class Eigen::SimplicialCholeskyBase< Derived >

A base class for direct sparse Cholesky factorizations.

This is a base class for LL^T and LDL^T Cholesky factorizations of sparse matrices that are selfadjoint and positive definite. These factorizations allow for solving A.X = B where X and B can be either dense or sparse.

In order to reduce the fill-in, a symmetric permutation P is applied prior to the factorization such that the factorized matrix is P A P^-1.

Template Parameters
Derivedthe type of the derived class, that is the actual factorization type.

Definition at line 57 of file SimplicialCholesky.h.

Member Typedef Documentation

◆ Base

template<typename Derived >
typedef SparseSolverBase<Derived> Eigen::SimplicialCholeskyBase< Derived >::Base
private

Definition at line 59 of file SimplicialCholesky.h.

◆ CholMatrixType

template<typename Derived >
typedef SparseMatrix<Scalar,ColMajor,StorageIndex> Eigen::SimplicialCholeskyBase< Derived >::CholMatrixType

Definition at line 69 of file SimplicialCholesky.h.

◆ ConstCholMatrixPtr

template<typename Derived >
typedef CholMatrixType const* Eigen::SimplicialCholeskyBase< Derived >::ConstCholMatrixPtr

Definition at line 70 of file SimplicialCholesky.h.

◆ MatrixType

template<typename Derived >
typedef internal::traits<Derived>::MatrixType Eigen::SimplicialCholeskyBase< Derived >::MatrixType

Definition at line 63 of file SimplicialCholesky.h.

◆ OrderingType

template<typename Derived >
typedef internal::traits<Derived>::OrderingType Eigen::SimplicialCholeskyBase< Derived >::OrderingType

Definition at line 64 of file SimplicialCholesky.h.

◆ RealScalar

template<typename Derived >
typedef MatrixType::RealScalar Eigen::SimplicialCholeskyBase< Derived >::RealScalar

Definition at line 67 of file SimplicialCholesky.h.

◆ Scalar

template<typename Derived >
typedef MatrixType::Scalar Eigen::SimplicialCholeskyBase< Derived >::Scalar

Definition at line 66 of file SimplicialCholesky.h.

◆ StorageIndex

template<typename Derived >
typedef MatrixType::StorageIndex Eigen::SimplicialCholeskyBase< Derived >::StorageIndex

Definition at line 68 of file SimplicialCholesky.h.

◆ VectorI

template<typename Derived >
typedef Matrix<StorageIndex,Dynamic,1> Eigen::SimplicialCholeskyBase< Derived >::VectorI

Definition at line 72 of file SimplicialCholesky.h.

◆ VectorType

template<typename Derived >
typedef Matrix<Scalar,Dynamic,1> Eigen::SimplicialCholeskyBase< Derived >::VectorType

Definition at line 71 of file SimplicialCholesky.h.

Member Enumeration Documentation

◆ anonymous enum

template<typename Derived >
anonymous enum
Enumerator
UpLo 

Definition at line 65 of file SimplicialCholesky.h.

65 { UpLo = internal::traits<Derived>::UpLo };

◆ anonymous enum

template<typename Derived >
anonymous enum
Enumerator
ColsAtCompileTime 
MaxColsAtCompileTime 

Definition at line 74 of file SimplicialCholesky.h.

74  {
75  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
76  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
77  };

Constructor & Destructor Documentation

◆ SimplicialCholeskyBase() [1/2]

template<typename Derived >
Eigen::SimplicialCholeskyBase< Derived >::SimplicialCholeskyBase ( )
inline

Default constructor

Definition at line 84 of file SimplicialCholesky.h.

◆ SimplicialCholeskyBase() [2/2]

template<typename Derived >
Eigen::SimplicialCholeskyBase< Derived >::SimplicialCholeskyBase ( const MatrixType matrix)
inlineexplicit

Definition at line 92 of file SimplicialCholesky.h.

93  : m_info(Success),
94  m_factorizationIsOk(false),
95  m_analysisIsOk(false),
96  m_shiftOffset(0),
97  m_shiftScale(1)
98  {
99  derived().compute(matrix);
100  }

◆ ~SimplicialCholeskyBase()

template<typename Derived >
Eigen::SimplicialCholeskyBase< Derived >::~SimplicialCholeskyBase ( )
inline

Definition at line 102 of file SimplicialCholesky.h.

103  {
104  }

Member Function Documentation

◆ analyzePattern()

template<typename Derived >
void Eigen::SimplicialCholeskyBase< Derived >::analyzePattern ( const MatrixType a,
bool  doLDLT 
)
inlineprotected

Definition at line 240 of file SimplicialCholesky.h.

241  {
242  eigen_assert(a.rows()==a.cols());
243  Index size = a.cols();
244  CholMatrixType tmp(size,size);
245  ConstCholMatrixPtr pmat;
246  ordering(a, pmat, tmp);
247  analyzePattern_preordered(*pmat,doLDLT);
248  }
#define eigen_assert(x)
Definition: Macros.h:902
CholMatrixType const * ConstCholMatrixPtr
void ordering(const MatrixType &a, ConstCholMatrixPtr &pmat, CholMatrixType &ap)
void analyzePattern_preordered(const CholMatrixType &a, bool doLDLT)
SparseMatrix< Scalar, ColMajor, StorageIndex > CholMatrixType
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82

◆ analyzePattern_preordered()

template<typename Derived >
void Eigen::SimplicialCholeskyBase< Derived >::analyzePattern_preordered ( const CholMatrixType a,
bool  doLDLT 
)
protected

Definition at line 28 of file SimplicialCholesky_impl.h.

29 {
30  const StorageIndex size = StorageIndex(ap.rows());
34 
36 
37  for(StorageIndex k = 0; k < size; ++k)
38  {
39  /* L(k,:) pattern: all nodes reachable in etree from nz in A(0:k-1,k) */
40  m_parent[k] = -1; /* parent of k is not yet known */
41  tags[k] = k; /* mark node k as visited */
42  m_nonZerosPerCol[k] = 0; /* count of nonzeros in column k of L */
43  for(typename CholMatrixType::InnerIterator it(ap,k); it; ++it)
44  {
45  StorageIndex i = it.index();
46  if(i < k)
47  {
48  /* follow path from i to root of etree, stop at flagged node */
49  for(; tags[i] != k; i = m_parent[i])
50  {
51  /* find parent of i if not yet determined */
52  if (m_parent[i] == -1)
53  m_parent[i] = k;
54  m_nonZerosPerCol[i]++; /* L (k,i) is nonzero */
55  tags[i] = k; /* mark i as visited */
56  }
57  }
58  }
59  }
60 
61  /* construct Lp index array from m_nonZerosPerCol column counts */
63  Lp[0] = 0;
64  for(StorageIndex k = 0; k < size; ++k)
65  Lp[k+1] = Lp[k] + m_nonZerosPerCol[k] + (doLDLT ? 0 : 1);
66 
68 
69  m_isInitialized = true;
70  m_info = Success;
71  m_analysisIsOk = true;
72  m_factorizationIsOk = false;
73 }
#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER)
Definition: Memory.h:847
constexpr void resize(Index rows, Index cols)
MatrixType::StorageIndex StorageIndex
void resize(Index rows, Index cols)
Definition: SparseMatrix.h:739
void resizeNonZeros(Index size)
Definition: SparseMatrix.h:758
const StorageIndex * outerIndexPtr() const
Definition: SparseMatrix.h:195

◆ cols()

template<typename Derived >
Index Eigen::SimplicialCholeskyBase< Derived >::cols ( void  ) const
inline

Definition at line 109 of file SimplicialCholesky.h.

109 { return m_matrix.cols(); }
Index cols() const
Definition: SparseMatrix.h:167

◆ compute()

template<typename Derived >
template<bool DoLDLT>
void Eigen::SimplicialCholeskyBase< Derived >::compute ( const MatrixType matrix)
inlineprotected

Computes the sparse Cholesky decomposition of matrix

Definition at line 204 of file SimplicialCholesky.h.

205  {
206  eigen_assert(matrix.rows()==matrix.cols());
207  Index size = matrix.cols();
208  CholMatrixType tmp(size,size);
209  ConstCholMatrixPtr pmat;
210  ordering(matrix, pmat, tmp);
211  analyzePattern_preordered(*pmat, DoLDLT);
212  factorize_preordered<DoLDLT>(*pmat);
213  }

◆ derived() [1/4]

template<typename Derived >
Derived& Eigen::SparseSolverBase< Derived >::derived
inline

Definition at line 83 of file SparseSolverBase.h.

83 { return *static_cast<Derived*>(this); }

◆ derived() [2/4]

template<typename Derived >
Derived& Eigen::SimplicialCholeskyBase< Derived >::derived ( )
inline

Definition at line 106 of file SimplicialCholesky.h.

106 { return *static_cast<Derived*>(this); }

◆ derived() [3/4]

template<typename Derived >
const Derived& Eigen::SparseSolverBase< Derived >::derived
inline

Definition at line 84 of file SparseSolverBase.h.

84 { return *static_cast<const Derived*>(this); }

◆ derived() [4/4]

template<typename Derived >
const Derived& Eigen::SimplicialCholeskyBase< Derived >::derived ( ) const
inline

Definition at line 107 of file SimplicialCholesky.h.

107 { return *static_cast<const Derived*>(this); }

◆ factorize()

template<typename Derived >
template<bool DoLDLT>
void Eigen::SimplicialCholeskyBase< Derived >::factorize ( const MatrixType a)
inlineprotected

Definition at line 216 of file SimplicialCholesky.h.

217  {
218  eigen_assert(a.rows()==a.cols());
219  Index size = a.cols();
220  CholMatrixType tmp(size,size);
221  ConstCholMatrixPtr pmat;
222 
223  if(m_P.size() == 0 && (int(UpLo) & int(Upper)) == Upper)
224  {
225  // If there is no ordering, try to directly use the input matrix without any copy
226  internal::simplicial_cholesky_grab_input<CholMatrixType,MatrixType>::run(a, pmat, tmp);
227  }
228  else
229  {
230  tmp.template selfadjointView<Upper>() = a.template selfadjointView<UpLo>().twistedBy(m_P);
231  pmat = &tmp;
232  }
233 
234  factorize_preordered<DoLDLT>(*pmat);
235  }
PermutationMatrix< Dynamic, Dynamic, StorageIndex > m_P
@ Upper
Definition: Constants.h:213

◆ factorize_preordered()

template<typename Derived >
template<bool DoLDLT>
void Eigen::SimplicialCholeskyBase< Derived >::factorize_preordered ( const CholMatrixType a)
protected

Definition at line 78 of file SimplicialCholesky_impl.h.

79 {
80  using std::sqrt;
81 
82  eigen_assert(m_analysisIsOk && "You must first call analyzePattern()");
83  eigen_assert(ap.rows()==ap.cols());
84  eigen_assert(m_parent.size()==ap.rows());
85  eigen_assert(m_nonZerosPerCol.size()==ap.rows());
86 
87  const StorageIndex size = StorageIndex(ap.rows());
88  const StorageIndex* Lp = m_matrix.outerIndexPtr();
90  Scalar* Lx = m_matrix.valuePtr();
91 
95 
96  bool ok = true;
97  m_diag.resize(DoLDLT ? size : 0);
98 
99  for(StorageIndex k = 0; k < size; ++k)
100  {
101  // compute nonzero pattern of kth row of L, in topological order
102  y[k] = Scalar(0); // Y(0:k) is now all zero
103  StorageIndex top = size; // stack for pattern is empty
104  tags[k] = k; // mark node k as visited
105  m_nonZerosPerCol[k] = 0; // count of nonzeros in column k of L
106  for(typename CholMatrixType::InnerIterator it(ap,k); it; ++it)
107  {
108  StorageIndex i = it.index();
109  if(i <= k)
110  {
111  y[i] += numext::conj(it.value()); /* scatter A(i,k) into Y (sum duplicates) */
112  Index len;
113  for(len = 0; tags[i] != k; i = m_parent[i])
114  {
115  pattern[len++] = i; /* L(k,i) is nonzero */
116  tags[i] = k; /* mark i as visited */
117  }
118  while(len > 0)
119  pattern[--top] = pattern[--len];
120  }
121  }
122 
123  /* compute numerical values kth row of L (a sparse triangular solve) */
124 
125  RealScalar d = numext::real(y[k]) * m_shiftScale + m_shiftOffset; // get D(k,k), apply the shift function, and clear Y(k)
126  y[k] = Scalar(0);
127  for(; top < size; ++top)
128  {
129  Index i = pattern[top]; /* pattern[top:n-1] is pattern of L(:,k) */
130  Scalar yi = y[i]; /* get and clear Y(i) */
131  y[i] = Scalar(0);
132 
133  /* the nonzero entry L(k,i) */
134  Scalar l_ki;
135  if(DoLDLT)
136  l_ki = yi / numext::real(m_diag[i]);
137  else
138  yi = l_ki = yi / Lx[Lp[i]];
139 
140  Index p2 = Lp[i] + m_nonZerosPerCol[i];
141  Index p;
142  for(p = Lp[i] + (DoLDLT ? 0 : 1); p < p2; ++p)
143  y[Li[p]] -= numext::conj(Lx[p]) * yi;
144  d -= numext::real(l_ki * numext::conj(yi));
145  Li[p] = k; /* store L(k,i) in column form of L */
146  Lx[p] = l_ki;
147  ++m_nonZerosPerCol[i]; /* increment count of nonzeros in col i */
148  }
149  if(DoLDLT)
150  {
151  m_diag[k] = d;
152  if(d == RealScalar(0))
153  {
154  ok = false; /* failure, D(k,k) is zero */
155  break;
156  }
157  }
158  else
159  {
160  Index p = Lp[k] + m_nonZerosPerCol[k]++;
161  Li[p] = k ; /* store L(k,k) = sqrt (d) in column k */
162  if(d <= RealScalar(0)) {
163  ok = false; /* failure, matrix is not positive definite */
164  break;
165  }
166  Lx[p] = sqrt(d) ;
167  }
168  }
169 
170  m_info = ok ? Success : NumericalIssue;
171  m_factorizationIsOk = true;
172 }
const SqrtReturnType sqrt() const
RealReturnType real() const
float * p
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
MatrixType::RealScalar RealScalar
const Scalar * valuePtr() const
Definition: SparseMatrix.h:177
const StorageIndex * innerIndexPtr() const
Definition: SparseMatrix.h:186
@ NumericalIssue
Definition: Constants.h:448
const Scalar & y
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< typename Derived::Scalar >, const Derived > conj(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)

◆ info()

template<typename Derived >
ComputationInfo Eigen::SimplicialCholeskyBase< Derived >::info ( ) const
inline

Reports whether previous computation was successful.

Returns
Success if computation was successful, NumericalIssue if the matrix.appears to be negative.

Definition at line 117 of file SimplicialCholesky.h.

118  {
119  eigen_assert(m_isInitialized && "Decomposition is not initialized.");
120  return m_info;
121  }

◆ ordering()

template<typename Derived >
void Eigen::SimplicialCholeskyBase< Derived >::ordering ( const MatrixType a,
ConstCholMatrixPtr pmat,
CholMatrixType ap 
)
protected

Definition at line 660 of file SimplicialCholesky.h.

661 {
662  eigen_assert(a.rows()==a.cols());
663  const Index size = a.rows();
664  pmat = &ap;
665  // Note that ordering methods compute the inverse permutation
666  if(!internal::is_same<OrderingType,NaturalOrdering<Index> >::value)
667  {
668  {
669  CholMatrixType C;
670  C = a.template selfadjointView<UpLo>();
671 
673  ordering(C,m_Pinv);
674  }
675 
676  if(m_Pinv.size()>0) m_P = m_Pinv.inverse();
677  else m_P.resize(0);
678 
679  ap.resize(size,size);
680  ap.template selfadjointView<Upper>() = a.template selfadjointView<UpLo>().twistedBy(m_P);
681  }
682  else
683  {
684  m_Pinv.resize(0);
685  m_P.resize(0);
686  if(int(UpLo)==int(Lower) || MatrixType::IsRowMajor)
687  {
688  // we have to transpose the lower part to to the upper one
689  ap.resize(size,size);
690  ap.template selfadjointView<Upper>() = a.template selfadjointView<UpLo>();
691  }
692  else
693  internal::simplicial_cholesky_grab_input<CholMatrixType,MatrixType>::run(a, pmat, ap);
694  }
695 }
void resize(Index newSize)
InverseReturnType inverse() const
PermutationMatrix< Dynamic, Dynamic, StorageIndex > m_Pinv
internal::traits< Derived >::OrderingType OrderingType
@ Lower
Definition: Constants.h:211

◆ permutationP()

template<typename Derived >
const PermutationMatrix<Dynamic,Dynamic,StorageIndex>& Eigen::SimplicialCholeskyBase< Derived >::permutationP ( ) const
inline
Returns
the permutation P
See also
permutationPinv()

Definition at line 125 of file SimplicialCholesky.h.

126  { return m_P; }

◆ permutationPinv()

template<typename Derived >
const PermutationMatrix<Dynamic,Dynamic,StorageIndex>& Eigen::SimplicialCholeskyBase< Derived >::permutationPinv ( ) const
inline
Returns
the inverse P^-1 of the permutation P
See also
permutationP()

Definition at line 130 of file SimplicialCholesky.h.

131  { return m_Pinv; }

◆ rows()

template<typename Derived >
Index Eigen::SimplicialCholeskyBase< Derived >::rows ( void  ) const
inline

Definition at line 110 of file SimplicialCholesky.h.

110 { return m_matrix.rows(); }
Index rows() const
Definition: SparseMatrix.h:165

◆ setShift()

template<typename Derived >
Derived& Eigen::SimplicialCholeskyBase< Derived >::setShift ( const RealScalar offset,
const RealScalar scale = 1 
)
inline

Sets the shift parameters that will be used to adjust the diagonal coefficients during the numerical factorization.

During the numerical factorization, the diagonal coefficients are transformed by the following linear model:
d_ii = offset + scale * d_ii

The default is the identity transformation with offset=0, and scale=1.

Returns
a reference to *this.

Definition at line 142 of file SimplicialCholesky.h.

143  {
144  m_shiftOffset = offset;
145  m_shiftScale = scale;
146  return derived();
147  }

Member Data Documentation

◆ m_analysisIsOk

template<typename Derived >
bool Eigen::SimplicialCholeskyBase< Derived >::m_analysisIsOk
protected

Definition at line 263 of file SimplicialCholesky.h.

◆ m_diag

template<typename Derived >
VectorType Eigen::SimplicialCholeskyBase< Derived >::m_diag
protected

Definition at line 266 of file SimplicialCholesky.h.

◆ m_factorizationIsOk

template<typename Derived >
bool Eigen::SimplicialCholeskyBase< Derived >::m_factorizationIsOk
protected

Definition at line 262 of file SimplicialCholesky.h.

◆ m_info

template<typename Derived >
ComputationInfo Eigen::SimplicialCholeskyBase< Derived >::m_info
mutableprotected

Definition at line 261 of file SimplicialCholesky.h.

◆ m_isInitialized

template<typename Derived >
bool Eigen::SparseSolverBase< Derived >::m_isInitialized
mutableprivate

Definition at line 123 of file SparseSolverBase.h.

◆ m_matrix

template<typename Derived >
CholMatrixType Eigen::SimplicialCholeskyBase< Derived >::m_matrix
protected

Definition at line 265 of file SimplicialCholesky.h.

◆ m_nonZerosPerCol

template<typename Derived >
VectorI Eigen::SimplicialCholeskyBase< Derived >::m_nonZerosPerCol
protected

Definition at line 268 of file SimplicialCholesky.h.

◆ m_P

template<typename Derived >
PermutationMatrix<Dynamic,Dynamic,StorageIndex> Eigen::SimplicialCholeskyBase< Derived >::m_P
protected

Definition at line 269 of file SimplicialCholesky.h.

◆ m_parent

template<typename Derived >
VectorI Eigen::SimplicialCholeskyBase< Derived >::m_parent
protected

Definition at line 267 of file SimplicialCholesky.h.

◆ m_Pinv

template<typename Derived >
PermutationMatrix<Dynamic,Dynamic,StorageIndex> Eigen::SimplicialCholeskyBase< Derived >::m_Pinv
protected

Definition at line 270 of file SimplicialCholesky.h.

◆ m_shiftOffset

template<typename Derived >
RealScalar Eigen::SimplicialCholeskyBase< Derived >::m_shiftOffset
protected

Definition at line 272 of file SimplicialCholesky.h.

◆ m_shiftScale

template<typename Derived >
RealScalar Eigen::SimplicialCholeskyBase< Derived >::m_shiftScale
protected

Definition at line 273 of file SimplicialCholesky.h.


The documentation for this class was generated from the following files: