eigenpy 3.12.0
Bindings between Numpy and Eigen using Boost.Python
Loading...
Searching...
No Matches
PartialPivLU.hpp
1/*
2 * Copyright 2025 INRIA
3 */
4
5#ifndef __eigenpy_decompositions_partialpivlu_hpp__
6#define __eigenpy_decompositions_partialpivlu_hpp__
7
8#include <Eigen/LU>
9#include <Eigen/Core>
10
11#include "eigenpy/eigenpy.hpp"
12#include "eigenpy/utils/scalar-name.hpp"
13#include "eigenpy/eigen/EigenBase.hpp"
14
15namespace eigenpy {
16
17template <typename _MatrixType>
18struct PartialPivLUSolverVisitor : public boost::python::def_visitor<
19 PartialPivLUSolverVisitor<_MatrixType>> {
20 typedef _MatrixType MatrixType;
21 typedef typename MatrixType::Scalar Scalar;
22 typedef typename MatrixType::RealScalar RealScalar;
23 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, MatrixType::Options>
24 VectorXs;
25 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic,
26 MatrixType::Options>
27 MatrixXs;
28 typedef Eigen::PartialPivLU<MatrixType> Solver;
29
30 template <class PyClass>
31 void visit(PyClass &cl) const {
32 cl.def(bp::init<>(bp::arg("self"), "Default constructor"))
33 .def(bp::init<Eigen::DenseIndex>(
34 bp::args("self", "size"),
35 "Default constructor with memory preallocation"))
36 .def(bp::init<MatrixType>(
37 bp::args("self", "matrix"),
38 "Constructs a LU factorization from a given matrix."))
39
41
42 .def("determinant", &Solver::determinant, bp::arg("self"),
43 "Returns the determinant of the matrix of which *this is the LU "
44 "decomposition.")
45 .def(
46 "compute",
47 (Solver & (Solver::*)(const Eigen::EigenBase<MatrixType> &matrix)) &
48 Solver::compute,
49 bp::args("self", "matrix"),
50 "Computes the LU factorization of given matrix.",
51 bp::return_self<>())
52 .def(
53 "inverse",
54 +[](const Solver &self) -> MatrixType { return self.inverse(); },
55 bp::arg("self"),
56 "Returns the inverse of the matrix of which *this is the LU "
57 "decomposition.")
58 .def("matrixLU", &Solver::matrixLU, bp::arg("self"),
59 "Returns the LU decomposition matrix.",
60 bp::return_internal_reference<>())
61
62 .def("permutationP", &Solver::permutationP, bp::arg("self"),
63 "Returns the permutation P.",
64 bp::return_value_policy<bp::copy_const_reference>())
65
66 .def("rcond", &Solver::rcond, bp::arg("self"),
67 "Returns an estimate of the reciprocal condition number of the "
68 "matrix.")
69 .def("reconstructedMatrix", &Solver::reconstructedMatrix,
70 bp::arg("self"),
71 "Returns the matrix represented by the decomposition, i.e., it "
72 "returns the product: P-1LUQ-1. This function is provided for "
73 "debug "
74 "purpose.")
75 .def("solve", &solve<VectorXs>, bp::args("self", "b"),
76 "Returns the solution x of A x = b using the current "
77 "decomposition of A.")
78 .def("solve", &solve<MatrixXs>, 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
83 static void expose() {
84 static const std::string classname =
85 "FullPivLU" + scalar_name<Scalar>::shortname();
86 expose(classname);
87 }
88
89 static void expose(const std::string &name) {
90 bp::class_<Solver>(
91 name.c_str(),
92 "LU decomposition of a matrix with partial pivoting, "
93 "and related features. \n\n"
94 "This class represents a LU decomposition of a square "
95 "invertible matrix, "
96 "with partial pivoting: the matrix A is decomposed as A "
97 "= PLU where L is "
98 "unit-lower-triangular, U is upper-triangular, and P is "
99 "a permutation matrix.\n\n"
100 "Typically, partial pivoting LU decomposition is only "
101 "considered numerically "
102 "stable for square invertible matrices. Thus LAPACK's "
103 "dgesv and dgesvx require "
104 "the matrix to be square and invertible. The present "
105 "class does the same. It "
106 "will assert that the matrix is square, but it won't "
107 "(actually it can't) check "
108 "that the matrix is invertible: it is your task to "
109 "check that you only use this "
110 "decomposition on invertible matrices. \n\n"
111 "The guaranteed safe alternative, working for all matrices, "
112 "is the full pivoting LU decomposition, provided by class "
113 "FullPivLU. \n\n"
114 "This is not a rank-revealing LU decomposition. Many features "
115 "are intentionally absent from this class, such as "
116 "rank computation. If you need these features, use class "
117 "FullPivLU. \n\n"
118 "This LU decomposition is suitable to invert invertible "
119 "matrices. It is what MatrixBase::inverse() uses in the "
120 "general case. On the other hand, it is not suitable to "
121 "determine whether a given matrix is invertible. \n\n"
122 "The data of the LU decomposition can be directly accessed "
123 "through the methods matrixLU(), permutationP().",
124 bp::no_init)
125 .def(IdVisitor<Solver>())
127 }
128
129 private:
130 template <typename MatrixOrVector>
131 static MatrixOrVector solve(const Solver &self, const MatrixOrVector &vec) {
132 return self.solve(vec);
133 }
134};
135
136} // namespace eigenpy
137
138#endif // ifndef __eigenpy_decompositions_partialpivlu_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