Commit 1f2ae251 by Justin Carpentier

### cholesky: support solve with input matrices

parent 51b79afe
Pipeline #15520 passed with stage
in 14 minutes and 23 seconds
 /* /* * Copyright 2020 INRIA * Copyright 2020-2021 INRIA */ */ #ifndef __eigenpy_decomposition_ldlt_hpp__ #ifndef __eigenpy_decomposition_ldlt_hpp__ ... @@ -23,7 +23,8 @@ namespace eigenpy ... @@ -23,7 +23,8 @@ namespace eigenpy typedef _MatrixType MatrixType; typedef _MatrixType MatrixType; typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::RealScalar RealScalar; typedef typename MatrixType::RealScalar RealScalar; typedef Eigen::Matrix VectorType; typedef Eigen::Matrix VectorXs; typedef Eigen::Matrix MatrixXs; typedef Eigen::LDLT Solver; typedef Eigen::LDLT Solver; template template ... @@ -55,7 +56,7 @@ namespace eigenpy ... @@ -55,7 +56,7 @@ namespace eigenpy "Returns the LDLT decomposition matrix.", "Returns the LDLT decomposition matrix.", bp::return_internal_reference<>()) bp::return_internal_reference<>()) .def("rankUpdate",(Solver & (Solver::*)(const Eigen::MatrixBase &, const RealScalar &))&Solver::template rankUpdate, .def("rankUpdate",(Solver & (Solver::*)(const Eigen::MatrixBase &, const RealScalar &))&Solver::template rankUpdate, bp::args("self","vector","sigma"), bp::args("self","vector","sigma"), bp::return_self<>()) bp::return_self<>()) ... @@ -78,8 +79,10 @@ namespace eigenpy ... @@ -78,8 +79,10 @@ namespace eigenpy #endif #endif .def("reconstructedMatrix",&Solver::reconstructedMatrix,bp::arg("self"), .def("reconstructedMatrix",&Solver::reconstructedMatrix,bp::arg("self"), "Returns the matrix represented by the decomposition, i.e., it returns the product: L L^*. This function is provided for debug purpose.") "Returns the matrix represented by the decomposition, i.e., it returns the product: L L^*. This function is provided for debug purpose.") .def("solve",&solve,bp::args("self","b"), .def("solve",&solve,bp::args("self","b"), "Returns the solution x of A x = b using the current decomposition of A.") "Returns the solution x of A x = b using the current decomposition of A.") .def("solve",&solve,bp::args("self","B"), "Returns the solution X of A X = B using the current decomposition of A where B is a right hand side matrix.") .def("setZero",&Solver::setZero,bp::arg("self"), .def("setZero",&Solver::setZero,bp::arg("self"), "Clear any existing decomposition.") "Clear any existing decomposition.") ... @@ -107,7 +110,7 @@ namespace eigenpy ... @@ -107,7 +110,7 @@ namespace eigenpy static MatrixType matrixL(const Solver & self) { return self.matrixL(); } static MatrixType matrixL(const Solver & self) { return self.matrixL(); } static MatrixType matrixU(const Solver & self) { return self.matrixU(); } static MatrixType matrixU(const Solver & self) { return self.matrixU(); } static VectorType vectorD(const Solver & self) { return self.vectorD(); } static VectorXs vectorD(const Solver & self) { return self.vectorD(); } static MatrixType transpositionsP(const Solver & self) static MatrixType transpositionsP(const Solver & self) { { ... @@ -115,8 +118,8 @@ namespace eigenpy ... @@ -115,8 +118,8 @@ namespace eigenpy self.matrixL().rows()); self.matrixL().rows()); } } template template static VectorType solve(const Solver & self, const VectorType & vec) static MatrixOrVector solve(const Solver & self, const MatrixOrVector & vec) { { return self.solve(vec); return self.solve(vec); } } ... ...
 /* /* * Copyright 2020 INRIA * Copyright 2020-2021 INRIA */ */ #ifndef __eigenpy_decomposition_llt_hpp__ #ifndef __eigenpy_decomposition_llt_hpp__ ... @@ -23,7 +23,8 @@ namespace eigenpy ... @@ -23,7 +23,8 @@ namespace eigenpy typedef _MatrixType MatrixType; typedef _MatrixType MatrixType; typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::RealScalar RealScalar; typedef typename MatrixType::RealScalar RealScalar; typedef Eigen::Matrix VectorType; typedef Eigen::Matrix VectorXs; typedef Eigen::Matrix MatrixXs; typedef Eigen::LLT Solver; typedef Eigen::LLT Solver; template template ... @@ -46,10 +47,10 @@ namespace eigenpy ... @@ -46,10 +47,10 @@ namespace eigenpy bp::return_internal_reference<>()) bp::return_internal_reference<>()) #if EIGEN_VERSION_AT_LEAST(3,3,90) #if EIGEN_VERSION_AT_LEAST(3,3,90) .def("rankUpdate",(Solver& (Solver::*)(const VectorType &, const RealScalar &))&Solver::template rankUpdate, .def("rankUpdate",(Solver& (Solver::*)(const VectorXs &, const RealScalar &))&Solver::template rankUpdate, bp::args("self","vector","sigma"), bp::return_self<>()) bp::args("self","vector","sigma"), bp::return_self<>()) #else #else .def("rankUpdate",(Solver (Solver::*)(const VectorType &, const RealScalar &))&Solver::template rankUpdate, .def("rankUpdate",(Solver (Solver::*)(const VectorXs &, const RealScalar &))&Solver::template rankUpdate, bp::args("self","vector","sigma")) bp::args("self","vector","sigma")) #endif #endif ... @@ -72,8 +73,10 @@ namespace eigenpy ... @@ -72,8 +73,10 @@ namespace eigenpy #endif #endif .def("reconstructedMatrix",&Solver::reconstructedMatrix,bp::arg("self"), .def("reconstructedMatrix",&Solver::reconstructedMatrix,bp::arg("self"), "Returns the matrix represented by the decomposition, i.e., it returns the product: L L^*. This function is provided for debug purpose.") "Returns the matrix represented by the decomposition, i.e., it returns the product: L L^*. This function is provided for debug purpose.") .def("solve",&solve,bp::args("self","b"), .def("solve",&solve,bp::args("self","b"), "Returns the solution x of A x = b using the current decomposition of A.") "Returns the solution x of A x = b using the current decomposition of A.") .def("solve",&solve,bp::args("self","B"), "Returns the solution X of A X = B using the current decomposition of A where B is a right hand side matrix.") ; ; } } ... @@ -99,8 +102,8 @@ namespace eigenpy ... @@ -99,8 +102,8 @@ namespace eigenpy static MatrixType matrixL(const Solver & self) { return self.matrixL(); } static MatrixType matrixL(const Solver & self) { return self.matrixL(); } static MatrixType matrixU(const Solver & self) { return self.matrixU(); } static MatrixType matrixU(const Solver & self) { return self.matrixU(); } template template static VectorType solve(const Solver & self, const VectorType & vec) static MatrixOrVector solve(const Solver & self, const MatrixOrVector & vec) { { return self.solve(vec); return self.solve(vec); } } ... ...
 import eigenpy import eigenpy eigenpy.switchToNumpyArray() import numpy as np import numpy as np import numpy.linalg as la import numpy.linalg as la ... @@ -16,3 +15,9 @@ D = ldlt.vectorD() ... @@ -16,3 +15,9 @@ D = ldlt.vectorD() P = ldlt.transpositionsP() P = ldlt.transpositionsP() assert eigenpy.is_approx(np.transpose(P).dot(L.dot(np.diag(D).dot(np.transpose(L).dot(P)))),A) assert eigenpy.is_approx(np.transpose(P).dot(L.dot(np.diag(D).dot(np.transpose(L).dot(P)))),A) X = np.random.rand(dim,20) B = A.dot(X) X_est = ldlt.solve(B) assert eigenpy.is_approx(X,X_est) assert eigenpy.is_approx(A.dot(X_est),B)
 import eigenpy import eigenpy eigenpy.switchToNumpyArray() import numpy as np import numpy as np import numpy.linalg as la import numpy.linalg as la ... @@ -12,5 +11,10 @@ A = (A + A.T)*0.5 + np.diag(10. + np.random.rand(dim)) ... @@ -12,5 +11,10 @@ A = (A + A.T)*0.5 + np.diag(10. + np.random.rand(dim)) llt = eigenpy.LLT(A) llt = eigenpy.LLT(A) L = llt.matrixL() L = llt.matrixL() assert eigenpy.is_approx(L.dot(np.transpose(L)),A) assert eigenpy.is_approx(L.dot(np.transpose(L)),A) X = np.random.rand(dim,20) B = A.dot(X) X_est = llt.solve(B) assert eigenpy.is_approx(X,X_est) assert eigenpy.is_approx(A.dot(X_est),B)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!