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

前端 未结 2 596
执念已碎
执念已碎 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:44

    In this article, Simon Funk shows a simple, effective way to estimate a singular value decomposition (SVD) of a very large matrix. In his case, the matrix is sparse, with dimensions: 17,000 x 500,000.

    Now, looking here, describes how eigenvalue decomposition closely related to SVD. Thus, you might benefit from considering a modified version of Simon Funk's approach, especially if your matrix is sparse. Furthermore, your matrix is not only square but also symmetric (if that is what you mean by covariance-like), which likely leads to additional simplification.

    ... Just an idea :)

    0 讨论(0)
  • 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 <Eigen/Core>
    #include <Spectra/SymEigsSolver.h>
    // <Spectra/MatOp/DenseSymMatProd.h> is implicitly included
    #include <iostream>
    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<double> op(M);
        // Construct eigen solver object, requesting the largest three eigenvalues
        SymEigsSolver< double, LARGEST_ALGE, DenseSymMatProd<double> > 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;
    }
    
    0 讨论(0)
提交回复
热议问题