BasicPreconditioners.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017, Justin Carpentier, LAAS-CNRS
3  *
4  * This file is part of eigenpy.
5  * eigenpy is free software: you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation, either version 3 of
8  * the License, or (at your option) any later version.
9  * eigenpy is distributed in the hope that it will be
10  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
11  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details. You should
13  * have received a copy of the GNU Lesser General Public License along
14  * with eigenpy. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef __eigenpy_basic_preconditioners_hpp__
18 #define __eigenpy_basic_preconditioners_hpp__
19 
20 #include <Eigen/IterativeLinearSolvers>
21 
22 #include "eigenpy/fwd.hpp"
23 
24 namespace eigenpy {
25 
26 template <typename Preconditioner>
28  : public bp::def_visitor<PreconditionerBaseVisitor<Preconditioner> > {
29  typedef Eigen::MatrixXd MatrixType;
30  typedef Eigen::VectorXd VectorType;
31 
32  template <class PyClass>
33  void visit(PyClass& cl) const {
34  cl.def(bp::init<>("Default constructor"))
35  .def(bp::init<MatrixType>(bp::args("self", "A"),
36  "Initialize the preconditioner with matrix A "
37  "for further Az=b solving."))
38 #if EIGEN_VERSION_AT_LEAST(3, 3, 0)
39  .def("info", &Preconditioner::info,
40  "Returns success if the Preconditioner has been well initialized.")
41 #endif
42  .def("solve", &solve, bp::arg("b"),
43  "Returns the solution A * z = b where the preconditioner is an "
44  "estimate of A^-1.")
45 
46  .def("compute", &Preconditioner::template compute<MatrixType>,
47  bp::arg("mat"),
48  "Initialize the preconditioner from the matrix value.",
49  bp::return_value_policy<bp::reference_existing_object>())
50  .def("factorize", &Preconditioner::template factorize<MatrixType>,
51  bp::arg("mat"),
52  "Initialize the preconditioner from the matrix value, i.e "
53  "factorize the mat given as input to approximate its inverse.",
54  bp::return_value_policy<bp::reference_existing_object>());
55  }
56 
57  private:
58  static VectorType solve(Preconditioner& self, const VectorType& b) {
59  return self.solve(b);
60  }
61 };
62 
63 template <typename Scalar>
65  : PreconditionerBaseVisitor<Eigen::DiagonalPreconditioner<Scalar> > {
66  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixType;
67  typedef Eigen::DiagonalPreconditioner<Scalar> Preconditioner;
68 
69  template <class PyClass>
70  void visit(PyClass& cl) const {
72  .def("rows", &Preconditioner::rows,
73  "Returns the number of rows in the preconditioner.")
74  .def("cols", &Preconditioner::cols,
75  "Returns the number of cols in the preconditioner.");
76  }
77 
78  static void expose() {
79  bp::class_<Preconditioner>(
80  "DiagonalPreconditioner",
81  "A preconditioner based on the digonal entrie.\n"
82  "This class allows to approximately solve for A.x = b problems "
83  "assuming A is a diagonal matrix.",
84  bp::no_init);
85  }
86 };
87 
88 #if EIGEN_VERSION_AT_LEAST(3, 3, 5)
89 template <typename Scalar>
90 struct LeastSquareDiagonalPreconditionerVisitor
92  Eigen::LeastSquareDiagonalPreconditioner<Scalar> > {
93  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixType;
94  typedef Eigen::LeastSquareDiagonalPreconditioner<Scalar> Preconditioner;
95 
96  template <class PyClass>
97  void visit(PyClass&) const {}
98 
99  static void expose() {
100  bp::class_<Preconditioner>(
101  "LeastSquareDiagonalPreconditioner",
102  "Jacobi preconditioner for LeastSquaresConjugateGradient.\n"
103  "his class allows to approximately solve for A' A x = A' b problems "
104  "assuming A' A is a diagonal matrix.",
105  bp::no_init)
107  }
108 };
109 #endif
110 
112  : PreconditionerBaseVisitor<Eigen::IdentityPreconditioner> {
113  typedef Eigen::IdentityPreconditioner Preconditioner;
114 
115  template <class PyClass>
116  void visit(PyClass&) const {}
117 
118  static void expose() {
119  bp::class_<Preconditioner>("IdentityPreconditioner", bp::no_init)
121  }
122 };
123 
124 } // namespace eigenpy
125 
126 #endif // ifndef __eigenpy_basic_preconditioners_hpp__
Eigen::IdentityPreconditioner Preconditioner
Eigen::DiagonalPreconditioner< Scalar > Preconditioner
void expose()
Call the expose function of a given type T.
Definition: expose.hpp:23
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > MatrixType
static VectorType solve(Preconditioner &self, const VectorType &b)


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Fri Jun 2 2023 02:10:26