minres.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021 INRIA
3  */
4 
5 #ifndef __eigenpy_decompositions_minres_hpp__
6 #define __eigenpy_decompositions_minres_hpp__
7 
8 #include <Eigen/Core>
9 #include <iostream>
10 #include <unsupported/Eigen/IterativeSolvers>
11 
12 #include "eigenpy/eigenpy.hpp"
14 
15 namespace eigenpy {
16 template <typename _Solver>
18  : public boost::python::def_visitor<IterativeSolverBaseVisitor<_Solver> > {
19  typedef _Solver Solver;
20  typedef typename Solver::MatrixType MatrixType;
21  typedef typename Solver::Preconditioner Preconditioner;
22  typedef typename Solver::Scalar Scalar;
23  typedef typename Solver::RealScalar RealScalar;
24 
25  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic,
26  MatrixType::Options>
28 
29  template <class PyClass>
30  void visit(PyClass& cl) const {
31  cl.def("analyzePattern",
32  (Solver & (Solver::*)(const Eigen::EigenBase<MatrixType>& matrix)) &
33  Solver::analyzePattern,
34  bp::args("self", "A"),
35  "Initializes the iterative solver for the sparsity pattern of the "
36  "matrix A for further solving Ax=b problems.",
37  bp::return_self<>())
38  .def(
39  "factorize",
40  (Solver & (Solver::*)(const Eigen::EigenBase<MatrixType>& matrix)) &
41  Solver::factorize,
42  bp::args("self", "A"),
43  "Initializes the iterative solver with the numerical values of the "
44  "matrix A for further solving Ax=b problems.",
45  bp::return_self<>())
46  .def(
47  "compute",
48  (Solver & (Solver::*)(const Eigen::EigenBase<MatrixType>& matrix)) &
49  Solver::compute,
50  bp::args("self", "A"),
51  "Initializes the iterative solver with the matrix A for further "
52  "solving Ax=b problems.",
53  bp::return_self<>())
54 
55  .def("rows", &Solver::rows, bp::arg("self"),
56  "Returns the number of rows.")
57  .def("cols", &Solver::cols, bp::arg("self"),
58  "Returns the number of columns.")
59  .def("tolerance", &Solver::tolerance, bp::arg("self"),
60  "Returns the tolerance threshold used by the stopping criteria.")
61  .def("setTolerance", &Solver::setTolerance,
62  bp::args("self", "tolerance"),
63  "Sets the tolerance threshold used by the stopping criteria.\n"
64  "This value is used as an upper bound to the relative residual "
65  "error: |Ax-b|/|b|.\n"
66  "The default value is the machine precision given by "
67  "NumTraits<Scalar>::epsilon().",
68  bp::return_self<>())
69  .def("preconditioner",
70  (Preconditioner & (Solver::*)()) & Solver::preconditioner,
71  bp::arg("self"),
72  "Returns a read-write reference to the preconditioner for custom "
73  "configuration.",
74  bp::return_internal_reference<>())
75 
76  .def("maxIterations", &Solver::maxIterations, bp::arg("self"),
77  "Returns the max number of iterations.\n"
78  "It is either the value setted by setMaxIterations or, by "
79  "default, twice the number of columns of the matrix.")
80  .def("setMaxIterations", &Solver::setMaxIterations,
81  bp::args("self", "max_iterations"),
82  "Sets the max number of iterations.\n"
83  "Default is twice the number of columns of the matrix.",
84  bp::return_self<>())
85 
86  .def(
87  "iterations", &Solver::iterations, bp::arg("self"),
88  "Returns the number of iterations performed during the last solve.")
89  .def("error", &Solver::error, bp::arg("self"),
90  "Returns the tolerance error reached during the last solve.\n"
91  "It is a close approximation of the true relative residual error "
92  "|Ax-b|/|b|.")
93  .def("info", &Solver::error, bp::arg("info"),
94  "Returns Success if the iterations converged, and NoConvergence "
95  "otherwise.")
96 
97  .def("solveWithGuess", &solveWithGuess<MatrixXs, MatrixXs>,
98  bp::args("self", "b", "x0"),
99  "Returns the solution x of A x = b using the current "
100  "decomposition of A and x0 as an initial solution.")
101 
102  .def(
103  "solve", &solve<MatrixXs>, bp::args("self", "b"),
104  "Returns the solution x of A x = b using the current decomposition "
105  "of A where b is a right hand side matrix or vector.");
106  }
107 
108  private:
109  template <typename MatrixOrVector1, typename MatrixOrVector2>
110  static MatrixOrVector1 solveWithGuess(const Solver& self,
111  const MatrixOrVector1& b,
112  const MatrixOrVector2& guess) {
113  return self.solveWithGuess(b, guess);
114  }
115 
116  template <typename MatrixOrVector>
117  static MatrixOrVector solve(const Solver& self,
118  const MatrixOrVector& mat_or_vec) {
119  MatrixOrVector res = self.solve(mat_or_vec);
120  return res;
121  }
122 };
123 
124 template <typename _MatrixType>
126  : public boost::python::def_visitor<MINRESSolverVisitor<_MatrixType> > {
127  typedef _MatrixType MatrixType;
128  typedef typename MatrixType::Scalar Scalar;
129  typedef typename MatrixType::RealScalar RealScalar;
130  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, MatrixType::Options>
132  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic,
133  MatrixType::Options>
135  typedef Eigen::MINRES<MatrixType> Solver;
136 
137  template <class PyClass>
138  void visit(PyClass& cl) const {
139  cl.def(bp::init<>(bp::arg("self"), "Default constructor"))
140  .def(bp::init<MatrixType>(
141  bp::args("self", "matrix"),
142  "Initialize the solver with matrix A for further Ax=b solving.\n"
143  "This constructor is a shortcut for the default constructor "
144  "followed by a call to compute()."))
145 
147  }
148 
149  static void expose() {
150  static const std::string classname =
151  "MINRES" + scalar_name<Scalar>::shortname();
152  expose(classname);
153  }
154 
155  static void expose(const std::string& name) {
156  bp::class_<Solver, boost::noncopyable>(
157  name.c_str(),
158  "A minimal residual solver for sparse symmetric problems.\n"
159  "This class allows to solve for A.x = b sparse linear problems using "
160  "the MINRES algorithm of Paige and Saunders (1975). The sparse matrix "
161  "A must be symmetric (possibly indefinite). The vectors x and b can be "
162  "either dense or sparse.\n"
163  "The maximal number of iterations and tolerance value can be "
164  "controlled via the setMaxIterations() and setTolerance() methods. The "
165  "defaults are the size of the problem for the maximal number of "
166  "iterations and NumTraits<Scalar>::epsilon() for the tolerance.\n",
167  bp::no_init)
168  .def(MINRESSolverVisitor())
169  .def(IdVisitor<Solver>());
170  }
171 
172  private:
173  template <typename MatrixOrVector>
174  static MatrixOrVector solve(const Solver& self, const MatrixOrVector& vec) {
175  return self.solve(vec);
176  }
177 };
178 
179 } // namespace eigenpy
180 
181 #endif // ifndef __eigenpy_decompositions_minres_hpp__
eigenpy::IterativeSolverBaseVisitor
Definition: minres.hpp:17
eigenpy::MINRESSolverVisitor::MatrixType
_MatrixType MatrixType
Definition: minres.hpp:127
eigenpy::IterativeSolverBaseVisitor::MatrixType
Solver::MatrixType MatrixType
Definition: minres.hpp:20
eigenpy::MINRESSolverVisitor::expose
static void expose()
Definition: minres.hpp:149
eigenpy::IterativeSolverBaseVisitor::Solver
_Solver Solver
Definition: minres.hpp:19
scalar-name.hpp
test_complex.rows
int rows
Definition: test_complex.py:4
test_matrix.vec
vec
Definition: test_matrix.py:180
eigenpy::IterativeSolverBaseVisitor::solve
static MatrixOrVector solve(const Solver &self, const MatrixOrVector &mat_or_vec)
Definition: minres.hpp:117
eigenpy::MINRESSolverVisitor::MatrixXs
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, MatrixType::Options > MatrixXs
Definition: minres.hpp:134
eigenpy::IterativeSolverBaseVisitor::visit
void visit(PyClass &cl) const
Definition: minres.hpp:30
eigenpy
Definition: alignment.hpp:14
eigenpy::MINRESSolverVisitor::Solver
Eigen::MINRES< MatrixType > Solver
Definition: minres.hpp:135
eigenpy::scalar_name::shortname
static std::string shortname()
test_geometry.res
res
Definition: test_geometry.py:88
eigenpy::MINRESSolverVisitor::VectorXs
Eigen::Matrix< Scalar, Eigen::Dynamic, 1, MatrixType::Options > VectorXs
Definition: minres.hpp:131
eigenpy::MINRESSolverVisitor::RealScalar
MatrixType::RealScalar RealScalar
Definition: minres.hpp:129
eigenpy::IterativeSolverBaseVisitor::MatrixXs
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, MatrixType::Options > MatrixXs
Definition: minres.hpp:27
eigenpy::IterativeSolverBaseVisitor::Scalar
Solver::Scalar Scalar
Definition: minres.hpp:22
eigenpy::MINRESSolverVisitor::visit
void visit(PyClass &cl) const
Definition: minres.hpp:138
eigenpy::IdVisitor
Add the Python method id to retrieving a unique id for a given object exposed with Boost....
Definition: id.hpp:18
eigenpy::MINRESSolverVisitor::solve
static MatrixOrVector solve(const Solver &self, const MatrixOrVector &vec)
Definition: minres.hpp:174
eigenpy::IterativeSolverBaseVisitor::RealScalar
Solver::RealScalar RealScalar
Definition: minres.hpp:23
setup.name
name
Definition: setup.in.py:179
eigenpy.hpp
eigenpy::IterativeSolverBaseVisitor::solveWithGuess
static MatrixOrVector1 solveWithGuess(const Solver &self, const MatrixOrVector1 &b, const MatrixOrVector2 &guess)
Definition: minres.hpp:110
test_complex.cols
int cols
Definition: test_complex.py:5
eigenpy::IterativeSolverBaseVisitor::Preconditioner
Solver::Preconditioner Preconditioner
Definition: minres.hpp:21
eigenpy::MINRESSolverVisitor::Scalar
MatrixType::Scalar Scalar
Definition: minres.hpp:128
eigenpy::MINRESSolverVisitor
Definition: minres.hpp:125
eigenpy::MINRESSolverVisitor::expose
static void expose(const std::string &name)
Definition: minres.hpp:155


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