求伪逆矩阵c++代码(Eigen库)

非方阵的矩阵的逆矩阵  pseudoInverse

伪逆矩阵是逆矩阵的广义形式,广义逆矩阵

matlab中是pinv(A)--》inv(A)。

#include "stdafx.h"
#include<iostream> 
#include<Eigen/Core> 
#include<Eigen/SVD>   


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(); 
}   
int main() 
{  
    Eigen::MatrixXd A(2,3);  
    A<< 1, 2, 3, 4, 5, 7;  
    std::cout<<A<<std::endl<<std::endl;  
    std::cout<<pseudoInverse(A)<<std::endl;   
    Eigen::MatrixXd B(3,2);  
    B<< 1, 2, 3, 4, 5, 7;  
    std::cout<<B<<std::endl<<std::endl;  
    std::cout<<pseudoInverse(B)<<std::endl;  
    getchar();
    return 0; 
}

 

矩阵的伪逆(pseudoinverse) - 冯瑾亓 - 博客园 https://www.cnblogs.com/Rambler1995/p/5583645.html

矩阵伪逆介绍及C++/OpenCV/Eigen的三种实现,不能直接运行 - CSDN博客 https://blog.csdn.net/fengbingchun/article/details/72874623

上面那个的开源代码GitHub - fengbingchun/Eigen_Test: Eigen's usage https://github.com/fengbingchun/Eigen_Test

posted @ 2018-04-20 18:08  ostartech  阅读(4307)  评论(0编辑  收藏  举报