2014-04-26 81 views
2

我使用Eigen 3的Cholesky模块来求解线性方程组。 Eigen文档指出,使用LDLT而不是LLT可能会更快,但我的基准测试结果显示出不同的结果。Eigen LDLT比LLT慢吗?

我使用下面的代码为标杆:

#include <iostream> 
#include <chrono> 
#include <Eigen/Core> 
#include <Eigen/Cholesky> 
using namespace std; 
using namespace std::chrono; 
using namespace Eigen; 

int main() 
{ 
    MatrixXf cov = MatrixXf::Random(4200, 4200); 
    cov = (cov + cov.transpose()) + 1000 * MatrixXf::Identity(4200, 4200); 
    VectorXf b = VectorXf::Random(4200), r1, r2; 

    r1 = b; 
    LLT<MatrixXf> llt; 
    auto start = high_resolution_clock::now(); 
    llt.compute(cov); 
    if (llt.info() != Success) 
    { 
     cout << "Error on LLT!" << endl; 
     return 1; 
    } 
    auto middle = high_resolution_clock::now(); 
    llt.solveInPlace(r1); 
    auto stop = high_resolution_clock::now(); 
    cout << "LLT decomposition & solving in " << duration_cast<milliseconds>(middle - start).count() 
     << " + " << duration_cast<milliseconds>(stop - middle).count() << " ms." << endl; 

    r2 = b; 
    LDLT<MatrixXf> ldlt; 
    start = high_resolution_clock::now(); 
    ldlt.compute(cov); 
    if (ldlt.info() != Success) 
    { 
     cout << "Error on LDLT!" << endl; 
     return 1; 
    } 
    middle = high_resolution_clock::now(); 
    ldlt.solveInPlace(r2); 
    stop = high_resolution_clock::now(); 
    cout << "LDLT decomposition & solving in " << duration_cast<milliseconds>(stop - start).count() 
     << " + " << duration_cast<milliseconds>(stop - middle).count() << " ms." << endl; 

    cout << "Total result difference: " << (r2 - r1).cwiseAbs().sum() << endl; 
    return 0; 
} 

我和g++ -std=c++11 -O2 -o llt.exe llt.cc编译它在Windows上,这就是我得到:

LLT decomposition & solving in 6515 + 15 ms. 
LDLT decomposition & solving in 8562 + 15 ms. 
Total result difference: 1.27354e-006 

那么,为什么是活体肝移植比LLT慢?我做错了什么或者我误解了文档?

+0

你需要一个正定矩阵cholesky。所以我认为错误是'cov =(cov + cov.transpose())'加法应该是一个乘法。 – typ1232

+0

@ typ1232:用'cov =(cov * cov.transpose())+ 1000 * MatrixXf :: Identity(4200,4200);'试过了,但是这并没有改变测量的性能。 – Callidior

回答

4

此文档的句子已过时。对于最近版本的Eigen,因为LLT实现利用缓存友好矩阵操作,而LDLT实现仅涉及旋转和矩阵向量操作,所以对于相当大的矩阵,LLT应该比LDLT快得多。随着devel分支你的例子给我:

LLT decomposition & solving in 380 + 4 ms. 
LDLT decomposition & solving in 2746 + 4 ms.