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 "eigenpy/fwd.hpp"
21 #include <Eigen/IterativeLinearSolvers>
22 
23 namespace eigenpy
24 {
25 
26  namespace bp = boost::python;
27 
28  template<typename Preconditioner>
30  : public bp::def_visitor< PreconditionerBaseVisitor<Preconditioner> >
31  {
32  typedef Eigen::MatrixXd MatrixType;
33  typedef Eigen::VectorXd VectorType;
34 
35  template<class PyClass>
36  void visit(PyClass& cl) const
37  {
38  cl
39  .def(bp::init<>("Default constructor"))
40  .def(bp::init<MatrixType>(bp::arg("A"),"Initialize the preconditioner with matrix A for further Az=b solving."))
41 #if EIGEN_VERSION_AT_LEAST(3,3,0)
42  .def("info",&Preconditioner::info,
43  "Returns success if the Preconditioner has been well initialized.")
44 #endif
45  .def("solve",&solve,bp::arg("b"),
46  "Returns the solution A * z = b where the preconditioner is an estimate of A^-1.")
47 
48  .def("compute",&Preconditioner::template compute<MatrixType>,bp::arg("mat"),
49  "Initialize the preconditioner from the matrix value.",
50  bp::return_value_policy<bp::reference_existing_object>())
51  .def("factorize",&Preconditioner::template factorize<MatrixType>,bp::arg("mat"),
52  "Initialize the preconditioner from the matrix value, i.e factorize the mat given as input to approximate its inverse.",
53  bp::return_value_policy<bp::reference_existing_object>())
54  ;
55 
56  }
57 
58  private:
59 
60  static VectorType solve(Preconditioner & self, const VectorType & b)
61  {
62  return self.solve(b);
63  }
64 
65  };
66 
67  template<typename Scalar>
68  struct DiagonalPreconditionerVisitor : PreconditionerBaseVisitor<Eigen::DiagonalPreconditioner<Scalar> >
69  {
70  typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> MatrixType;
71  typedef Eigen::DiagonalPreconditioner<Scalar> Preconditioner;
72 
73  template<class PyClass>
74  void visit(PyClass& cl) const
75  {
76  cl
78  .def("rows",&Preconditioner::rows,"Returns the number of rows in the preconditioner.")
79  .def("cols",&Preconditioner::cols,"Returns the number of cols in the preconditioner.")
80  ;
81 
82  }
83 
84  static void expose()
85  {
86  bp::class_<Preconditioner>("DiagonalPreconditioner",
87  "A preconditioner based on the digonal entrie.\n"
88  "This class allows to approximately solve for A.x = b problems assuming A is a diagonal matrix.",
89  bp::no_init)
90  ;
91 
92  }
93  };
94 
95 #if EIGEN_VERSION_AT_LEAST(3,3,5)
96  template<typename Scalar>
97  struct LeastSquareDiagonalPreconditionerVisitor : PreconditionerBaseVisitor<Eigen::LeastSquareDiagonalPreconditioner<Scalar> >
98  {
99  typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> MatrixType;
100  typedef Eigen::LeastSquareDiagonalPreconditioner<Scalar> Preconditioner;
101 
102  template<class PyClass>
103  void visit(PyClass&) const
104  {
105  }
106 
107  static void expose()
108  {
109  bp::class_<Preconditioner>("LeastSquareDiagonalPreconditioner",
110  "Jacobi preconditioner for LeastSquaresConjugateGradient.\n"
111  "his class allows to approximately solve for A' A x = A' b problems assuming A' A is a diagonal matrix.",
112  bp::no_init)
114  ;
115 
116  }
117  };
118 #endif
119 
120  struct IdentityPreconditionerVisitor : PreconditionerBaseVisitor<Eigen::IdentityPreconditioner >
121  {
122  typedef Eigen::IdentityPreconditioner Preconditioner;
123 
124  template<class PyClass>
125  void visit(PyClass&) const
126  {
127  }
128 
129  static void expose()
130  {
131  bp::class_<Preconditioner>("IdentityPreconditioner",
132  bp::no_init)
134  ;
135 
136  }
137  };
138 
139 
140 } // namespace eigenpy
141 
142 #endif // ifndef __eigenpy_basic_preconditioners_hpp__
Eigen::IdentityPreconditioner Preconditioner
Eigen::DiagonalPreconditioner< Scalar > Preconditioner
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > MatrixType
void expose()
Call the expose function of a given type T.
Definition: expose.hpp:25
static VectorType solve(Preconditioner &self, const VectorType &b)


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