lsst.jointcal  15.0-17-g076ea75+1
Eigenstuff.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 #ifndef LSST_JOINTCAL_EIGENSTUFF_H
3 #define LSST_JOINTCAL_EIGENSTUFF_H
4 
5 #include "lsst/pex/exceptions.h"
6 
7 #include "Eigen/CholmodSupport" // to switch to cholmod
8 #include "Eigen/Core"
9 
10 typedef Eigen::Matrix<double, Eigen::Dynamic, 2> MatrixX2d;
11 
12 typedef Eigen::SparseMatrix<double> SparseMatrixD;
13 
14 /* Cholesky factorization class using cholmod, with the small-rank update capability.
15  *
16  * Class derived from Eigen's CholmodBase, to add the factorization
17  * update capability to the interface. Besides this addition, it
18  * behaves the same way as Eigen's native Cholesky factorization
19  * classes. It relies on the simplicial LDLt factorization.
20  *
21  * @Seealso Eigen::CholmodSimplicialLDLT
22  */
23 template <typename _MatrixType, int _UpLo = Eigen::Lower>
25  : public Eigen::CholmodBase<_MatrixType, _UpLo, CholmodSimplicialLDLT2<_MatrixType, _UpLo>> {
26  typedef Eigen::CholmodBase<_MatrixType, _UpLo, CholmodSimplicialLDLT2> Base;
27  using Base::m_cholmod;
28 
29 public:
30  typedef _MatrixType MatrixType;
31  typedef typename MatrixType::Index Index;
32  typedef typename MatrixType::RealScalar RealScalar;
33 
34  CholmodSimplicialLDLT2() : Base() { init(); }
35 
36  CholmodSimplicialLDLT2(MatrixType const &matrix) : Base() {
37  init();
38  this->compute(matrix);
39  }
40 
41  // this routine is the one we added
42  void update(SparseMatrixD const &H, bool UpOrDown) {
43  // check size
44  Index const size = Base::m_cholmodFactor->n;
45  EIGEN_UNUSED_VARIABLE(size);
46  eigen_assert(size == H.rows());
47 
48  cholmod_sparse C_cs = viewAsCholmod(H);
49  /* We have to apply the magic permutation to the update matrix,
50  read page 117 of Cholmod UserGuide.pdf */
51  cholmod_sparse *C_cs_perm =
52  cholmod_submatrix(&C_cs, (int *)Base::m_cholmodFactor->Perm, Base::m_cholmodFactor->n,
53  nullptr, -1, true, true, &this->cholmod());
54  assert(C_cs_perm);
55  int isOk = cholmod_updown(UpOrDown, C_cs_perm, Base::m_cholmodFactor, &this->cholmod());
56  cholmod_free_sparse(&C_cs_perm, &this->cholmod());
57  if (!isOk) {
58  throw(LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "cholmod_update failed!"));
59  }
60  }
61 
62 protected:
63  void init() {
64  m_cholmod.final_asis = 1;
65  m_cholmod.supernodal = CHOLMOD_SIMPLICIAL;
66  // In CholmodBase::CholmodBase(), the following statement is missing in
67  // SuiteSparse 3.2.0.8. Fixed in 3.2.7
68  Base::m_shiftOffset[0] = Base::m_shiftOffset[1] = RealScalar(0.0);
69  }
70 };
71 
72 #endif // LSST_JOINTCAL_EIGENSTUFF_H
Eigen::SparseMatrix< double > SparseMatrixD
Definition: Eigenstuff.h:12
_MatrixType MatrixType
Definition: Eigenstuff.h:30
CholmodSimplicialLDLT2(MatrixType const &matrix)
Definition: Eigenstuff.h:36
void update(SparseMatrixD const &H, bool UpOrDown)
Definition: Eigenstuff.h:42
MatrixType::RealScalar RealScalar
Definition: Eigenstuff.h:32
MatrixType::Index Index
Definition: Eigenstuff.h:31
Eigen::Matrix< double, Eigen::Dynamic, 2 > MatrixX2d
Definition: Eigenstuff.h:10
#define LSST_EXCEPT(type,...)