lsst.jointcal  16.0-33-g7c26e80+4
Eigenstuff.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * This file is part of jointcal.
4  *
5  * Developed for the LSST Data Management System.
6  * This product includes software developed by the LSST Project
7  * (https://www.lsst.org).
8  * See the COPYRIGHT file at the top-level directory of this distribution
9  * for details of code ownership.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
25 #ifndef LSST_JOINTCAL_EIGENSTUFF_H
26 #define LSST_JOINTCAL_EIGENSTUFF_H
27 
28 #include "lsst/pex/exceptions.h"
29 
30 #include "Eigen/CholmodSupport" // to switch to cholmod
31 #include "Eigen/Core"
32 
33 typedef Eigen::Matrix<double, Eigen::Dynamic, 2> MatrixX2d;
34 
35 typedef Eigen::SparseMatrix<double> SparseMatrixD;
36 
37 /* Cholesky factorization class using cholmod, with the small-rank update capability.
38  *
39  * Class derived from Eigen's CholmodBase, to add the factorization
40  * update capability to the interface. Besides this addition, it
41  * behaves the same way as Eigen's native Cholesky factorization
42  * classes. It relies on the simplicial LDLt factorization.
43  *
44  * @Seealso Eigen::CholmodSimplicialLDLT
45  */
46 template <typename _MatrixType, int _UpLo = Eigen::Lower>
48  : public Eigen::CholmodBase<_MatrixType, _UpLo, CholmodSimplicialLDLT2<_MatrixType, _UpLo>> {
49  typedef Eigen::CholmodBase<_MatrixType, _UpLo, CholmodSimplicialLDLT2> Base;
50  using Base::m_cholmod;
51 
52 public:
53  typedef _MatrixType MatrixType;
54  typedef typename MatrixType::Index Index;
55  typedef typename MatrixType::RealScalar RealScalar;
56 
57  CholmodSimplicialLDLT2() : Base() { init(); }
58 
59  CholmodSimplicialLDLT2(MatrixType const &matrix) : Base() {
60  init();
61  this->compute(matrix);
62  }
63 
64  // this routine is the one we added
65  void update(SparseMatrixD const &H, bool UpOrDown) {
66  // check size
67  Index const size = Base::m_cholmodFactor->n;
68  EIGEN_UNUSED_VARIABLE(size);
69  eigen_assert(size == H.rows());
70 
71  cholmod_sparse C_cs = viewAsCholmod(H);
72  /* We have to apply the magic permutation to the update matrix,
73  read page 117 of Cholmod UserGuide.pdf */
74  cholmod_sparse *C_cs_perm =
75  cholmod_submatrix(&C_cs, (int *)Base::m_cholmodFactor->Perm, Base::m_cholmodFactor->n,
76  nullptr, -1, true, true, &this->cholmod());
77  assert(C_cs_perm);
78  int isOk = cholmod_updown(UpOrDown, C_cs_perm, Base::m_cholmodFactor, &this->cholmod());
79  cholmod_free_sparse(&C_cs_perm, &this->cholmod());
80  if (!isOk) {
81  throw(LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "cholmod_update failed!"));
82  }
83  }
84 
85 protected:
86  void init() {
87  m_cholmod.final_asis = 1;
88  m_cholmod.supernodal = CHOLMOD_SIMPLICIAL;
89  // In CholmodBase::CholmodBase(), the following statement is missing in
90  // SuiteSparse 3.2.0.8. Fixed in 3.2.7
91  Base::m_shiftOffset[0] = Base::m_shiftOffset[1] = RealScalar(0.0);
92  }
93 };
94 
95 #endif // LSST_JOINTCAL_EIGENSTUFF_H
Eigen::SparseMatrix< double > SparseMatrixD
Definition: Eigenstuff.h:35
_MatrixType MatrixType
Definition: Eigenstuff.h:53
CholmodSimplicialLDLT2(MatrixType const &matrix)
Definition: Eigenstuff.h:59
void update(SparseMatrixD const &H, bool UpOrDown)
Definition: Eigenstuff.h:65
MatrixType::RealScalar RealScalar
Definition: Eigenstuff.h:55
MatrixType::Index Index
Definition: Eigenstuff.h:54
Eigen::Matrix< double, Eigen::Dynamic, 2 > MatrixX2d
Definition: Eigenstuff.h:33
#define LSST_EXCEPT(type,...)