BasicPreconditioners.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 CNRS
3  * Copyright 2024 Inria
4  */
5 
6 #ifndef __eigenpy_basic_preconditioners_hpp__
7 #define __eigenpy_basic_preconditioners_hpp__
8 
9 #include <Eigen/IterativeLinearSolvers>
10 
11 #include "eigenpy/fwd.hpp"
12 
13 namespace eigenpy {
14 
15 template <typename Preconditioner>
17  : public bp::def_visitor<PreconditionerBaseVisitor<Preconditioner> > {
18  typedef Eigen::MatrixXd MatrixType;
19  typedef Eigen::VectorXd VectorType;
20 
21  template <class PyClass>
22  void visit(PyClass& cl) const {
23  cl.def(bp::init<>("Default constructor"))
24  .def(bp::init<MatrixType>(bp::args("self", "A"),
25  "Initialize the preconditioner with matrix A "
26  "for further Az=b solving."))
27 #if EIGEN_VERSION_AT_LEAST(3, 3, 0)
28  .def("info", &Preconditioner::info,
29  "Returns success if the Preconditioner has been well initialized.")
30 #endif
31  .def("solve", &solve, bp::arg("b"),
32  "Returns the solution A * z = b where the preconditioner is an "
33  "estimate of A^-1.")
34 
35  .def("compute", &Preconditioner::template compute<MatrixType>,
36  bp::arg("mat"),
37  "Initialize the preconditioner from the matrix value.",
38  bp::return_value_policy<bp::reference_existing_object>())
39  .def("factorize", &Preconditioner::template factorize<MatrixType>,
40  bp::arg("mat"),
41  "Initialize the preconditioner from the matrix value, i.e "
42  "factorize the mat given as input to approximate its inverse.",
43  bp::return_value_policy<bp::reference_existing_object>());
44  }
45 
46  private:
47  static VectorType solve(Preconditioner& self, const VectorType& b) {
48  return self.solve(b);
49  }
50 };
51 
52 template <typename Scalar>
54  : PreconditionerBaseVisitor<Eigen::DiagonalPreconditioner<Scalar> > {
55  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixType;
56  typedef Eigen::DiagonalPreconditioner<Scalar> Preconditioner;
57 
58  template <class PyClass>
59  void visit(PyClass& cl) const {
61  .def("rows", &Preconditioner::rows,
62  "Returns the number of rows in the preconditioner.")
63  .def("cols", &Preconditioner::cols,
64  "Returns the number of cols in the preconditioner.");
65  }
66 
67  static void expose() {
68  bp::class_<Preconditioner>(
69  "DiagonalPreconditioner",
70  "A preconditioner based on the digonal entrie.\n"
71  "This class allows to approximately solve for A.x = b problems "
72  "assuming A is a diagonal matrix.",
73  bp::no_init)
75  }
76 };
77 
78 #if EIGEN_VERSION_AT_LEAST(3, 3, 5)
79 template <typename Scalar>
80 struct LeastSquareDiagonalPreconditionerVisitor
81  : PreconditionerBaseVisitor<
82  Eigen::LeastSquareDiagonalPreconditioner<Scalar> > {
83  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixType;
84  typedef Eigen::LeastSquareDiagonalPreconditioner<Scalar> Preconditioner;
85 
86  template <class PyClass>
87  void visit(PyClass&) const {}
88 
89  static void expose() {
90  bp::class_<Preconditioner>(
91  "LeastSquareDiagonalPreconditioner",
92  "Jacobi preconditioner for LeastSquaresConjugateGradient.\n"
93  "his class allows to approximately solve for A' A x = A' b problems "
94  "assuming A' A is a diagonal matrix.",
95  bp::no_init)
96  .def(DiagonalPreconditionerVisitor<Scalar>())
97  .def(IdVisitor<Preconditioner>());
98  }
99 };
100 #endif
101 
103  : PreconditionerBaseVisitor<Eigen::IdentityPreconditioner> {
104  typedef Eigen::IdentityPreconditioner Preconditioner;
105 
106  template <class PyClass>
107  void visit(PyClass&) const {}
108 
109  static void expose() {
110  bp::class_<Preconditioner>("IdentityPreconditioner", bp::no_init)
113  }
114 };
115 
116 } // namespace eigenpy
117 
118 #endif // ifndef __eigenpy_basic_preconditioners_hpp__
eigenpy::DiagonalPreconditionerVisitor
Definition: BasicPreconditioners.hpp:53
fwd.hpp
test_complex.rows
int rows
Definition: test_complex.py:4
eigenpy::IdentityPreconditionerVisitor::Preconditioner
Eigen::IdentityPreconditioner Preconditioner
Definition: BasicPreconditioners.hpp:104
eigenpy::DiagonalPreconditionerVisitor::Preconditioner
Eigen::DiagonalPreconditioner< Scalar > Preconditioner
Definition: BasicPreconditioners.hpp:56
eigenpy
Definition: alignment.hpp:14
eigenpy::IdentityPreconditionerVisitor
Definition: BasicPreconditioners.hpp:102
eigenpy::PreconditionerBaseVisitor::MatrixType
Eigen::MatrixXd MatrixType
Definition: BasicPreconditioners.hpp:18
eigenpy::PreconditionerBaseVisitor::visit
void visit(PyClass &cl) const
Definition: BasicPreconditioners.hpp:22
eigenpy::IdVisitor
Add the Python method id to retrieving a unique id for a given object exposed with Boost....
Definition: id.hpp:18
eigenpy::expose
void expose()
Call the expose function of a given type T.
Definition: expose.hpp:23
eigenpy::IdentityPreconditionerVisitor::expose
static void expose()
Definition: BasicPreconditioners.hpp:109
eigenpy::DiagonalPreconditionerVisitor::expose
static void expose()
Definition: BasicPreconditioners.hpp:67
eigenpy::PreconditionerBaseVisitor::VectorType
Eigen::VectorXd VectorType
Definition: BasicPreconditioners.hpp:19
eigenpy::DiagonalPreconditionerVisitor::MatrixType
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > MatrixType
Definition: BasicPreconditioners.hpp:55
eigenpy::DiagonalPreconditionerVisitor::visit
void visit(PyClass &cl) const
Definition: BasicPreconditioners.hpp:59
eigenpy::PreconditionerBaseVisitor
Definition: BasicPreconditioners.hpp:16
eigenpy::IdentityPreconditionerVisitor::visit
void visit(PyClass &) const
Definition: BasicPreconditioners.hpp:107
test_complex.cols
int cols
Definition: test_complex.py:5
eigenpy::PreconditionerBaseVisitor::solve
static VectorType solve(Preconditioner &self, const VectorType &b)
Definition: BasicPreconditioners.hpp:47


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Nov 2 2024 02:14:45