/* nag_complex_qr (f01rcc) Example Program. * * Copyright 1990 Numerical Algorithms Group. * * Mark 1, 1990. * Mark 8 revised, 2004. */ #include #include #include #include #include #define COMPLEX(A) A.re, A.im #define A(I, J) a[(I) *tda + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Complex *a = 0, *theta = 0; Integer exit_status = 0, i, j, m, n, tda; NagError fail; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fprintf(fpout, "nag_complex_qr (f01rcc) Example Program Results\n"); fscanf(fpin, "%ld%ld", &m, &n); fprintf(fpout, "\n"); if (n >= 0 && m >= n) { if (!(a = NAG_ALLOC(m*n, Complex)) || !(theta = NAG_ALLOC(n, Complex))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tda = n; } else { fprintf(fpout, "Invalid n or m.\n"); exit_status = 1; return exit_status; } for (i = 0; i < m; ++i) for (j = 0; j < n; ++j) fscanf(fpin, " ( %lf, %lf ) ", COMPLEX(&A(i, j))); /* Find the QR factorization of A. */ /* nag_complex_qr (f01rcc). * QR factorization of complex m by n matrix (m~>=~n) */ nag_complex_qr(m, n, a, tda, theta, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_complex_qr (f01rcc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "QR factorization of A\n"); fprintf(fpout, "Vector THETA\n"); for (i = 0; i < n; ++i) fprintf(fpout, " (%7.4f,%8.4f)%s", COMPLEX(theta[i]), (i%3 == 2 || i == n-1)?"\n":" "); fprintf(fpout, "\nMatrix A after factorization (upper triangular part is R)\n"); for (i = 0; i < m; ++i) { for (j = 0; j < n; ++j) fprintf(fpout, " (%7.4f,%8.4f)%s", COMPLEX(A(i, j)), (j%3 == 2 || j == n-1)?"\n":" "); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (a) NAG_FREE(a); if (theta) NAG_FREE(theta); return exit_status; }