LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
LSSTDataManagementBasePackage
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,...)
Create an exception with a given type.
Definition: Exception.h:48
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104