/* qrdemo -- QR factorization using LAPACK Written November 26 by Eric Olson This program uses LAPACK compiled with ATLAS BLAS. The example performs the following steps: 1. Given A we call clapack_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 ATL_C2Fdormlq 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 #include /* The routine ATL_C2Fdormlq is not included in the standard header files, so we include a declaration here */ extern int ATL_C2Fdormlq(const enum CBLAS_SIDE S, const enum CBLAS_TRANSPOSE TA, ATL_CINT M, ATL_CINT N, ATL_CINT K, double *A, ATL_CINT lda, double *TAU, double *C, ATL_CINT ldc); 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 1\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); clapack_dgeqrf(CblasRowMajor, 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); ATL_C2Fdormlq(CblasRight, CblasNoTrans, 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; }