eigenpy 3.12.0
Bindings between Numpy and Eigen using Boost.Python
Loading...
Searching...
No Matches
BasicPreconditioners.hpp
1/*
2 * Copyright 2017 CNRS
3 * Copyright 2024-2025 INRIA
4 */
5
6#ifndef __eigenpy_solvers_basic_preconditioners_hpp__
7#define __eigenpy_solvers_basic_preconditioners_hpp__
8
9#include <Eigen/IterativeLinearSolvers>
10
11#include "eigenpy/fwd.hpp"
12
13namespace eigenpy {
14
15template <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
52template <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)
79template <typename Scalar>
80struct LeastSquareDiagonalPreconditionerVisitor
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_solvers_basic_preconditioners_hpp__
void expose()
Call the expose function of a given type T.
Definition expose.hpp:23
Add the Python method id to retrieving a unique id for a given object exposed with Boost....
Definition id.hpp:18