I want to find diagonal elements of R matrix obtained from QR-decomposition of A matrix as A=QR using lapack. I tried lapack dgeqrf subroutine but it is giving back the same matrix A i.e. input and output matrices are same. How to find R matrix and its diagonals ? I can't figure out what is going wrong with this code. I am programming in C.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void dgeqrf_(int *rows, int *cols, double *matA, int *LDA, double *TAU, double *WORK, int *LWORK, int *INFO);
int main()
{
int rows=3;
int cols=3;
double *matA=malloc(sizeof(double)*rows*cols);
matA[0]=10;
matA[1]=20;
matA[2]=10;
matA[3]=40;
matA[4]=20;
matA[5]=50;
matA[6]=70;
matA[7]=30;
matA[8]=20;
for(int i=0; i<rows*cols; i++)
{
printf("%f ",matA[i]);
}
printf("\n");
int LDA=rows;
int INFO;
double *WORK=malloc(sizeof(double)*2);
int LWORK=-1;
int rowcolmin=rows;
if(rowcolmin>cols)
{
rowcolmin=cols;
}
double *TAU=malloc(sizeof(double)*rowcolmin);
dgeqrf_(&rows, &cols, matA, &LDA, TAU, WORK, &LWORK, &INFO);
for(int i=0; i<rows*cols; i++)
{
printf("%f ",matA[i]);
}
printf("\n");
free(WORK);
free(TAU);
free(matA);
return 0;
}