Skip to content

Commit 4fd007e

Browse files
committed
Added all preconditioners in solvers
1 parent f5da84e commit 4fd007e

File tree

6 files changed

+90
-65
lines changed

6 files changed

+90
-65
lines changed

include/eigenpy/decompositions/sparse/IncompleteLUT.hpp

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ namespace eigenpy {
1212

1313
template <typename _MatrixType>
1414
struct IncompleteLUTVisitor
15-
: public boost::python::def_visitor<
16-
IncompleteLUTVisitor<_MatrixType>> {
17-
15+
: public boost::python::def_visitor<IncompleteLUTVisitor<_MatrixType>> {
1816
typedef _MatrixType MatrixType;
1917
typedef typename MatrixType::Scalar Scalar;
2018
typedef typename MatrixType::RealScalar RealScalar;
@@ -26,68 +24,67 @@ struct IncompleteLUTVisitor
2624
DenseMatrixXs;
2725

2826
template <class PyClass>
29-
void visit(PyClass &cl) const {
27+
void visit(PyClass& cl) const {
3028
cl.def(bp::init<>(bp::arg("self"), "Default constructor"))
3129
.def(bp::init<MatrixType>(bp::args("self", "matrix"),
3230
"Constructs and performs the LDLT "
3331
"factorization from a given matrix."))
3432
.def(bp::init<const MatrixType&, RealScalar, int>(
35-
(bp::arg("matrix"),
36-
bp::arg("droptol") = Eigen::NumTraits<Scalar>::dummy_precision(),
37-
bp::arg("fillfactor") = 10),
38-
"Constructs an incomplete LU factorization from a given matrix."))
33+
(bp::arg("matrix"),
34+
bp::arg("droptol") = Eigen::NumTraits<Scalar>::dummy_precision(),
35+
bp::arg("fillfactor") = 10),
36+
"Constructs an incomplete LU factorization from a given matrix."))
3937

40-
.def("rows", &Solver::rows, bp::arg("self"),
38+
.def("rows", &Solver::rows, bp::arg("self"),
4139
"Returns the number of rows of the matrix.")
42-
.def("cols", &Solver::cols, bp::arg("self"),
40+
.def("cols", &Solver::cols, bp::arg("self"),
4341
"Returns the number of cols of the matrix.")
4442

4543
.def("info", &Solver::info, bp::arg("self"),
46-
"Reports whether previous computation was successful.")
44+
"Reports whether previous computation was successful.")
4745

4846
.def(
49-
"analyzePattern",
50-
+[](Solver& self, const MatrixType& amat) {
51-
self.analyzePattern(amat);
52-
},
53-
bp::arg("matrix"))
47+
"analyzePattern",
48+
+[](Solver& self, const MatrixType& amat) {
49+
self.analyzePattern(amat);
50+
},
51+
bp::arg("matrix"))
5452
.def(
55-
"factorize",
56-
+[](Solver& self, const MatrixType& amat) { self.factorize(amat); },
57-
bp::arg("matrix"))
53+
"factorize",
54+
+[](Solver& self, const MatrixType& amat) { self.factorize(amat); },
55+
bp::arg("matrix"))
5856
.def(
59-
"compute",
60-
+[](Solver& self, const MatrixType& amat) { self.compute(amat); },
61-
bp::arg("matrix"))
57+
"compute",
58+
+[](Solver& self, const MatrixType& amat) { self.compute(amat); },
59+
bp::arg("matrix"))
6260

6361
.def("setDroptol", &Solver::setDroptol, bp::arg("self"))
6462
.def("setFillfactor", &Solver::setFillfactor, bp::arg("self"))
6563

6664
.def(
67-
"solve",
68-
+[](Solver const& self, const Eigen::Ref<DenseVectorXs const>& b)
69-
-> DenseVectorXs { return self.solve(b); },
70-
bp::arg("b"),
71-
"Returns the solution x of A x = b using the current decomposition "
72-
"of A, where b is a right hand side vector.")
65+
"solve",
66+
+[](Solver const& self, const Eigen::Ref<DenseVectorXs const>& b)
67+
-> DenseVectorXs { return self.solve(b); },
68+
bp::arg("b"),
69+
"Returns the solution x of A x = b using the current decomposition "
70+
"of A, where b is a right hand side vector.")
7371
.def(
74-
"solve",
75-
+[](Solver const& self, const Eigen::Ref<DenseMatrixXs const>& B)
76-
-> DenseMatrixXs { return self.solve(B); },
77-
bp::arg("b"),
78-
"Returns the solution X of A X = B using the current decomposition "
79-
"of A where B is a right hand side matrix.")
72+
"solve",
73+
+[](Solver const& self, const Eigen::Ref<DenseMatrixXs const>& B)
74+
-> DenseMatrixXs { return self.solve(B); },
75+
bp::arg("b"),
76+
"Returns the solution X of A X = B using the current decomposition "
77+
"of A where B is a right hand side matrix.")
8078
.def(
81-
"solve",
82-
+[](Solver const& self, const MatrixType& B) -> MatrixType {
83-
DenseMatrixXs B_dense = DenseMatrixXs(B);
84-
DenseMatrixXs X_dense = self.solve(B_dense);
85-
return MatrixType(X_dense.sparseView());
86-
},
87-
bp::arg("b"),
88-
"Returns the solution X of A X = B using the current decomposition "
89-
"of A where B is a right hand side matrix.")
90-
;
79+
"solve",
80+
+[](Solver const& self, const MatrixType& B) -> MatrixType {
81+
DenseMatrixXs B_dense = DenseMatrixXs(B);
82+
DenseMatrixXs X_dense = self.solve(B_dense);
83+
return MatrixType(X_dense.sparseView());
84+
},
85+
bp::arg("b"),
86+
"Returns the solution X of A X = B using the current decomposition "
87+
"of A where B is a right hand side matrix.");
9188
}
9289

9390
static void expose() {
@@ -96,7 +93,7 @@ struct IncompleteLUTVisitor
9693
expose(classname);
9794
}
9895

99-
static void expose(const std::string &name) {
96+
static void expose(const std::string& name) {
10097
bp::class_<Solver, boost::noncopyable>(
10198
name.c_str(),
10299
"Incomplete LU factorization with dual-threshold strategy.",

include/eigenpy/solvers/BiCGSTAB.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace eigenpy {
1515
template <typename BiCGSTAB>
1616
struct BiCGSTABVisitor
1717
: public boost::python::def_visitor<BiCGSTABVisitor<BiCGSTAB>> {
18-
typedef Eigen::MatrixXd MatrixType;
18+
typedef typename BiCGSTAB::MatrixType MatrixType;
1919

2020
template <class PyClass>
2121
void visit(PyClass& cl) const {
@@ -28,8 +28,8 @@ struct BiCGSTABVisitor
2828
"followed by a call to compute()."));
2929
}
3030

31-
static void expose() {
32-
bp::class_<BiCGSTAB, boost::noncopyable>("BiCGSTAB", bp::no_init)
31+
static void expose(const std::string& name = "BiCGSTAB") {
32+
bp::class_<BiCGSTAB, boost::noncopyable>(name.c_str(), bp::no_init)
3333
.def(IterativeSolverVisitor<BiCGSTAB>())
3434
.def(BiCGSTABVisitor<BiCGSTAB>())
3535
.def(IdVisitor<BiCGSTAB>());

include/eigenpy/solvers/LeastSquaresConjugateGradient.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ template <typename LeastSquaresConjugateGradient>
1616
struct LeastSquaresConjugateGradientVisitor
1717
: public boost::python::def_visitor<
1818
LeastSquaresConjugateGradientVisitor<LeastSquaresConjugateGradient>> {
19-
typedef Eigen::MatrixXd MatrixType;
19+
typedef typename LeastSquaresConjugateGradient::MatrixType MatrixType;
2020

2121
template <class PyClass>
2222
void visit(PyClass& cl) const {
@@ -29,9 +29,10 @@ struct LeastSquaresConjugateGradientVisitor
2929
"followed by a call to compute()."));
3030
}
3131

32-
static void expose() {
33-
bp::class_<LeastSquaresConjugateGradient, boost::noncopyable>(
34-
"LeastSquaresConjugateGradient", bp::no_init)
32+
static void expose(
33+
const std::string& name = "LeastSquaresConjugateGradient") {
34+
bp::class_<LeastSquaresConjugateGradient, boost::noncopyable>(name.c_str(),
35+
bp::no_init)
3536
.def(IterativeSolverVisitor<LeastSquaresConjugateGradient>())
3637
.def(LeastSquaresConjugateGradientVisitor<
3738
LeastSquaresConjugateGradient>())

src/solvers/solvers.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,43 @@
1818
namespace eigenpy {
1919
void exposeSolvers() {
2020
using namespace Eigen;
21-
ConjugateGradientVisitor<
22-
ConjugateGradient<MatrixXd, Lower | Upper>>::expose();
21+
22+
using Eigen::Lower;
23+
using Eigen::Upper;
24+
25+
using Eigen::BiCGSTAB;
26+
using Eigen::ConjugateGradient;
27+
using Eigen::LeastSquaresConjugateGradient;
28+
29+
using Eigen::DiagonalPreconditioner;
30+
using Eigen::IdentityPreconditioner;
31+
using Eigen::LeastSquareDiagonalPreconditioner;
32+
33+
using IdentityBiCGSTAB = BiCGSTAB<MatrixXd, IdentityPreconditioner>;
34+
using IdentityConjugateGradient =
35+
ConjugateGradient<MatrixXd, Lower | Upper, IdentityPreconditioner>;
36+
using IdentityLeastSquaresConjugateGradient =
37+
LeastSquaresConjugateGradient<MatrixXd, IdentityPreconditioner>;
38+
using DiagonalLeastSquaresConjugateGradient = LeastSquaresConjugateGradient<
39+
MatrixXd, DiagonalPreconditioner<typename MatrixXd::Scalar>>;
40+
41+
ConjugateGradientVisitor<ConjugateGradient<MatrixXd, Lower | Upper>>::expose(
42+
"ConjugateGradient");
43+
ConjugateGradientVisitor<IdentityConjugateGradient>::expose(
44+
"IdentityConjugateGradient");
45+
2346
#if EIGEN_VERSION_AT_LEAST(3, 3, 5)
2447
LeastSquaresConjugateGradientVisitor<LeastSquaresConjugateGradient<
25-
MatrixXd, LeastSquareDiagonalPreconditioner<MatrixXd::Scalar>>>::expose();
48+
MatrixXd, LeastSquareDiagonalPreconditioner<MatrixXd::Scalar>>>::
49+
expose("LeastSquaresConjugateGradient");
50+
LeastSquaresConjugateGradientVisitor<IdentityLeastSquaresConjugateGradient>::
51+
expose("IdentityLeastSquaresConjugateGradient");
52+
LeastSquaresConjugateGradientVisitor<DiagonalLeastSquaresConjugateGradient>::
53+
expose("DiagonalLeastSquaresConjugateGradient");
2654
#endif
2755

28-
// Conjugate gradient with limited BFGS preconditioner
29-
ConjugateGradientVisitor<
30-
ConjugateGradient<MatrixXd, Lower | Upper, IdentityPreconditioner>>::
31-
expose("IdentityConjugateGradient");
32-
// ConjugateGradientVisitor<
33-
// ConjugateGradient<MatrixXd,Lower|Upper,LimitedBFGSPreconditioner<double,Dynamic,Lower|Upper>
34-
// > >::expose("LimitedBFGSConjugateGradient");
35-
36-
BiCGSTABVisitor<BiCGSTAB<
37-
MatrixXd, LeastSquareDiagonalPreconditioner<MatrixXd::Scalar>>>::expose();
56+
BiCGSTABVisitor<BiCGSTAB<MatrixXd>>::expose("BiCGSTAB");
57+
BiCGSTABVisitor<IdentityBiCGSTAB>::expose("IdentityBiCGSTAB");
3858
}
3959
} // namespace eigenpy
4060

unittest/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ if(BUILD_TESTING_SCIPY)
219219
add_python_eigenpy_unit_test(
220220
"py-SimplicialLDLT"
221221
"unittest/python/decompositions/sparse/test_SimplicialLDLT.py")
222+
add_python_eigenpy_unit_test(
223+
"py-IncompleteCholesky"
224+
"unittest/python/decompositions/sparse/test_IncompleteCholesky.py")
225+
add_python_eigenpy_unit_test(
226+
"py-IncompleteLUT"
227+
"unittest/python/decompositions/sparse/test_IncompleteLUT.py")
222228
add_python_eigenpy_unit_test(
223229
"py-SparseLU" "unittest/python/decompositions/sparse/test_SparseLU.py")
224230
add_python_eigenpy_unit_test(

unittest/python/decompositions/sparse/test_IncompleteLUT.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
from scipy.sparse import csc_matrix
3+
34
import eigenpy
45

56
dim = 100

0 commit comments

Comments
 (0)