QR decomposition in C++ by LAPACK

2.2k views Asked by At

I have recently installed a new library for matrix calculation in C++, called LAPACK. I am beginner in this field and wanted to test its application in QR decomposition by using the dgeqrf function. I prepared this simple code below:

#include <iostream>
#include <lapacke.h>

using namespace std;

int main()
{
    double a[4] = {0, 2, 2, -1};

    int m=2;
    int n=2;
    int info = 0;
    int lda = m;
    int lwork = n;
    double *work;
    double *tau;

    dgeqrfp_(&m, &n, a, &lda, tau, work, &lwork, &info);
}

I built it without error but when I tried to run it, it didn't work. I got these warning messages:

D:\c++ code\lllll\main.cpp|15|warning: 'tau' is used uninitialized in this function [-Wuninitialized]|
D:\c++ code\lllll\main.cpp|15|warning: 'work' is used uninitialized in this function [-Wuninitialized]|

I don't know what's the problem but I think my definition of the dgeqrf function is wrong.

Also, the dgeqrf is a void function. I need to save its result (Q matrix) into an another matrix and use it in my calculation.

Does anyone have an idea about it?

1

There are 1 answers

0
Baum mit Augen On BEST ANSWER

As stated in the docs, TAU and WORK are supposed to be arrays the function can work on.

In particular, WORK shall be an array of double and have (at least) length LWORK, it is used as internal temporary memory.

TAU is an array used to output the elementary reflectors of the QR decomposition and shall have length (at least) min(n,m).

So your complete call would look something like this:

#include <iostream>
#include <lapacke.h>
using namespace std;
int main()
{
    double a[4] = {0,2,2,-1};
    int m=2;
    int n=2;
    int info = 0;
    int lda = m;
    int lwork = n;
    double work[2];
    double tau[2];
    dgeqrfp_(&m, &n, a, &lda, tau, work, &lwork, &info);
}