我想以Ax = b
线性最小二乘方式求解线性系统,从而获得x
. 矩阵A
,x
并b
包含复数元素。
矩阵A
的维数为n
,n
并且A
是一个也是下三角形的方阵。向量b
和x
的长度为n
。这个系统中的未知数与方程的数量一样多,但由于b
是一个充满实际测量“数据”的向量,我怀疑最好以线性最小二乘法的方式执行此操作。
我正在寻找一种能够以 LLS 方式有效解决该系统的算法,可能使用稀疏矩阵数据结构来处理下三角矩阵A
。
也许已经有一个具有这种算法的 C/C++ 库?(由于代码优化,我怀疑最好使用库。)环顾 Eigen 矩阵库,似乎 SVD 分解可用于以 LLS 方式求解方程组(指向 Eigen 文档的链接)。但是,如何在 Eigen 中处理复数?
似乎 Eigen 库与 SVD 一起使用,然后将其用于 LLS 求解。
这是一个代码片段,演示了我想做的事情:
#include <iostream>
#include <Eigen/Dense>
#include <complex>
using namespace Eigen;
int main()
{
// I would like to assign complex numbers
// to A and b
/*
MatrixXcd A(4, 4);
A(0,0) = std::complex(3,5); // Compiler error occurs here
A(1,0) = std::complex(4,4);
A(1,1) = std::complex(5,3);
A(2,0) = std::complex(2,2);
A(2,1) = std::complex(3,3);
A(2,2) = std::complex(4,4);
A(3,0) = std::complex(5,3);
A(3,1) = std::complex(2,4);
A(3,2) = std::complex(4,3);
A(3,3) = std::complex(2,4);
*/
// The following code is taken from:
// http://eigen.tuxfamily.org/dox/TutorialLinearAlgebra.html#TutorialLinAlgLeastsquares
// This is what I want to do, but with complex numbers
// and with A as lower triangular
MatrixXf A = MatrixXf::Random(3, 3);
std::cout << "Here is the matrix A:\n" << A << std::endl;
VectorXf b = VectorXf::Random(3);
std::cout << "Here is the right hand side b:\n" << b << std::endl;
std::cout << "The least-squares solution is:\n"
<< A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b) << std::endl;
}// end
这是编译器错误:
error: missing template arguments before '(' token
更新
这是一个更新的程序,展示了如何使用 Eigen 处理 LLS 求解。这段代码确实可以正确编译。
#include <iostream>
#include <Eigen/Dense>
#include <complex>
using namespace Eigen;
int main()
{
MatrixXcd A(4, 4);
A(0,0) = std::complex<double>(3,5);
A(1,0) = std::complex<double>(4,4);
A(1,1) = std::complex<double>(5,3);
A(2,0) = std::complex<double>(2,2);
A(2,1) = std::complex<double>(3,3);
A(2,2) = std::complex<double>(4,4);
A(3,0) = std::complex<double>(5,3);
A(3,1) = std::complex<double>(2,4);
A(3,2) = std::complex<double>(4,3);
A(3,3) = std::complex<double>(2,4);
VectorXcd b(4);
b(0) = std::complex<double>(3,5);
b(1) = std::complex<double>(2,0);
b(2) = std::complex<double>(8,2);
b(3) = std::complex<double>(4,8);
std::cout << "Here is the A matrix:" << std::endl;
std::cout << A << std::endl;
std::cout << "Here is the b vector:" << std::endl;
std::cout << b << std::endl;
std::cout << "The least-squares solution is:\n"
<< A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b) << std::endl;
}// end