/* 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("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 */ f08aec(order, m, n, a, pda, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from 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 */ f08afc(order, m, m, n, u, pdu, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("order=%d\n", order); Vprintf("Error from 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 */ f08kec(order, n, n, pt, pdpt, d, e, tauq, taup, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08kec.\n%s\n", fail.message); exit_status = 1; goto END; } /* Update Q, storing the result in U */ f08kgc(order, Nag_FormQ, Nag_RightSide, Nag_NoTrans, m, n, n, pt, pdpt, tauq, u, pdu, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from 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"); x04cac(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, n, u, pdu, "Example 1: matrix Q", 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from x04cac.\n%s\n", fail.message); exit_status = 1; goto END; } } else { /* Compute the LQ factorization of A */ f08ahc(order, m, n, a, pda, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from 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 */ f08ajc(order, n, n, m, pt, pdpt, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from 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 */ f08kec(order, m, m, u, pdu, d, e, tauq, taup, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08kec.\n%s\n", fail.message); exit_status = 1; goto END; } /* Update P**T, storing the result in PT */ f08kgc(order, Nag_FormP, Nag_LeftSide, Nag_Trans, m, n, m, u, pdu, taup, pt, pdpt, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from 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"); x04cac(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, n, pt, pdpt, "Example 2: matrix P**T", 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from 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; }