Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate Cholesky Decomposition using Eigen

I'm trying to calculate the Cholesky factor of a matrix in C++ (for a given matrix P find L such that LL^T=P). My objective is NOT to solve a linear system P*x=b, as such matrix decompositions are often used for, but to actually obtain the matrix L. (I'm trying to calculate "sigma points" as is done in the unscented transform.)

The library Eigen supposedly calculates Cholesky decompositions, but I can't figure out how to get it to give me the values in matrix L. When I attempt the following lines of code

Eigen::MatrixXd P(3,3);
P << 6, 0, 0, 0, 4, 0, 0, 0, 7;
std::cout << P.llt().matrixL().col(0) << std::endl;

I get compiler error

error: ‘Eigen::internal::LLT_Traits<Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>, 1>::MatrixL’ has no member named ‘col’

The documentation says that LLT.matrixL() returns type Traits::MatrixL. What is that and how do I get the values of L?

like image 715
Clark Avatar asked Oct 19 '12 06:10

Clark


1 Answers

You can look up what the Trait is in the LLT.h header file. Its a TriangularView like the documentation says. The triangular view does not have a col member, so that is why you get the error. Copying the triangular view into a dense matrix like so:

Eigen::MatrixXd P(3,3);
P << 6, 0, 0, 0, 4, 0, 0, 0, 7;
Eigen::MatrixXd L( P.llt().matrixL() );
std::cout << L.col(0) << std::endl;

will get you what you want.

like image 184
Jakob Avatar answered Oct 05 '22 00:10

Jakob