I am running the following code using eigen3:
_H.resize(_J.cols(), _J.cols());
_H.triangularView<Eigen::Lower>() = _Jiew.transpose() * _J;
with Eigen::SparseMatrix<double> _J
, but I get the following compilation error:
In file included from /home/enrico/catkin_ws/external/horizon_gui/horizon/cpp/src/sqp_test.cpp:1: /home/enrico/catkin_ws/external/horizon_gui/horizon/cpp/src/sqp.h: In instantiation of ‘const DMDict& horizon::SQPGaussNewton<CASADI_TYPE>::solve(const DM&, const DM&, const DM&, const DM&, const DM&, const DM&) [with CASADI_TYPE = casadi::Matrix<casadi::SXElem>; casadi::DMDict = std::map<std::__cxx11::basic_string<char>, casadi::Matrix<double> >; casadi::DM = casadi::Matrix<double>]’: /home/enrico/catkin_ws/external/horizon_gui/horizon/cpp/src/sqp_test.cpp:36:49: required from here /home/enrico/catkin_ws/external/horizon_gui/horizon/cpp/src/sqp.h:233:47: error: no match for ‘operator=’ (operand types are ‘const Eigen::TriangularView<const Eigen::SparseMatrix<double>, 1>’ and ‘const Eigen::Product<Eigen::Transpose<Eigen::SparseMatrix<double> >, Eigen::SparseMatrix<double>, 2>’) 233 | _H.triangularView<Eigen::Lower>() = _J.transpose() * _J; | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/eigen3/Eigen/Core:88, from /usr/include/eigen3/Eigen/Dense:1, from /home/enrico/catkin_ws/external/horizon_gui/horizon/cpp/src/wrapped_function.h:5, from /home/enrico/catkin_ws/external/horizon_gui/horizon/cpp/src/sqp.h:5, from /home/enrico/catkin_ws/external/horizon_gui/horizon/cpp/src/sqp_test.cpp:1: /usr/include/eigen3/Eigen/src/Core/TriangularMatrix.h:220:5: note: candidate: ‘Eigen::TriangularView<MatrixType, Mode>& Eigen::TriangularView<MatrixType, Mode>::operator=(const Eigen::TriangularView<MatrixType, Mode>&) [with _MatrixType = const Eigen::SparseMatrix<double>; unsigned int _Mode = 1]’ 220 | EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TriangularView) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/eigen3/Eigen/src/Core/TriangularMatrix.h:220:5: note: no known conversion for argument 1 from ‘const Eigen::Product<Eigen::Transpose<Eigen::SparseMatrix<double> >, Eigen::SparseMatrix<double>, 2>’ to ‘const Eigen::TriangularView<const Eigen::SparseMatrix<double>, 1>&’ 220 | EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TriangularView) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/eigen3/Eigen/SparseCore:60, from /usr/include/eigen3/Eigen/Sparse:26, from /home/enrico/catkin_ws/external/horizon_gui/horizon/cpp/src/wrapped_function.h:6, from /home/enrico/catkin_ws/external/horizon_gui/horizon/cpp/src/sqp.h:5, from /home/enrico/catkin_ws/external/horizon_gui/horizon/cpp/src/sqp_test.cpp:1: /usr/include/eigen3/Eigen/src/SparseCore/SparseTriangularView.h:25:56: note: candidate: ‘constexpr Eigen::TriangularViewImpl<const Eigen::SparseMatrix<double>, 1, Eigen::Sparse>& Eigen::TriangularViewImpl<const Eigen::SparseMatrix<double>, 1, Eigen::Sparse>::operator=(Eigen::TriangularViewImpl<const Eigen::SparseMatrix<double>, 1, Eigen::Sparse>&&)’ 25 | template<typename MatrixType, unsigned int Mode> class TriangularViewImpl<MatrixType,Mode,Sparse> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/eigen3/Eigen/src/SparseCore/SparseTriangularView.h:25:56: note: no known conversion for argument 1 from ‘const Eigen::Product<Eigen::Transpose<Eigen::SparseMatrix<double> >, Eigen::SparseMatrix<double>, 2>’ to ‘Eigen::TriangularViewImpl<const Eigen::SparseMatrix<double>, 1, Eigen::Sparse>&&’ /usr/include/eigen3/Eigen/src/SparseCore/SparseTriangularView.h:25:56: note: candidate: ‘constexpr Eigen::TriangularViewImpl<const Eigen::SparseMatrix<double>, 1, Eigen::Sparse>& Eigen::TriangularViewImpl<const Eigen::SparseMatrix<double>, 1, Eigen::Sparse>::operator=(const Eigen::TriangularViewImpl<const Eigen::SparseMatrix<double>, 1, Eigen::Sparse>&)’ /usr/include/eigen3/Eigen/src/SparseCore/SparseTriangularView.h:25:56: note: no known conversion for argument 1 from ‘const Eigen::Product<Eigen::Transpose<Eigen::SparseMatrix<double> >, Eigen::SparseMatrix<double>, 2>’ to ‘const Eigen::TriangularViewImpl<const Eigen::SparseMatrix<double>, 1, Eigen::Sparse>&’ make[2]: *** [CMakeFiles/sqp_test.dir/build.make:63: CMakeFiles/sqp_test.dir/src/sqp_test.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:106: CMakeFiles/sqp_test.dir/all] Error 2 make: *** [Makefile:130: all] Error 2
The thing that is not clear to me is: I copied this part of code from another source where I was using dense Eigen::MatrixXd
and it compiles without problems.
Am I doing something wrong with sparse matrices and/or there is another way to compute only a triangular part of the product J.transpose()*J
?
Thank you very much!
Here is a minimal reproducible example:
#include <Eigen/Dense>
#include <Eigen/Sparse>
int main(){
// This compile
Eigen::MatrixXd A, B;
A.resize(8,8);
A.setRandom(8,8);
B.resize(8, 8);
B.triangularView<Eigen::Upper>() = A.transpose()*A;
//This does not compile
Eigen::SparseMatrix<double> J, H;
J.resize(8, 8);
J.setIdentity();
H.resize(8, 8);
H.triangularView<Eigen::Upper>() = J.transpose()*J;
}
I added as well some "extra" code such as setRandom()
and setIdentity()
which is not really necessary for the issue.
I am using Eigen 3.3.7 on Ubuntu 20.04.
I solved using _H.selfadjointView<Eigen::Lower>().rankUpdate(_J.transpose())
.