/* nag_real_form_q (f01qec) Example Program. * * Copyright 1990 Numerical Algorithms Group. * * Mark 1, 1990. * Mark 8 revised, 2004. */ #include #include #include #include #include #define A(I, J) a[(I) *tda + J] #define Q(I, J) q[(I) *tdq + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, j, m, n, ncolq, tda, tdq; double *a = 0, *q = 0, *zeta = 0; 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); fprintf(fpout, "nag_real_form_q (f01qec) Example Program Results\n"); fscanf(fpin, " %*[^\n]"); /* skip headings in data file */ fscanf(fpin, " %*[^\n]"); fscanf(fpin, "%ld%ld", &m, &n); if (n >= 0 && m >= n) { if (!(zeta = NAG_ALLOC(n, double)) || !(a = NAG_ALLOC(m*n, double)) || !(q = NAG_ALLOC(m*m, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tda = n; tdq = m; } else { fprintf(fpout, "Invalid n or m.\n"); exit_status = 1; return exit_status; } fscanf(fpin, " %*[^\n]"); for (i = 0; i < m; ++i) /* Read matrix data */ for (j = 0; j < n; ++j) fscanf(fpin, "%lf", &A(i, j)); /* Find the QR factorization of A */ /* nag_real_qr (f01qcc). * QR factorization of real m by n matrix (m~>=~n) */ nag_real_qr(m, n, a, tda, zeta, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_real_qr (f01qcc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Copy the array a into q and form the m by m matrix Q */ for (j = 0; j < n; ++j) for (i = 0; i < m; ++i) Q(i, j) = A(i, j); ncolq = m; /* nag_real_form_q (f01qec). * Form columns of Q after factorization by nag_real_qr * (f01qcc) */ nag_real_form_q(Nag_ElementsSeparate, m, n, ncolq, q, tdq, zeta, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_real_form_q (f01qec).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "Matrix Q\n"); for (i = 0; i < m; ++i) { for (j = 0; j < ncolq; ++j) fprintf(fpout, " %8.4f", Q(i, j)); fprintf(fpout, "\n"); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (zeta) NAG_FREE(zeta); if (a) NAG_FREE(a); if (q) NAG_FREE(q); return exit_status; }