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慢?我做錯了什麼或者我誤解了文檔?
你需要一個正定矩陣cholesky。所以我認爲錯誤是'cov =(cov + cov.transpose())'加法應該是一個乘法。 – typ1232
@ typ1232:用'cov =(cov * cov.transpose())+ 1000 * MatrixXf :: Identity(4200,4200);'試過了,但是這並沒有改變測量的性能。 – Callidior