/* nag_dormbr (f08kgc) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include #include int main(void) { /* Scalars */ Integer i, ic, j, m, n, pda, pdpt, pdu; Integer d_len, e_len, tau_len, tauq_len, taup_len; Integer exit_status=0; NagError fail; Nag_OrderType order; /* Arrays */ double *a=0, *d=0, *e=0, *pt=0, *tau=0, *taup=0, *tauq=0, *u=0; #ifdef NAG_COLUMN_MAJOR #define A(I,J) a[(J-1)*pda + I - 1] #define U(I,J) u[(J-1)*pdu + I - 1] #define PT(I,J) pt[(J-1)*pdpt + I - 1] order = Nag_ColMajor; #else #define A(I,J) a[(I-1)*pda + J - 1] #define U(I,J) u[(I-1)*pdu + J - 1] #define PT(I,J) pt[(I-1)*pdpt + J - 1] order = Nag_RowMajor; #endif INIT_FAIL(fail); Vprintf("nag_dormbr (f08kgc) Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n] "); for (ic = 1; ic <= 2; ++ic) { Vscanf("%ld%ld%*[^\n] ", &m, &n); #ifdef NAG_COLUMN_MAJOR pda = m; pdu = m; pdpt = n; taup_len = n; tauq_len = n; tau_len = n; d_len = n; e_len = n-1; #else pda = n; pdu = m; pdpt = n; taup_len = n; tauq_len = n; tau_len = n; d_len = n; e_len = n-1; #endif /* Allocate memory */ if ( !(a = NAG_ALLOC(m * n, double)) || !(d = NAG_ALLOC(d_len, double)) || !(e = NAG_ALLOC(e_len, double)) || !(pt = NAG_ALLOC(n * n, double)) || !(tau = NAG_ALLOC(tau_len, double)) || !(taup = NAG_ALLOC(taup_len, double)) || !(tauq = NAG_ALLOC(tauq_len, double)) || !(u = NAG_ALLOC(m * m, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } /* Read A from data file */ for (i = 1; i <= m; ++i) { for (j = 1; j <= n; ++j) Vscanf("%lf", &A(i,j)); } Vscanf("%*[^\n] "); if (m >= n) { /* Compute the QR factorization of A */ /* nag_dgeqrf (f08aec). * QR factorization of real general rectangular matrix */ nag_dgeqrf(order, m, n, a, pda, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dgeqrf (f08aec).\n%s\n", fail.message); exit_status = 1; goto END; } /* Copy A to U */ for (i = 1; i <= m; ++i) { for (j = 1; j <= MIN(i,n); ++j) U(i,j) = A(i,j); } /* Form Q explicitly, storing the result in U */ /* nag_dorgqr (f08afc). * Form all or part of orthogonal Q from QR factorization * determined by nag_dgeqrf (f08aec) or nag_dgeqpf (f08bec) */ nag_dorgqr(order, m, m, n, u, pdu, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("order=%d\n", order); Vprintf("Error from nag_dorgqr (f08afc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Copy R to PT (used as workspace) */ for (i = 1; i <= n; ++i) { for (j = i; j <= n; ++j) PT(i,j) = A(i,j); } /* Set the strictly lower triangular part of R to zero */ for (i = 2; i <= n; ++i) { for (j = 1; j <= MIN(i-1,n-1); ++j) PT(i,j) = 0.0; } /* Bidiagonalize R */ /* nag_dgebrd (f08kec). * Orthogonal reduction of real general rectangular matrix * to bidiagonal form */ nag_dgebrd(order, n, n, pt, pdpt, d, e, tauq, taup, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dgebrd (f08kec).\n%s\n", fail.message); exit_status = 1; goto END; } /* Update Q, storing the result in U */ /* nag_dormbr (f08kgc). * Apply orthogonal transformations from reduction to * bidiagonal form determined by nag_dgebrd (f08kec) */ nag_dormbr(order, Nag_FormQ, Nag_RightSide, Nag_NoTrans, m, n, n, pt, pdpt, tauq, u, pdu, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dormbr (f08kgc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print bidiagonal form and matrix Q */ Vprintf("\nExample 1: bidiagonal matrix B\nDiagonal\n"); for (i = 1; i <= n; ++i) Vprintf("%8.4f%s", d[i-1], i%8==0 ?"\n":" "); Vprintf("\nSuper-diagonal\n"); for (i = 1; i <= n - 1; ++i) Vprintf("%8.4f%s", e[i-1], i%8 == 0 ?"\n":" "); Vprintf("\n\n"); /* nag_gen_real_mat_print (x04cac). * Print real general matrix (easy-to-use) */ nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, n, u, pdu, "Example 1: matrix Q", 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message); exit_status = 1; goto END; } } else { /* Compute the LQ factorization of A */ /* nag_dgelqf (f08ahc). * LQ factorization of real general rectangular matrix */ nag_dgelqf(order, m, n, a, pda, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dgelqf (f08ahc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Copy A to PT */ for (i = 1; i <= m; ++i) { for (j = i; j <= n; ++j) PT(i,j) = A(i,j); } /* Form Q explicitly, storing the result in PT */ /* nag_dorglq (f08ajc). * Form all or part of orthogonal Q from LQ factorization * determined by nag_dgelqf (f08ahc) */ nag_dorglq(order, n, n, m, pt, pdpt, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dorglq (f08ajc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Copy L to U (used as workspace) */ for (i = 1; i <= m; ++i) { for (j = 1; j <= i; ++j) U(i,j) = A(i,j); } /* Set the strictly upper triangular part of L to zero */ for (i = 1; i <= m-1; ++i) { for (j = i+1; j <= m; ++j) U(i,j) = 0.0; } /* Bidiagonalize L */ /* nag_dgebrd (f08kec), see above. */ nag_dgebrd(order, m, m, u, pdu, d, e, tauq, taup, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dgebrd (f08kec).\n%s\n", fail.message); exit_status = 1; goto END; } /* Update P**T, storing the result in PT */ /* nag_dormbr (f08kgc), see above. */ nag_dormbr(order, Nag_FormP, Nag_LeftSide, Nag_Trans, m, n, m, u, pdu, taup, pt, pdpt, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dormbr (f08kgc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print bidiagonal form and matrix P**T */ Vprintf("\nExample 2: bidiagonal matrix B\n%s\n", "Diagonal"); for (i = 1; i <= m; ++i) Vprintf("%8.4f%s", d[i-1], i%8==0 ?"\n":" "); Vprintf("\nSuper-diagonal\n"); for (i = 1; i <= m - 1; ++i) Vprintf("%8.4f%s", e[i-1], i%8==0 ?"\n":" "); Vprintf("\n\n"); /* nag_gen_real_mat_print (x04cac), see above. */ nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, n, pt, pdpt, "Example 2: matrix P**T", 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message); exit_status = 1; goto END; } } END: if (a) NAG_FREE(a); if (d) NAG_FREE(d); if (e) NAG_FREE(e); if (pt) NAG_FREE(pt); if (tau) NAG_FREE(tau); if (taup) NAG_FREE(taup); if (tauq) NAG_FREE(tauq); if (u) NAG_FREE(u); } return exit_status; }