Ron  Tubman
Ron Tubman

Reputation: 119

C++ Eigen svd.singularValues() returns only the nonzero ones. How do I get the zero ones as well?

I'm trying to compute the nullspace of a matrix (in this case, a matrix with one row, i.e. a vector).

To do so I compute its SVD using the Eigen::JacobiSvd class (Edit: passing ComputeFullV as a parameter). It has a function, singularValues that returns a vector with the singular values of the matrix.

EDIT: I specifically want an orthonormal basis, which SVD guarantees by default while other sorts of decompositions don't necessarily do.

The problem is that it only returns the nonzero values, and I'm interested in the zero values (and which singular vectors correspond to them) to compute a nullspace. Is there a way to get the zero values as well, in order to find their corresponding columns?

I searched Eigen's documentation on the JacobiSvd class for more info on its member function but couldn't find any. Was I looking at the wrong place?

Edit 2: This is the function that I'm using:

Eigen::MatrixXd nullspace(const Eigen::MatrixXd& mat, double tol = 1e-5) {
// Perform SVD
Eigen::JacobiSVD<Eigen::MatrixXd> svd(mat, Eigen::ComputeFullU | Eigen::ComputeFullV);

// Get the singular values
Eigen::VectorXd singularValues = svd.singularValues();
// debug
cout << "size of singular values array" << singularValues.size() << endl;
cout << singularValues.transpose() << endl;

// Determine the dimension of the nullspace
int nullity = (singularValues.array() < tol).count();

// Get the matrix V from the decomposition
Eigen::MatrixXd V = svd.matrixV();

// Extract the basis vectors for the nullspace
Eigen::MatrixXd ns = V.rightCols(nullity);

// If nullity is 1, return the vector instead of a matrix
if (nullity == 1) {
    return ns.col(0); // Return the first column as a vector
}

// Return the basis vectors of the nullspace
return ns;

with the input 1x3 matrix (1, 1, 1).

Upvotes: 1

Views: 106

Answers (0)

Related Questions