I'm getting unexpected and invalid results from a linear solve computation in Eigen. I have a vector x
and matrix P
.
In MATLAB notation, I'm doing this:
x'/P*x
(similar to x'*inv(P)*x
, but no direct inversion)
which is quadratic form and should yield a positive result. The matrix P
is positive definite and not ill conditioned. In MATLAB the result is positive though large.
In C++ Eigen, my slash operator is like this:
x/P
is implemented as:
(P.transpose()).ColPivHouseholderQr().solve(x.transpose).transpose()
This gives the correct answer in general, but appears to be more fragile than MATLAB. In the case I'm looking at now, it gives a very large negative number for x'/P*x
as though there was overflow and wraparound or somesuch.
Is there a way to defragilize this?
EDIT: Doing some experimentation I see that Eigen also fails to invert this matrix, where MATLAB has no problem. The matrix condition number is 3e7, which is not bad. Eigen gives the same (wrong) answer with the linear solve and a simple x.transpose() * P.inverse() * x
. What's going on?
ColPivHouseholderQr()
to solve this? – Cris Luengox.transpose() * P.colPivHouseholderQr().solve(x)
?? Since it's doing column-pivoting, working onP
orP'
might be quite different. Nonetheless, with3e7
as condition number even an explicit computation ofP.ibverse()
should produce a correct result. Have you check the condition number on the C++ side? ( e.g.,BDVSVD<MatrixXd> svd(P); cout << P.singularValues()(0)/P.singularValues()(P.cols()-1) << "\n";
) – ggael