How to compute basis of nullspace of a matrix with Eigen library?
I tried to find explicit function name to compute null basis and also
Alternative: Use OpenCV to calculate null space:
`
cv::Mat EpipolarConstraint::getNullSpace(cv::Mat p)
{
cv::SVD svd = cv::SVD(p, cv::SVD::FULL_UV);
cv::Mat vt_ = svd.vt;
int i;
for (i = 1; i <= 3; i++)
{
if (p.at<double>(i - 1, i - 1) == 0)
{
break;
}
}
cv::Mat result = vt_(cv::Rect(0, i-1, p.cols, vt_.rows-i+1));
cv::Mat result_t;
cv::transpose(result, result_t);
return result_t;
}`
You can get a basis of the null space using Eigen::FullPivLU::kernel() method:
FullPivLU<MatrixXd> lu(A);
MatrixXd A_null_space = lu.kernel();
FullPivLU is most expensive computationally in Eigen, http://eigen.tuxfamily.org/dox/group__DenseDecompositionBenchmark.html.
A quicker alternative is to use CompleteOrthogonalDecomposition. This code uses four fundamental subspaces of a matrix (google four fundamental subspaces and URV decomposition):
Matrix<double, Dynamic, Dynamic> mat37(3,7);
mat37 = MatrixXd::Random(3, 7);
CompleteOrthogonalDecomposition<Matrix<double, Dynamic, Dynamic> > cod;
cod.compute(mat37);
cout << "rank : " << cod.rank() << "\n";
// Find URV^T
MatrixXd V = cod.matrixZ().transpose();
MatrixXd Null_space = V.block(0, cod.rank(),V.rows(), V.cols() - cod.rank());
MatrixXd P = cod.colsPermutation();
Null_space = P * Null_space; // Unpermute the columns
// The Null space:
std::cout << "The null space: \n" << Null_space << "\n" ;
// Check that it is the null-space:
std::cout << "mat37 * Null_space = \n" << mat37 * Null_space << '\n';