-
-
Save gokhansolak/d2abaefcf3e3b767f5bc7d81cfe0b36a to your computer and use it in GitHub Desktop.
Moore-Penrose Pseudo-Inverse Using Eigen, works for non-square matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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::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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment