LDLT.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 INRIA
3  */
4 
5 #ifndef __eigenpy_decomposition_ldlt_hpp__
6 #define __eigenpy_decomposition_ldlt_hpp__
7 
8 #include "eigenpy/eigenpy.hpp"
9 
10 #include <Eigen/Core>
11 #include <Eigen/Cholesky>
12 
14 
15 namespace eigenpy
16 {
17 
18  template<typename _MatrixType>
20  : public boost::python::def_visitor< LDLTSolverVisitor<_MatrixType> >
21  {
22 
23  typedef _MatrixType MatrixType;
24  typedef typename MatrixType::Scalar Scalar;
25  typedef typename MatrixType::RealScalar RealScalar;
26  typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1,MatrixType::Options> VectorType;
27  typedef Eigen::LDLT<MatrixType> Solver;
28 
29  template<class PyClass>
30  void visit(PyClass& cl) const
31  {
32  namespace bp = boost::python;
33  cl
34  .def(bp::init<>("Default constructor"))
35  .def(bp::init<Eigen::DenseIndex>(bp::arg("size"),
36  "Default constructor with memory preallocation"))
37  .def(bp::init<MatrixType>(bp::arg("matrix"),
38  "Constructs a LDLT factorization from a given matrix."))
39 
40  .def("isNegative",&Solver::isNegative,bp::arg("self"),
41  "Returns true if the matrix is negative (semidefinite).")
42  .def("isPositive",&Solver::isPositive,bp::arg("self"),
43  "Returns true if the matrix is positive (semidefinite).")
44 
45  .def("matrixL",&matrixL,bp::arg("self"),
46  "Returns the lower triangular matrix L.")
47  .def("matrixU",&matrixU,bp::arg("self"),
48  "Returns the upper triangular matrix U.")
49  .def("vectorD",&vectorD,bp::arg("self"),
50  "Returns the coefficients of the diagonal matrix D.")
51  .def("transpositionsP",&transpositionsP,bp::arg("self"),
52  "Returns the permutation matrix P.")
53 
54  .def("matrixLDLT",&Solver::matrixLDLT,bp::arg("self"),
55  "Returns the LDLT decomposition matrix.",
56  bp::return_internal_reference<>())
57 
58  .def("rankUpdate",(Solver & (Solver::*)(const Eigen::MatrixBase<VectorType> &, const RealScalar &))&Solver::template rankUpdate<VectorType>,
59  bp::args("self","vector","sigma"),
60  bp::return_self<>())
61 
62 #if EIGEN_VERSION_AT_LEAST(3,3,0)
63  .def("adjoint",&Solver::adjoint,bp::arg("self"),
64  "Returns the adjoint, that is, a reference to the decomposition itself as if the underlying matrix is self-adjoint.",
65  bp::return_self<>())
66 #endif
67 
68  .def("compute",(Solver & (Solver::*)(const Eigen::EigenBase<MatrixType> & matrix))&Solver::compute,
69  bp::args("self","matrix"),
70  "Computes the LDLT of given matrix.",
71  bp::return_self<>())
72 
73  .def("info",&Solver::info,bp::arg("self"),
74  "NumericalIssue if the input contains INF or NaN values or overflow occured. Returns Success otherwise.")
75 #if EIGEN_VERSION_AT_LEAST(3,3,0)
76  .def("rcond",&Solver::rcond,bp::arg("self"),
77  "Returns an estimate of the reciprocal condition number of the matrix.")
78 #endif
79  .def("reconstructedMatrix",&Solver::reconstructedMatrix,bp::arg("self"),
80  "Returns the matrix represented by the decomposition, i.e., it returns the product: L L^*. This function is provided for debug purpose.")
81  .def("solve",&solve<VectorType>,bp::args("self","b"),
82  "Returns the solution x of A x = b using the current decomposition of A.")
83 
84  .def("setZero",&Solver::setZero,bp::arg("self"),
85  "Clear any existing decomposition.")
86  ;
87  }
88 
89  static void expose()
90  {
91  static const std::string classname = "LDLT" + scalar_name<Scalar>::shortname();
92  expose(classname);
93  }
94 
95  static void expose(const std::string & name)
96  {
97  namespace bp = boost::python;
98  bp::class_<Solver>(name.c_str(),
99  "Robust Cholesky decomposition of a matrix with pivoting.\n\n"
100  "Perform a robust Cholesky decomposition of a positive semidefinite or negative semidefinite matrix $ A $ such that $ A = P^TLDL^*P $, where P is a permutation matrix, L is lower triangular with a unit diagonal and D is a diagonal matrix.\n\n"
101  "The decomposition uses pivoting to ensure stability, so that L will have zeros in the bottom right rank(A) - n submatrix. Avoiding the square root on D also stabilizes the computation.",
102  bp::no_init)
103  .def(LDLTSolverVisitor());
104  }
105 
106  private:
107 
108  static MatrixType matrixL(const Solver & self) { return self.matrixL(); }
109  static MatrixType matrixU(const Solver & self) { return self.matrixU(); }
110  static VectorType vectorD(const Solver & self) { return self.vectorD(); }
111 
112  static MatrixType transpositionsP(const Solver & self)
113  {
114  return self.transpositionsP() * MatrixType::Identity(self.matrixL().rows(),
115  self.matrixL().rows());
116  }
117 
118  template<typename VectorType>
119  static VectorType solve(const Solver & self, const VectorType & vec)
120  {
121  return self.solve(vec);
122  }
123  };
124 
125 } // namespace eigenpy
126 
127 #endif // ifndef __eigenpy_decomposition_ldlt_hpp__
MatrixType::Scalar Scalar
Definition: LDLT.hpp:24
boost::python::object matrix()
Definition: bnpy.cpp:20
static MatrixType matrixL(const Solver &self)
Definition: LDLT.hpp:108
_MatrixType MatrixType
Definition: LDLT.hpp:23
Eigen::LDLT< MatrixType > Solver
Definition: LDLT.hpp:27
static std::string shortname()
static MatrixType matrixU(const Solver &self)
Definition: LDLT.hpp:109
void visit(PyClass &cl) const
Definition: LDLT.hpp:30
Eigen::Matrix< Scalar, Eigen::Dynamic, 1, MatrixType::Options > VectorType
Definition: LDLT.hpp:26
static VectorType vectorD(const Solver &self)
Definition: LDLT.hpp:110
static void expose()
Definition: LDLT.hpp:89
static VectorType solve(const Solver &self, const VectorType &vec)
Definition: LDLT.hpp:119
static MatrixType transpositionsP(const Solver &self)
Definition: LDLT.hpp:112
static void expose(const std::string &name)
Definition: LDLT.hpp:95
MatrixType::RealScalar RealScalar
Definition: LDLT.hpp:25


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Apr 17 2021 02:37:59