|
| 1 | +/* |
| 2 | + * Copyright 2024 INRIA |
| 3 | + */ |
| 4 | + |
| 5 | +#ifndef __eigenpy_decompositions_sparse_incomplete_cholesky_hpp__ |
| 6 | +#define __eigenpy_decompositions_sparse_incomplete_cholesky_hpp__ |
| 7 | + |
| 8 | +#include "eigenpy/eigenpy.hpp" |
| 9 | +#include "eigenpy/utils/scalar-name.hpp" |
| 10 | + |
| 11 | +namespace eigenpy { |
| 12 | + |
| 13 | +template <typename _MatrixType> |
| 14 | +struct IncompleteCholeskyVisitor : public boost::python::def_visitor< |
| 15 | + IncompleteCholeskyVisitor<_MatrixType>> { |
| 16 | + typedef _MatrixType MatrixType; |
| 17 | + typedef typename MatrixType::Scalar Scalar; |
| 18 | + typedef typename MatrixType::RealScalar RealScalar; |
| 19 | + typedef Eigen::IncompleteCholesky<Scalar> Solver; |
| 20 | + typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, MatrixType::Options> |
| 21 | + DenseVectorXs; |
| 22 | + typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, |
| 23 | + MatrixType::Options> |
| 24 | + DenseMatrixXs; |
| 25 | + |
| 26 | + typedef Eigen::SparseMatrix<Scalar, Eigen::ColMajor> FactorType; |
| 27 | + typedef Eigen::Matrix<RealScalar, Eigen::Dynamic, 1> VectorRx; |
| 28 | + typedef Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic> |
| 29 | + PermutationType; |
| 30 | + |
| 31 | + template <class PyClass> |
| 32 | + void visit(PyClass& cl) const { |
| 33 | + cl.def(bp::init<>(bp::arg("self"), "Default constructor")) |
| 34 | + .def(bp::init<MatrixType>(bp::args("self", "matrix"), |
| 35 | + "Constructs and performs the LDLT " |
| 36 | + "factorization from a given matrix.")) |
| 37 | + |
| 38 | + .def("rows", &Solver::rows, bp::arg("self"), |
| 39 | + "Returns the number of rows of the matrix.") |
| 40 | + .def("cols", &Solver::cols, bp::arg("self"), |
| 41 | + "Returns the number of cols of the matrix.") |
| 42 | + |
| 43 | + .def("info", &Solver::info, bp::arg("self"), |
| 44 | + "Reports whether previous computation was successful.") |
| 45 | + |
| 46 | + .def("setInitialShift", &Solver::setInitialShift, |
| 47 | + bp::args("self", "shift"), "Set the initial shift parameter.") |
| 48 | + |
| 49 | + .def( |
| 50 | + "analyzePattern", |
| 51 | + +[](Solver& self, const MatrixType& amat) { |
| 52 | + self.analyzePattern(amat); |
| 53 | + }, |
| 54 | + bp::arg("matrix")) |
| 55 | + .def( |
| 56 | + "factorize", |
| 57 | + +[](Solver& self, const MatrixType& amat) { self.factorize(amat); }, |
| 58 | + bp::arg("matrix")) |
| 59 | + .def( |
| 60 | + "compute", |
| 61 | + +[](Solver& self, const MatrixType& amat) { self.compute(amat); }, |
| 62 | + bp::arg("matrix")) |
| 63 | + |
| 64 | + .def( |
| 65 | + "matrixL", |
| 66 | + +[](Solver& self) -> const FactorType& { return self.matrixL(); }, |
| 67 | + bp::return_value_policy<bp::copy_const_reference>()) |
| 68 | + .def( |
| 69 | + "scalingS", |
| 70 | + +[](Solver& self) -> const VectorRx& { return self.scalingS(); }, |
| 71 | + bp::return_value_policy<bp::copy_const_reference>()) |
| 72 | + .def( |
| 73 | + "permutationP", |
| 74 | + +[](Solver& self) -> const PermutationType& { |
| 75 | + return self.permutationP(); |
| 76 | + }, |
| 77 | + bp::return_value_policy<bp::copy_const_reference>()) |
| 78 | + |
| 79 | + .def( |
| 80 | + "solve", |
| 81 | + +[](Solver const& self, const Eigen::Ref<DenseVectorXs const>& b) |
| 82 | + -> DenseVectorXs { return self.solve(b); }, |
| 83 | + bp::arg("b"), |
| 84 | + "Returns the solution x of A x = b using the current decomposition " |
| 85 | + "of A, where b is a right hand side vector.") |
| 86 | + .def( |
| 87 | + "solve", |
| 88 | + +[](Solver const& self, const Eigen::Ref<DenseMatrixXs const>& B) |
| 89 | + -> DenseMatrixXs { return self.solve(B); }, |
| 90 | + bp::arg("b"), |
| 91 | + "Returns the solution X of A X = B using the current decomposition " |
| 92 | + "of A where B is a right hand side matrix.") |
| 93 | + .def( |
| 94 | + "solve", |
| 95 | + +[](Solver const& self, const MatrixType& B) -> MatrixType { |
| 96 | + DenseMatrixXs B_dense = DenseMatrixXs(B); |
| 97 | + DenseMatrixXs X_dense = self.solve(B_dense); |
| 98 | + return MatrixType(X_dense.sparseView()); |
| 99 | + }, |
| 100 | + bp::arg("b"), |
| 101 | + "Returns the solution X of A X = B using the current decomposition " |
| 102 | + "of A where B is a right hand side matrix."); |
| 103 | + } |
| 104 | + |
| 105 | + static void expose() { |
| 106 | + static const std::string classname = |
| 107 | + "IncompleteCholesky_" + scalar_name<Scalar>::shortname(); |
| 108 | + expose(classname); |
| 109 | + } |
| 110 | + |
| 111 | + static void expose(const std::string& name) { |
| 112 | + bp::class_<Solver, boost::noncopyable>(name.c_str(), "Incomplete Cholesky.", |
| 113 | + bp::no_init) |
| 114 | + .def(IncompleteCholeskyVisitor()) |
| 115 | + .def(IdVisitor<Solver>()); |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +} // namespace eigenpy |
| 120 | + |
| 121 | +#endif // ifndef __eigenpy_decompositions_sparse_incomplete_cholesky_hpp__ |
0 commit comments