我想用线性最小二乘的方式求解线性系统Ax = b,从而得到x。矩阵A、x和b包含复数元素。
矩阵A具有n表示的n维数,A是下三角矩阵。载体b和x的长度为n。这个系统中的未知数和方程一样多,但是由于b是一个包含实际测量“数据”的向量,我怀疑最好用线性最小二乘的方式来实现。
我正在寻找一个算法,将有效地解决这个系统的LLS方式,也许使用稀疏矩阵数据结构的下三角矩阵A。
也许有一个C/C++库已经有这样的算法可用了?(我怀疑,由于优化的代码,最好使用库。)从本征矩阵库中可以看出,奇异值分解可以用最小二乘方法(链接到特征文档)求解方程组。然而,如何处理本征中的复数呢?
看来,本征库与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更新
这是一个更新的程序,显示如何处理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发布于 2012-12-06 16:07:45
因为std::complex是一个模板类,您可以在std::complex(1,1);中加入它,所以编译器不知道它是什么类型。
使用std::complex<double>(1, 1);代替。
https://stackoverflow.com/questions/13747290
复制相似问题