我有一个~3000x3000协方差相似的矩阵,我在其上计算特征值 - 特征向量分解(它是一个OpenCV矩阵,我使用cv::eigen()
来完成工作)。
然而,我实际上只需要前30个特征值/向量,我不关心其余的。从理论上讲,这应该可以显着加快计算速度,对吧?我的意思是,这意味着它有2970个需要计算的特征向量。
哪个C ++库允许我这样做?请注意,OpenCV的eigen()
方法确实有参数,但是文档说它们被忽略了,我自己测试了,它们确实被忽略了:D
的更新: 的 我设法用ARPACK做到了。我设法为Windows编译它,甚至使用它。结果看起来很有希望,在这个玩具示例中可以看到一个例子:
#include "ardsmat.h"
#include "ardssym.h"
int n = 3; // Dimension of the problem.
double* EigVal = NULL; // Eigenvalues.
double* EigVec = NULL; // Eigenvectors stored sequentially.
int lowerHalfElementCount = (n*n+n) / 2;
//whole matrix:
/*
2 3 8
3 9 -7
8 -7 19
*/
double* lower = new double[lowerHalfElementCount]; //lower half of the matrix
//to be filled with COLUMN major (i.e. one column after the other, always starting from the diagonal element)
lower[0] = 2; lower[1] = 3; lower[2] = 8; lower[3] = 9; lower[4] = -7; lower[5] = 19;
//params: dimensions (i.e. width/height), array with values of the lower or upper half (sequentially, row major), 'L' or 'U' for upper or lower
ARdsSymMatrix<double> mat(n, lower, 'L');
// Defining the eigenvalue problem.
int noOfEigVecValues = 2;
//int maxIterations = 50000000;
//ARluSymStdEig<double> dprob(noOfEigVecValues, mat, "LM", 0, 0.5, maxIterations);
ARluSymStdEig<double> dprob(noOfEigVecValues, mat);
// Finding eigenvalues and eigenvectors.
int converged = dprob.EigenValVectors(EigVec, EigVal);
for (int eigValIdx = 0; eigValIdx < noOfEigVecValues; eigValIdx++) {
std::cout << "Eigenvalue: " << EigVal[eigValIdx] << "\nEigenvector: ";
for (int i = 0; i < n; i++) {
int idx = n*eigValIdx+i;
std::cout << EigVec[idx] << " ";
}
std::cout << std::endl;
}
结果是:
9.4298, 24.24059
表示特征值,
-0.523207, -0.83446237, -0.17299346
0.273269, -0.356554, 0.893416
分别为2个特征向量(每行一个特征向量) 代码无法找到3个特征向量(在这种情况下它只能找到1-2,断言()确保这一点,但是,这不是问题)。
答案 0 :(得分:0)
似乎Spectra会表现出色。
这是他们的文档中的一个示例,用于计算密集对称矩阵M(同样是协方差矩阵)的3个第一特征值:
#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;
}