In addition to @vsoftco's answer, we will also check the symmetry of the matrix, since a symmetric matrix is required to determine PD / PSD.
Eigen::LLT<Eigen::MatrixXd> A_llt(A); if (!A.isApprox(A.transpose()) || A_llt.info() == Eigen::NumericalIssue) { throw std::runtime_error("Possibly non semi-positive definitie matrix!"); }
This check is important, for example, for some proprietary solvers (such as LTDT ) a matrix input PSD (or NSD) is required. In fact, there is an asymmetric matrix A which passes A_llt.info() != Eigen::NumericalIssue . Consider the following example (figures taken from Jiuzhan Suanshu , chapter 8, problem 1):
Eigen::Matrix3d A; Eigen::Vector3d b; Eigen::Vector3d x;
Notes: more precisely, the definition of PSD can be applied by checking that A symmetric and all eigenvalues A> = 0. But, as mentioned in this question, this can be computationally expensive.
source share