c++ - Eigen - Check if matrix is Positive (Semi-)Definite -
i'm implementing spectral clustering algorithm , have ensure matrix (laplacian) positive semi-definite.
a check if matrix positive definite (pd) enough, since "semi-" part can seen in eigenvalues. matrix pretty big (nxn n in order of thousands) eigenanalysis expensive.
is there check in eigen gives bool result in runtime?
matlab can give result chol()
method throwing exception if matrix not pd. following idea, eigen returns result without complaining lll.llt().matrixl()
, although expecting warning/error. eigen has method ispositive
, due bug unusable systems old eigen version.
you can use cholesky decomposition (llt), returns eigen::numericalissue
if matrix negative, see documentation.
example below:
#include <eigen/dense> #include <iostream> #include <stdexcept> int main() { eigen::matrixxd a(2, 2); << 1, 0 , 0, -1; // non semi-positive definitie matrix std::cout << "the matrix is" << std::endl << << std::endl; eigen::llt<eigen::matrixxd> lltofa(a); // compute cholesky decomposition of if(lltofa.info() == eigen::numericalissue) { throw std::runtime_error("possibly non semi-positive definitie matrix!"); } }
Comments
Post a Comment