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
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 mat37(3,7);
mat37 = MatrixXd::Random(3, 7);
CompleteOrthogonalDecomposition > 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';