/* qrdemo -- QR factorization using LAPACK Written November 26 by Eric Olson This program uses LAPACKE compiled with ATLAS BLAS. The example performs the following steps: 1. Given A we call LAPACKE_dgeqrf which overwrites A with the results of the QR decomposition. 2. Extract the upper triangular matrix from the output and store it as the matrix R. 3. Call LAPACKE_dormqr to multiply R by Q and store the product overwriting matrix R. 4. Compute the error between QR and the original matrix. */ #include #include #include #include #include void vecprint(int n, double v[n]) { int j; for(j = 0; j < n; j++) printf("%10.2e%c", v[j], j == n - 1 ? '\n' : ' '); } void matprint(int n, double A[n][n]) { int i; for(i = 0; i < n; i++) vecprint(n, A[i]); } int main() { printf("qrdemo -- QR factorization using LAPACK Version 2\n" "Written November 26 by Eric Olson\n\n"); int i, j, n = 5; double A[n][n], tau[n]; for(i = 0; i < n; i++) for(j = 0; j < n; j++) A[i][j] = i * n + j; printf("A=\n"); matprint(n, A); LAPACKE_dgeqrf(LAPACK_ROW_MAJOR, n, n, A[0], n, tau); printf("output from dgeqrf=\n"); matprint(n, A); printf("tau=\n"); vecprint(n, tau); double R[n][n]; for(i = 0; i < n; i++) for(j = 0; j < n; j++) R[i][j] = j < i ? 0 : A[i][j]; printf("R=\n"); matprint(n, R); LAPACKE_dormqr(LAPACK_ROW_MAJOR, 'L', 'N', n, n, n, A[0], n, tau, R[0], n); printf("QR=\n"); matprint(n, R); double rmserr = 0.0; for(i = 0; i < n; i++) for(j = 0; j < n; j++) { double t = R[i][j] - (i * n + j); rmserr += t * t; } rmserr = sqrt(rmserr / n / n); printf("error=\n%23.15e\n", rmserr); return 0; }