用Lapack';s dgeqrf_ 我试图用C++中的矩阵分解矩阵,用LAPACK函数求解线性方程组(AX= B)< /P>

用Lapack';s dgeqrf_ 我试图用C++中的矩阵分解矩阵,用LAPACK函数求解线性方程组(AX= B)< /P>,c++,math,visual-studio-2012,linear-algebra,lapack,C++,Math,Visual Studio 2012,Linear Algebra,Lapack,据我所知,dgeqrf计算QR分解并覆盖输入矩阵。输出显然包含L(上三角)的值,但如何获得Q 我尝试了dormqr,据说它是根据dgeqrf的输出计算Q,但结果与前一个调用中的矩阵相同 以下是我的完整代码: boost::numeric::ublas::matrix<double> in_A(4, 3); in_A(0, 0) = 1.0; in_A(0, 1) = 2.0; in_A(0, 2) = 3.0; in_A(1, 1) = -3.0; in_A(1, 2) = 2.0

据我所知,dgeqrf计算QR分解并覆盖输入矩阵。输出显然包含L(上三角)的值,但如何获得Q

我尝试了dormqr,据说它是根据dgeqrf的输出计算Q,但结果与前一个调用中的矩阵相同

以下是我的完整代码:

boost::numeric::ublas::matrix<double> in_A(4, 3);
in_A(0, 0) = 1.0;
in_A(0, 1) = 2.0;
in_A(0, 2) = 3.0;

in_A(1, 1) = -3.0;
in_A(1, 2) = 2.0;
in_A(1, 3) = 1.0;

in_A(2, 1) = 2.0;
in_A(2, 2) = 0.0;
in_A(2, 3) = -1.0;

in_A(3, 1) = 3.0;
in_A(3, 2) = -1.0;
in_A(3, 3) = 2.0;

boost::numeric::ublas::vector<double> in_b(4);
in_b(0) = 2;
in_b(1) = 4;
in_b(2) = 6;
in_b(3) = 8;

int rows = in_A.size1();
int cols = in_A.size2();
double *A = (double *)malloc(rows*cols*sizeof(double));
double *b = (double *)malloc(in_b.size()*sizeof(double));

//Lapack has column-major order
for(size_t col=0; col<in_A.size2(); ++col)
{
    for(size_t row = 0; row<in_A.size1(); ++row)
{
    int D1_idx = col*in_A.size1() + row;
    A[D1_idx] = in_A(row, col);
}
b[col] = in_b(col);
}

integer m = rows;
integer n = cols;

integer info = 0;
integer k = n;          /* k = min(m,n);       */
integer lda = m;        /* lda = max(m,1);     */
integer lwork = n;      /* lwork = max(n,1);   */
int max = lwork;    /* max = max(lwork,1); */

double *work;
double *tau;

char *side = "L";
char *TR    = "T";
integer one = 1;
int i;

double *vec;

work = (double *) malloc( max * sizeof( double ) );
tau  = (double *) malloc( k * sizeof( double ) );
vec  = (double *) malloc( m * sizeof( double ) );

memset(work, 0, max * sizeof(double));
memset(tau, 0, k * sizeof(double));
std::cout << std::endl;
for(size_t row = 0; row < rows; ++row)
{
for(size_t col = 0; col < cols; ++col)
{
size_t idx = col*rows + row;
std::cout << A[idx] << " ";
}
std::cout << std::endl;
}
dgeqrf_(&m, &n, A, &lda, tau, work, &lwork, &info);
//printf("tau[0] = %f tau[1] = %f\n",tau[0],tau[1]);

std::cout << std::endl;
for(size_t row = 0; row < rows; ++row)
{
  for(size_t col = 0; col < cols; ++col)
  {
  size_t idx = col*rows + row;
  std::cout << A[idx] << " ";
  }
std::cout << std::endl;
}

memset(vec, 0, m * sizeof(double));
vec[2] = 1.0;

dormqr_(side, TR, &m, &one, &k, A, &lda, tau, vec, &lda, work, &lwork, &info);

free(vec);
free(tau);
free(work);
boost::numeric::ublas::矩阵(4,3);
in_A(0,0)=1.0;
in_A(0,1)=2.0;
in_A(0,2)=3.0;
in_(1,1)=-3.0;
in_A(1,2)=2.0;
in_A(1,3)=1.0;
in_A(2,1)=2.0;
in_A(2,2)=0.0;
in_(2,3)=-1.0;
in_A(3,1)=3.0;
in_(3,2)=-1.0;
in_A(3,3)=2.0;
boost::numeric::ublas::向量in_b(4);
in_b(0)=2;
in_b(1)=4;
in_b(2)=6;
in_b(3)=8;
int rows=in_A.size1();
int cols=in_A.size2();
double*A=(double*)malloc(rows*cols*sizeof(double));
double*b=(double*)malloc(in_b.size()*sizeof(double));
//拉帕克有一列大订单

对于(根据中的文档,大小=0;颜色)

()

在vec中计算乘积Q^T*e3,其中e3是第三个标准基向量(0,0,1,0,0,…,0)。如果要计算Q,则vec应包含一个填充有单位矩阵的矩阵大小的数组,且TRANS应为“N”


  • SIDE=“L”对于Q为左的正常QR分解

  • TRANS=“N”返回QC以代替C

  • A在内存中具有布局LDA x K,其中使用上部M x K块并对K个反射器进行编码

  • tau包含K反射器的系数

  • C在内存中具有布局LDC x M,其中上部的M x N块将用于保存结果QC

  • 为了使C在返回时保持Q,C必须是一个初始化为恒等式的平方M x M矩阵,即对角线项都为1


您可以考虑使用UBLAS提供的LAPACK数字绑定,如

()

然而,这个项目现在可能已经停止或停止


让我们重新从第一原理开始:目的是求解Ax=b,或者至少最小化| Ax-b |+| x |。为了保持一致,需要
colsA=rowsx
rowsA=rowsb


现在,要使讨论的代码正常工作,
A
必须是正方形或高矩形矩阵,
colsA虽然这是一个老问题,但如果您正在寻找一种使用QR和LAPACK来解决LLS的方法,它与上面的答案相同。

谢谢,我修改了TRANS并将vec更改为这个vec=(double)malloc(m*n*sizeof)(double));for(size_t row=0;row一个< <代码> > <代码> >,你要所有的代码<>代码> q>代码>,不只是第一个。矩阵<代码> A<代码>没有改变,结果是在 VEC < /C>中计算,所以你的最后一个输出应该打印 VC++ < /C> >的组成部分。不要使用malloc和free,而要使用new和delete。您必须指出您使用的不是std::vector,而是boost::…::vector。命名空间的别名可能会有所帮助。您可以编写一个display_matrix过程以避免代码重复。在定义行和列之后,使用它们。我已设法使其工作,我将您的答案标记为解决方案n并尽快发布摘要。请检查您在a中的结构。列索引是(0,1,2)还是(1,2,3)?
dormqr (SIDE, TRANS, M, N, K, A, LDA, TAU, C, LDC, WORK, LWORK, INFO)
#include "boost/numeric/ublas/matrix.hpp"
#include "boost/numeric/ublas/vector.hpp"

typedef boost::numeric::ublas::matrix<double> bmatrix;
typedef boost::numeric::ublas::vector<double> bvector;


namespace lapack {  


    extern "C" {
        void dgeqrf_(int* M, int* N, 
                    double* A, int* LDA, double* TAU, 
                    double* WORK, int* LWORK, int* INFO );

        void dormqr_(char*  SIDE, char* TRANS, 
                    int* M, int* N, int* K, 
                    double* A, int* LDA, double* TAU, 
                    double* C, int* LDC,
                    double* WORK, int* LWORK, int* INFO );

        void dtrtrs_(char* UPLO, char* TRANS, char* DIAG, 
                    int* N, int* NRHS, 
                    double* A, int* LDA, 
                    double* B, int* LDB, 
                    int* INFO );
    }

    int geqrf(int m, int n, 
              double* A, int lda, double *tau) {
        int info=0;
        int lwork=-1;
        double iwork;
        dgeqrf_(&m, &n, A, &lda, tau, 
                        &iwork, &lwork, &info);
        lwork = (int)iwork;
        double* work = new double[lwork];
        dgeqrf_(&m, &n, A, &lda, tau, 
                        work, &lwork, &info);
        delete[] work;
        return info;
    }

    int ormqr(char side, char trans, int m, int n, int k, 
              double *A, int lda, double *tau, double* C, int ldc) {
        int info=0;
        int lwork=-1;
        double iwork;
        dormqr_(&side, &trans, &m, &n, &k, 
                A, &lda, tau, C, &ldc, &iwork, &lwork, &info);
        lwork = (int)iwork;
        double* work = new double[lwork];
        dormqr_(&side, &trans, &m, &n, &k, 
                A, &lda, tau, C, &ldc, work, &lwork, &info);
        delete[] work;
        return info;
    }

    int trtrs(char uplo, char trans, char diag, 
              int n, int nrhs, 
              double* A, int lda, double* B, int ldb
    ) {
        int info = 0;
        dtrtrs_(&uplo, &trans, &diag, &n, &nrhs, 
                A, &lda, B, &ldb, &info);
        return info;
    }

}

static void PrintMatrix(double A[], size_t  rows, size_t  cols) {
    std::cout << std::endl;
    for(size_t row = 0; row < rows; ++row)
    {
        for(size_t col = 0; col < cols; ++col)
        {
            // Lapack uses column major format
            size_t idx = col*rows + row;
            std::cout << A[idx] << " ";
        }
        std::cout << std::endl;
    }
}

static int SolveQR(
    const bmatrix &in_A, // IN
    const bvector &in_b, // IN
    bvector &out_x // OUT
) {


    size_t  rows = in_A.size1();
    size_t  cols = in_A.size2();

    double *A = new double[rows*cols];
    double *b = new double[in_b.size()];

    //Lapack has column-major order
    for(size_t col=0, D1_idx=0; col<cols; ++col)
    {
        for(size_t row = 0; row<rows; ++row)
        {
            // Lapack uses column major format
            A[D1_idx++] = in_A(row, col);
        }
        b[col] = in_b(col);
    }

    for(size_t row = 0; row<rows; ++row)
    {
        b[row] = in_b(row);
    }

    // DGEQRF for Q*R=A, i.e., A and tau hold R and Householder reflectors


    double* tau = new double[cols];

    PrintMatrix(A, rows, cols);

    lapack::geqrf(rows, cols, A, rows, tau);

    PrintMatrix(A, rows, cols);

    // DORMQR: to compute b := Q^T*b

    lapack::ormqr('L', 'T', rows, 1, cols, A, rows, tau, b, rows);


    PrintMatrix(b, rows, 1);

    // DTRTRS: solve Rx=b by back substitution

    lapack::trtrs('U', 'N', 'N', cols, 1, A, rows, b, rows);

    for(size_t col=0; col<cols; col++) {
        out_x(col)=b[col];
    }

    PrintMatrix(b,cols,1);

    delete[] A;
    delete[] b;
    delete[] tau;

    return 0;
}


int main() {
    bmatrix in_A(4, 3);
    in_A(0, 0) =  1.0; in_A(0, 1) =  2.0; in_A(0, 2) =  3.0;
    in_A(1, 0) = -3.0; in_A(1, 1) =  2.0; in_A(1, 2) =  1.0;
    in_A(2, 0) =  2.0; in_A(2, 1) =  0.0; in_A(2, 2) = -1.0;
    in_A(3, 0) =  3.0; in_A(3, 1) = -1.0; in_A(3, 2) =  2.0;

    bvector in_b(4);
    in_b(0) = 2;
    in_b(1) = 4;
    in_b(2) = 6;
    in_b(3) = 8;

    bvector out_x(3);

    SolveQR( in_A,  in_b,  out_x);

    return 0;
}