/* nag_complex_form_q (f01rec) Example Program. * * Copyright 1990 Numerical Algorithms Group. * * Mark 1, 1990. * Mark 8 revised, 2004. */ #include #include #include #include #define COMPLEX(A) A.re, A.im #define A(I, J) a[(I) *tda + J] #define Q(I, J) q[(I) *tdq + J] int main(void) { Complex *a = 0, *q = 0, *theta = 0; Integer exit_status = 0, i, j, m, n, ncolq, tda, tdq; NagError fail; INIT_FAIL(fail); printf("nag_complex_form_q (f01rec) Example Program Results\n"); /* Skip heading in data file */ scanf("%*[^\n]"); scanf("%ld%ld", &m, &n); if (n >= 0 && m >= n) { if (!(a = NAG_ALLOC(m*n, Complex)) || !(q = NAG_ALLOC(m*m, Complex)) || !(theta = NAG_ALLOC(m, Complex))) { printf("Allocation failure\n"); exit_status = -1; goto END; } tda = n; tdq = m; } else { printf("Invalid n or m.\n"); exit_status = 1; return exit_status; } for (i = 0; i < m; ++i) for (j = 0; j < n; ++j) scanf(" ( %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) { printf("Error from nag_complex_qr (f01rcc).\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).re = A(i, j).re, Q(i, j).im = A(i, j).im; ncolq = m; /* nag_complex_form_q (f01rec). * Form columns of Q after factorization by nag_complex_qr * (f01rcc) */ nag_complex_form_q(Nag_ElementsSeparate, m, n, ncolq, q, tdq, theta, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_complex_form_q (f01rec).\n%s\n", fail.message); exit_status = 1; goto END; } printf("\nMatrix Q\n"); for (i = 0; i < m; ++i) { for (j = 0; j < ncolq; ++j) printf(" (%5.2f,%5.2f)%s", COMPLEX(Q(i, j)), (j%5 == 4 || j == ncolq-1)?"\n":" "); } END: if (a) NAG_FREE(a); if (q) NAG_FREE(q); if (theta) NAG_FREE(theta); return exit_status; }