Skip to content

Instantly share code, notes, and snippets.

@pshriwise
Last active July 3, 2023 06:12
Show Gist options
  • Save pshriwise/67c2ae78e5db3831da38390a8b2a209f to your computer and use it in GitHub Desktop.
Save pshriwise/67c2ae78e5db3831da38390a8b2a209f to your computer and use it in GitHub Desktop.
Moore-Penrose Pseudo-Inverse Using Eigen
// method for calculating the pseudo-Inverse as recommended by Eigen developers
template<typename _Matrix_Type_>
_Matrix_Type_ pseudoInverse(const _Matrix_Type_ &a, double epsilon = std::numeric_limits<double>::epsilon())
{
Eigen::JacobiSVD< _Matrix_Type_ > svd(a ,Eigen::ComputeFullU | Eigen::ComputeFullV);
// For a non-square matrix
// Eigen::JacobiSVD< _Matrix_Type_ > svd(a ,Eigen::ComputeThinU | Eigen::ComputeThinV);
double tolerance = epsilon * std::max(a.cols(), a.rows()) *svd.singularValues().array().abs()(0);
return svd.matrixV() * (svd.singularValues().array().abs() > tolerance).select(svd.singularValues().array().inverse(), 0).matrix().asDiagonal() * svd.matrixU().adjoint();
}
@pshriwise
Copy link
Author

Hi @muttistefano! As @gokhansolak noted above, this implementation doesn't seem to work for a non-square matrix. Perhaps you could give the version linked above a try?

@muttistefano
Copy link

Hi @pshriwise , i tried both versions and they both have problems.
I actually need to use Eigen::ComputeFullU | Eigen::ComputeFullV cause I need the full matrices of the svd.
If it's useful, I can post here a working version, based on the Eigen::BDCSVD class.
If I will have time I will dig more into this.
For now, I will stick to the working version.
I post it here cause if you look up for eigen pseudoinverse you might end up here.
Thanks

template<typename MatType>
PseudoInverseType<MatType> pseudoInverse(const MatType &a, double epsilon = std::numeric_limits<double>::epsilon())
{
	using WorkingMatType = Eigen::Matrix<typename MatType::Scalar, Eigen::Dynamic, Eigen::Dynamic, 0,
																			 MatType::MaxRowsAtCompileTime, MatType::MaxColsAtCompileTime>;
  Eigen::BDCSVD<WorkingMatType> svd(a, Eigen::ComputeFullU | Eigen::ComputeFullV);
	svd.setThreshold(epsilon*std::max(a.cols(), a.rows()));
	Eigen::Index rank = svd.rank();
	Eigen::Matrix<typename MatType::Scalar, Eigen::Dynamic, MatType::RowsAtCompileTime,
								0, Eigen::BDCSVD<WorkingMatType>::MaxDiagSizeAtCompileTime, MatType::MaxRowsAtCompileTime>
		tmp = svd.matrixU().leftCols(rank).adjoint();
	tmp = svd.singularValues().head(rank).asDiagonal().inverse() * tmp;
	return svd.matrixV().leftCols(rank) * tmp;
}

@pshriwise
Copy link
Author

Thanks for posting this @muttistefano! If I have time I'll try this version in the application where I applied the inverse in this gist to ensure it also has the behavior I'd expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment