#include <stdlib.h>
#include <stdio.h>
#define real double

int qrback(real** Q,real** R,int n,int m,real* b,real* x){
// QR backsubstitution routine.
// Q, R : input : the matrices from QR-decomposition A=QR;
// b : input : the right-hand side b of the equation Ax=b;
// x : output : the solution x of the equatin Ax=b;
   real* c = (real*)calloc(m,sizeof(real));
   for(int i=0;i<m;i++){
      c[i]=0; for(int j=0;j<n;j++) c[i]+=Q[i][j]*b[j];}
   for(int i=m-1;i>=0;i--){
      real s=0; for(int j=i+1;j<m;j++) s+=R[j][i]*x[j];
      x[i]=(c[i]-s)/R[i][i]; }
   free(c);
   return 0; }
