eigenpy 3.12.0
Bindings between Numpy and Eigen using Boost.Python
Loading...
Searching...
No Matches
SparseLU.hpp
1/*
2 * Copyright 2025 INRIA
3 */
4
5#ifndef __eigenpy_decompositions_sparse_lu_hpp__
6#define __eigenpy_decompositions_sparse_lu_hpp__
7
8#include <Eigen/SparseLU>
9#include <Eigen/Core>
10
11#include "eigenpy/eigenpy.hpp"
12#include "eigenpy/decompositions/sparse/SparseSolverBase.hpp"
13#include "eigenpy/utils/scalar-name.hpp"
14
15namespace eigenpy {
16
17template <typename MappedSupernodalType>
19 : public boost::python::def_visitor<
20 SparseLUMatrixLReturnTypeVisitor<MappedSupernodalType>> {
21 typedef Eigen::SparseLUMatrixLReturnType<MappedSupernodalType> LType;
22 typedef typename MappedSupernodalType::Scalar Scalar;
23 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Eigen::ColMajor> VectorXs;
24 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
25 MatrixXs;
26
27 template <typename MatrixOrVector>
28 static void solveInPlace(const LType &self,
29 Eigen::Ref<MatrixOrVector> mat_vec) {
30 self.solveInPlace(mat_vec);
31 }
32
33 template <class PyClass>
34 void visit(PyClass &cl) const {
35 cl.def(bp::init<MappedSupernodalType>(bp::args("self", "mapL")))
36
37 .def("rows", &LType::rows)
38 .def("cols", &LType::cols)
39
40 .def("solveInPlace", &solveInPlace<MatrixXs>, bp::args("self", "X"))
41 .def("solveInPlace", &solveInPlace<VectorXs>, bp::args("self", "x"));
42 }
43
44 static void expose(const std::string &name) {
45 bp::class_<LType>(name.c_str(), "Eigen SparseLUMatrixLReturnType",
46 bp::no_init)
48 .def(IdVisitor<LType>());
49 }
50};
51
52template <typename MatrixLType, typename MatrixUType>
54 : public boost::python::def_visitor<
55 SparseLUMatrixUReturnTypeVisitor<MatrixLType, MatrixUType>> {
56 typedef Eigen::SparseLUMatrixUReturnType<MatrixLType, MatrixUType> UType;
57 typedef typename MatrixLType::Scalar Scalar;
58 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Eigen::ColMajor> VectorXs;
59 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
60 MatrixXs;
61
62 template <typename MatrixOrVector>
63 static void solveInPlace(const UType &self,
64 Eigen::Ref<MatrixOrVector> mat_vec) {
65 self.solveInPlace(mat_vec);
66 }
67
68 template <class PyClass>
69 void visit(PyClass &cl) const {
70 cl.def(bp::init<MatrixLType, MatrixUType>(bp::args("self", "mapL", "mapU")))
71
72 .def("rows", &UType::rows)
73 .def("cols", &UType::cols)
74
75 .def("solveInPlace", &solveInPlace<MatrixXs>, bp::args("self", "X"))
76 .def("solveInPlace", &solveInPlace<VectorXs>, bp::args("self", "x"));
77 }
78
79 static void expose(const std::string &name) {
80 bp::class_<UType>(name.c_str(), "Eigen SparseLUMatrixUReturnType",
81 bp::no_init)
83 .def(IdVisitor<UType>());
84 }
85};
86
87template <typename _MatrixType,
88 typename _Ordering =
89 Eigen::COLAMDOrdering<typename _MatrixType::StorageIndex>>
90struct SparseLUVisitor : public boost::python::def_visitor<
91 SparseLUVisitor<_MatrixType, _Ordering>> {
93 typedef _MatrixType MatrixType;
94
95 typedef Eigen::SparseLU<MatrixType> Solver;
96 typedef typename MatrixType::Scalar Scalar;
97 typedef typename MatrixType::RealScalar RealScalar;
98
99 typedef typename Solver::SCMatrix SCMatrix;
100 typedef typename MatrixType::StorageIndex StorageIndex;
101 typedef Eigen::MappedSparseMatrix<Scalar, Eigen::ColMajor, StorageIndex>
102 MappedSparseMatrix;
103 typedef Eigen::SparseLUMatrixLReturnType<SCMatrix> LType;
104 typedef Eigen::SparseLUMatrixUReturnType<SCMatrix, MappedSparseMatrix> UType;
105
106 template <class PyClass>
107 void visit(PyClass &cl) const {
108 cl.def(bp::init<>(bp::arg("self"), "Default constructor"))
109 .def(bp::init<MatrixType>(bp::args("self", "matrix"),
110 "Constructs and performs the LU "
111 "factorization from a given matrix."))
112
113 .def("determinant", &Solver::determinant, bp::arg("self"),
114 "Returns the determinant of the matrix.")
115 .def("signDeterminant", &Solver::signDeterminant, bp::arg("self"),
116 "A number representing the sign of the determinant. ")
117 .def("absDeterminant", &Solver::absDeterminant, bp::arg("self"),
118 "Returns the absolute value of the determinant of the matrix of "
119 "which *this is the QR decomposition.")
120 .def("logAbsDeterminant", &Solver::logAbsDeterminant, bp::arg("self"),
121 "Returns the natural log of the absolute value of the determinant "
122 "of the matrix of which *this is the QR decomposition")
123
124 .def("rows", &Solver::rows, bp::arg("self"),
125 "Returns the number of rows of the matrix.")
126 .def("cols", &Solver::cols, bp::arg("self"),
127 "Returns the number of cols of the matrix.")
128
129 .def("nnzL", &Solver::nnzL, bp::arg("self"),
130 "The number of non zero elements in L")
131 .def("nnzU", &Solver::nnzU, bp::arg("self"),
132 "The number of non zero elements in U")
133
134 .def(
135 "matrixL",
136 +[](const Solver &self) -> LType { return self.matrixL(); },
137 "Returns an expression of the matrix L.")
138 .def(
139 "matrixU",
140 +[](const Solver &self) -> UType { return self.matrixU(); },
141 "Returns an expression of the matrix U.")
142
143 .def("colsPermutation", &Solver::colsPermutation, bp::arg("self"),
144 "Returns a reference to the column matrix permutation PTc such "
145 "that Pr A PTc = LU.",
146 bp::return_value_policy<bp::copy_const_reference>())
147 .def("rowsPermutation", &Solver::rowsPermutation, bp::arg("self"),
148 "Returns a reference to the row matrix permutation Pr such that "
149 "Pr A PTc = LU",
150 bp::return_value_policy<bp::copy_const_reference>())
151
152 .def("compute", &Solver::compute, bp::args("self", "matrix"),
153 "Compute the symbolic and numeric factorization of the input "
154 "sparse matrix. "
155 "The input matrix should be in column-major storage. ")
156
157 .def("analyzePattern", &Solver::analyzePattern,
158 bp::args("self", "matrix"),
159 "Compute the column permutation to minimize the fill-in.")
160 .def("factorize", &Solver::factorize, bp::args("self", "matrix"),
161 "Performs a numeric decomposition of a given matrix.\n"
162 "The given matrix must has the same sparcity than the matrix on "
163 "which the symbolic decomposition has been performed.")
164
165 .def("isSymmetric", &Solver::isSymmetric, bp::args("self", "sym"),
166 "Indicate that the pattern of the input matrix is symmetric. ")
167
168 .def("setPivotThreshold", &Solver::setPivotThreshold,
169 bp::args("self", "thresh"),
170 "Set the threshold used for a diagonal entry to be an acceptable "
171 "pivot.")
172
173 .def("info", &Solver::info, bp::arg("self"),
174 "NumericalIssue if the input contains INF or NaN values or "
175 "overflow occured. Returns Success otherwise.")
176 .def("lastErrorMessage", &Solver::lastErrorMessage, bp::arg("self"),
177 "Returns a string describing the type of error. ")
178
180 }
181
182 static void expose() {
183 static const std::string classname =
184 "SparseLU_" + scalar_name<Scalar>::shortname();
185 expose(classname);
186 }
187
188 static void expose(const std::string &name) {
189 bp::class_<Solver, boost::noncopyable>(
190 name.c_str(),
191 "Sparse supernodal LU factorization for general matrices. \n\n"
192 "This class implements the supernodal LU factorization for general "
193 "matrices. "
194 "It uses the main techniques from the sequential SuperLU package "
195 "(http://crd-legacy.lbl.gov/~xiaoye/SuperLU/). It handles "
196 "transparently real "
197 "and complex arithmetic with single and double precision, depending on "
198 "the scalar "
199 "type of your input matrix. The code has been optimized to provide "
200 "BLAS-3 operations "
201 "during supernode-panel updates. It benefits directly from the "
202 "built-in high-performant "
203 "Eigen BLAS routines. Moreover, when the size of a supernode is very "
204 "small, the BLAS "
205 "calls are avoided to enable a better optimization from the compiler. "
206 "For best performance, "
207 "you should compile it with NDEBUG flag to avoid the numerous bounds "
208 "checking on vectors.",
209 bp::no_init)
210 .def(SparseLUVisitor())
211 .def(IdVisitor<Solver>());
212 }
213};
214
215} // namespace eigenpy
216
217#endif // ifndef __eigenpy_decompositions_sparse_lu_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