在优化求解问题中,经常要用到矩阵奇异值的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=b186A[5000, 40]X=b702A[5000, 60]X=b1573A[5000, 80]X=b2805
害怕攀登高峰的人,永远在山下徘徊。