C++ eigenvalue/vector decomposition, only need first n vectors fast

前端 未结 2 595
执念已碎
执念已碎 2021-02-05 07:16

I have a ~3000x3000 covariance-alike matrix on which I compute the eigenvalue-eigenvector decomposition (it\'s a OpenCV matrix, and I use cv::eigen() to get the job

2条回答
  •  太阳男子
    2021-02-05 07:50

    It seems that Spectra will do the job with good performances.

    Here is an example from their documentation to compute the 3 first eigen values of a dense symmetric matrix M (likewise your covariance matrix):

    #include 
    #include 
    //  is implicitly included
    #include 
    using namespace Spectra;
    int main()
    {
        // We are going to calculate the eigenvalues of M
        Eigen::MatrixXd A = Eigen::MatrixXd::Random(10, 10);
        Eigen::MatrixXd M = A + A.transpose();
        // Construct matrix operation object using the wrapper class DenseSymMatProd
        DenseSymMatProd op(M);
        // Construct eigen solver object, requesting the largest three eigenvalues
        SymEigsSolver< double, LARGEST_ALGE, DenseSymMatProd > eigs(&op, 3, 6);
        // Initialize and compute
        eigs.init();
        int nconv = eigs.compute();
        // Retrieve results
        Eigen::VectorXd evalues;
        if(eigs.info() == SUCCESSFUL)
            evalues = eigs.eigenvalues();
        std::cout << "Eigenvalues found:\n" << evalues << std::endl;
        return 0;
    }
    

提交回复
热议问题