以复杂元素和下三角形方形A矩阵的线性最小二乘方式求解系统Ax = b

时间:2012-12-06 15:47:11

标签: c++ linear-algebra eigen

我想以线性最小二乘方式求解线性系统Ax = b,从而获得x。矩阵Axb包含复数的元素。

矩阵A的尺寸为n nA是一个方形矩阵,也是下三角形。向量bx的长度为n。在这个系统中存在与方程一样多的未知数,但由于b是一个充满实际测量“数据”的向量,我怀疑以线性最小二乘方式做这个更好。

我正在寻找一种能够以LLS方式有效地解决该系统的算法,可能使用稀疏矩阵数据结构来处理低三角矩阵A

也许有一个C / C ++库已经有这样的算法? (我怀疑由于优化的代码,最好使用库。)在Eigen矩阵库中查看,似乎SVD分解可用于以LLS方式求解方程组(link to Eigen documentation) 。但是,如何使用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

1 个答案:

答案 0 :(得分:2)

由于std::complex是模板类,并且使用std::complex(1,1);初始化,编译器不知道它是什么类型。

请改用std::complex<double>(1, 1);