Skip to content

Commit 37f9dd6

Browse files
committed
sparse: add SparseSolverBaseVisitor
1 parent f5b20db commit 37f9dd6

File tree

4 files changed

+62
-42
lines changed

4 files changed

+62
-42
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ set(${PROJECT_NAME}_DECOMPOSITIONS_SPARSE_CHOLMOD_HEADERS
142142
set(${PROJECT_NAME}_DECOMPOSITIONS_SPARSE_HEADERS
143143
include/eigenpy/decompositions/sparse/LLT.hpp
144144
include/eigenpy/decompositions/sparse/LDLT.hpp
145-
include/eigenpy/decompositions/sparse/SimplicialCholesky.hpp)
145+
include/eigenpy/decompositions/sparse/SimplicialCholesky.hpp
146+
include/eigenpy/decompositions/sparse/SparseSolverBase.hpp)
146147

147148
if(BUILD_WITH_CHOLMOD_SUPPORT)
148149
list(APPEND ${PROJECT_NAME}_DECOMPOSITIONS_SPARSE_HEADERS

include/eigenpy/decompositions/sparse/SimplicialCholesky.hpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "eigenpy/eigenpy.hpp"
99
#include "eigenpy/eigen/EigenBase.hpp"
10+
#include "eigenpy/decompositions/sparse/SparseSolverBase.hpp"
1011

1112
#include <Eigen/SparseCholesky>
1213

@@ -37,6 +38,7 @@ struct SimplicialCholeskyVisitor
3738
"problems having the same structure.")
3839

3940
.def(EigenBaseVisitor<Solver>())
41+
.def(SparseSolverBaseVisitor<Solver>())
4042

4143
.def("matrixL", &matrixL, bp::arg("self"),
4244
"Returns the lower triangular matrix L.")
@@ -74,33 +76,17 @@ struct SimplicialCholeskyVisitor
7476
"scale=1.",
7577
bp::return_self<>())
7678

77-
.def("solve", &solve<DenseVectorXs>, bp::args("self", "b"),
78-
"Returns the solution x of A x = b using the current "
79-
"decomposition of A.")
80-
.def("solve", &solve<DenseMatrixXs>, bp::args("self", "B"),
81-
"Returns the solution X of A X = B using the current "
82-
"decomposition of A where B is a right hand side matrix.")
83-
8479
.def("permutationP", &Solver::permutationP, bp::arg("self"),
8580
"Returns the permutation P.",
8681
bp::return_value_policy<bp::copy_const_reference>())
8782
.def("permutationPinv", &Solver::permutationPinv, bp::arg("self"),
8883
"Returns the inverse P^-1 of the permutation P.",
89-
bp::return_value_policy<bp::copy_const_reference>())
90-
91-
.def("solve", &solve<MatrixType>, bp::args("self", "B"),
92-
"Returns the solution X of A X = B using the current "
93-
"decomposition of A where B is a right hand side matrix.");
84+
bp::return_value_policy<bp::copy_const_reference>());
9485
}
9586

9687
private:
9788
static MatrixType matrixL(const Solver &self) { return self.matrixL(); }
9889
static MatrixType matrixU(const Solver &self) { return self.matrixU(); }
99-
100-
template <typename MatrixOrVector>
101-
static MatrixOrVector solve(const Solver &self, const MatrixOrVector &vec) {
102-
return self.solve(vec);
103-
}
10490
};
10591

10692
} // namespace eigenpy
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2024 INRIA
3+
*/
4+
5+
#ifndef __eigenpy_decomposition_sparse_sparse_solver_base_hpp__
6+
#define __eigenpy_decomposition_sparse_sparse_solver_base_hpp__
7+
8+
#include "eigenpy/eigenpy.hpp"
9+
#include "eigenpy/eigen/EigenBase.hpp"
10+
11+
#include <Eigen/SparseCholesky>
12+
13+
namespace eigenpy {
14+
15+
template <typename SimplicialDerived>
16+
struct SparseSolverBaseVisitor
17+
: public boost::python::def_visitor<
18+
SparseSolverBaseVisitor<SimplicialDerived> > {
19+
typedef SimplicialDerived Solver;
20+
21+
typedef typename SimplicialDerived::MatrixType MatrixType;
22+
typedef typename MatrixType::Scalar Scalar;
23+
typedef typename MatrixType::RealScalar RealScalar;
24+
25+
typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, MatrixType::Options>
26+
DenseVectorXs;
27+
typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic,
28+
MatrixType::Options>
29+
DenseMatrixXs;
30+
31+
template <class PyClass>
32+
void visit(PyClass &cl) const {
33+
cl.def("solve", &solve<DenseVectorXs>, bp::args("self", "b"),
34+
"Returns the solution x of A x = b using the current "
35+
"decomposition of A.")
36+
.def("solve", &solve<DenseMatrixXs>, bp::args("self", "B"),
37+
"Returns the solution X of A X = B using the current "
38+
"decomposition of A where B is a right hand side matrix.")
39+
40+
.def("solve", &solve<MatrixType>, bp::args("self", "B"),
41+
"Returns the solution X of A X = B using the current "
42+
"decomposition of A where B is a right hand side matrix.");
43+
}
44+
45+
private:
46+
template <typename MatrixOrVector>
47+
static MatrixOrVector solve(const Solver &self, const MatrixOrVector &vec) {
48+
return self.solve(vec);
49+
}
50+
};
51+
52+
} // namespace eigenpy
53+
54+
#endif // ifndef __eigenpy_decomposition_sparse_sparse_solver_base_hpp__

include/eigenpy/decompositions/sparse/cholmod/CholmodBase.hpp

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "eigenpy/eigenpy.hpp"
99
#include "eigenpy/eigen/EigenBase.hpp"
10+
#include "eigenpy/decompositions/sparse/SparseSolverBase.hpp"
1011

1112
#include <Eigen/CholmodSupport>
1213

@@ -23,12 +24,6 @@ struct CholmodBaseVisitor
2324
typedef MatrixType CholMatrixType;
2425
typedef typename MatrixType::StorageIndex StorageIndex;
2526

26-
typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, MatrixType::Options>
27-
DenseVectorXs;
28-
typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic,
29-
MatrixType::Options>
30-
DenseMatrixXs;
31-
3227
template <class PyClass>
3328
void visit(PyClass &cl) const {
3429
cl.def("analyzePattern", &Solver::analyzePattern,
@@ -38,6 +33,7 @@ struct CholmodBaseVisitor
3833
"problems having the same structure.")
3934

4035
.def(EigenBaseVisitor<Solver>())
36+
.def(SparseSolverBaseVisitor<Solver>())
4137

4238
.def("compute",
4339
(Solver & (Solver::*)(const MatrixType &matrix)) & Solver::compute,
@@ -70,24 +66,7 @@ struct CholmodBaseVisitor
7066
"are transformed by the following linear model: d_ii = offset + "
7167
"d_ii.\n"
7268
"The default is the identity transformation with offset=0.",
73-
bp::return_self<>())
74-
75-
.def("solve", &solve<DenseVectorXs>, bp::args("self", "b"),
76-
"Returns the solution x of A x = b using the current "
77-
"decomposition of A.")
78-
.def("solve", &solve<DenseMatrixXs>, bp::args("self", "B"),
79-
"Returns the solution X of A X = B using the current "
80-
"decomposition of A where B is a right hand side matrix.")
81-
82-
.def("solve", &solve<MatrixType>, bp::args("self", "B"),
83-
"Returns the solution X of A X = B using the current "
84-
"decomposition of A where B is a right hand side matrix.");
85-
}
86-
87-
private:
88-
template <typename MatrixOrVector>
89-
static MatrixOrVector solve(const Solver &self, const MatrixOrVector &vec) {
90-
return self.solve(vec);
69+
bp::return_self<>());
9170
}
9271
};
9372

0 commit comments

Comments
 (0)