Ubuntu下C++基於eigen庫SVD矩陣奇異值分解效率分析
阿新 • • 發佈:2019-02-05
在優化求解問題中,經常要用到矩陣奇異值的SVD分解。
執行結果:
奇異值分解 (singularvalue decomposition,SVD)是一種可靠地正交矩陣分解法,它比QR分解法要花上近十倍的計算時間。
使用SVD分解法的用途是解最小平方誤差法和資料壓縮。
在Ubuntu下基於eigen C++庫測試了eigen SVD演算法的效能,即SVD求解最小二乘/偽逆,程式碼如下:
//compile: g++ test_svd.cpp #include <iostream> #include <Eigen/Dense> #include <sys/time.h> using namespace std; using namespace Eigen; long getCurrentTime() { struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_sec * 1000 + tv.tv_usec / 1000; } int main() { for(int i = 1; i < 5; i++) { cout << "A[5000, " << i * 20 << "]X=b" << endl; long t1 = getCurrentTime(); MatrixXf A = MatrixXf::Random(5000, 20 * i); //cout << "A矩陣:\n" << A << endl; VectorXf b = VectorXf::Random(5000); //cout << "右側b向量:\n" << b << endl; //cout << "最小均方值為:\n" << A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b); long t2 = getCurrentTime(); cout << t2 - t1 << endl; } return 0; }
執行結果:
A[5000, 20]X=b
186
A[5000, 40]X=b
702
A[5000, 60]X=b
1573
A[5000, 80]X=b
2805