我在c ++中使用eigen3包来做一些线性代数,但是包含一些矩阵 - 矩阵和矩阵 - 向量乘法的代码的一部分需要太长时间。我的矩阵和向量相当大(订购20kx20k),但有些是稀疏的。我从本征文档中读到的内容,它旨在与稀疏矩阵一起高效工作。我不知道我做错了什么,或者我怎么能改进它。非常感谢任何帮助 这是代码的一部分;我们有n个输入数据,我们从函数中计算'k',对于每个点,我们需要找到代码中定义的'mean'值:
#pragma omp parallel for ordered schedule(dynamic)
for (unsigned long n = 0; n < nNew; n++) {
SparseVector<double> kJ(totalJ);
double k = something; #calculates using a function
for(int i=0; i<totalJ; i++) {
double covTmp = xxx; #calculates using a function
kJ.insert(i) = covTmp;
}
SparseVector<double> CJikJ(totalJ);
CJikJ = CJi * kJ;
double kJTCJikJ = kJ.transpose().dot(CJikJ);
double mu = 1. / (k - kJTCJikJ);
SparseVector<double> mJ(totalJ);
mJ= -mu * CJikJ;
SparseMatrix<double> MJi(totalJ, totalJ);
MJi = CJ - kJ*kJ.transpose()*mu / (1. + mu * kJTCJikJ);
SparseMatrix<double> VNGJMJiGJTi(nstars, nstars);
VNGJMJiGJTi = invertMatrix(VN + GJ * (MJi * GJT), nstars);
SparseMatrix<double> RJi(totalJ, totalJ);
RJi = MJi - MJi * GJT * (VNGJMJiGJTi) * (GJ * MJi); ## this line takes too long
RJi.prune(prunelim);
SparseVector<double> RJimJ;
RJimJ = RJi*mJ;
double alpha = mu - mJ.dot(RJimJ);
double beta = AN.dot((VNi * GJ) * RJimJ);
double mean = -beta / alpha;
outfile << setprecision(8) << newposm[n][0] << ", " << newposm[n][1] << ", " << newposm[n][2] << ", " << alpha << ", " << beta << ", " << mean << ", " << variance << "\n";
if(params.vb) {
cout << setprecision(8) << "# l, b, dist, alpha, beta, mean, var" << endl;
cout << setprecision(8) << newposm[n][0] << ", " << newposm[n][1] << ", " << newposm[n][2] << ", " << alpha << ", " << beta << ", " << mean << ", " << variance << "\n";
}
}