/* nag_zhbgst (f08usc) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include int main(void) { /* Scalars */ Integer i, j, k1, k2, ka, kb, n, pdab, pdbb, pdx, d_len, e_len; Integer exit_status=0; NagError fail; Nag_UploType uplo; Nag_OrderType order; /* Arrays */ char uplo_char[2]; Complex *ab=0, *bb=0, *x=0; double *d=0, *e=0; #ifdef NAG_COLUMN_MAJOR #define AB_UPPER(I,J) ab[(J-1)*pdab + k1 + I - J - 1] #define AB_LOWER(I,J) ab[(J-1)*pdab + I - J] #define BB_UPPER(I,J) bb[(J-1)*pdbb + k2 + I - J - 1] #define BB_LOWER(I,J) bb[(J-1)*pdbb + I - J] order = Nag_ColMajor; #else #define AB_UPPER(I,J) ab[(I-1)*pdab + J - I] #define AB_LOWER(I,J) ab[(I-1)*pdab + k1 + J - I - 1] #define BB_UPPER(I,J) bb[(I-1)*pdbb + J - I] #define BB_LOWER(I,J) bb[(I-1)*pdbb + k2 + J - I - 1] order = Nag_RowMajor; #endif INIT_FAIL(fail); Vprintf("f08usc Example Program Results\n\n"); /* Skip heading in data file */ Vscanf("%*[^\n] "); Vscanf("%ld%ld%ld%*[^\n] ", &n, &ka, &kb); pdab = ka + 1; pdbb = kb + 1; pdx = n; d_len = n; e_len = n-1; /* Allocate memory */ if ( !(ab = NAG_ALLOC(pdab * n, Complex)) || !(bb = NAG_ALLOC(pdbb * n, Complex)) || !(d = NAG_ALLOC(d_len, double)) || !(e = NAG_ALLOC(e_len, double)) || !(x = NAG_ALLOC(n * n, Complex)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } /* Read whether Upper or Lower part of A is stored */ Vscanf(" ' %1s '%*[^\n] ", uplo_char); if (*(unsigned char *)uplo_char == 'L') uplo = Nag_Lower; else if (*(unsigned char *)uplo_char == 'U') uplo = Nag_Upper; else { Vprintf("Unrecognised character for Nag_UploType type\n"); exit_status = -1; goto END; } /* Read A and B from data file */ k1 = ka + 1; k2 = kb + 1; if (uplo == Nag_Upper) { for (i = 1; i <= n; ++i) { for (j = i; j <= MIN(i+ka,n); ++j) { Vscanf(" ( %lf , %lf ) ", &AB_UPPER(i,j).re, &AB_UPPER(i,j).im); } } Vscanf("%*[^\n] "); } else { for (i = 1; i <= n; ++i) { for (j = MAX(1,i-ka); j <= i; ++j) { Vscanf(" ( %lf , %lf ) ", &AB_LOWER(i,j).re, &AB_LOWER(i,j).im); } } Vscanf("%*[^\n] "); } if (uplo == Nag_Upper) { for (i = 1; i <= n; ++i) { for (j = i; j <= MIN(i+kb,n); ++j) { Vscanf(" ( %lf, %lf ) ", &BB_UPPER(i,j).re, &BB_UPPER(i,j).im); } } Vscanf("%*[^\n] "); } else { for (i = 1; i <= n; ++i) { for (j = MAX(1,i-kb); j <= i; ++j) { Vscanf(" ( %lf, %lf ) ", &BB_LOWER(i,j).re, &BB_LOWER(i,j).im); } } Vscanf("%*[^\n] "); } /* Compute the split Cholesky factorization of B */ f08utc(order, uplo, n, kb, bb, pdbb, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08utc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Reduce the problem to standard form C*y = lambda*y, */ /* storing the result in A */ f08usc(order, Nag_DoNotForm, uplo, n, ka, kb, ab, pdab, bb, pdbb, x, pdx, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08usc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Reduce C to tridiagonal form T = (Q**T)*C*Q */ f08hsc(order, Nag_DoNotForm, uplo, n, ka, ab, pdab, d, e, x, pdx, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08hsc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Calculate the eigenvalues of T (same as C) */ f08jfc(n, d, e, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08jfc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Print eigenvalues */ Vprintf(" Eigenvalues\n"); for (i = 0; i < n; ++i) Vprintf(" %8.4lf",d[i]); Vprintf("\n"); END: if (ab) NAG_FREE(ab); if (bb) NAG_FREE(bb); if (d) NAG_FREE(d); if (e) NAG_FREE(e); if (x) NAG_FREE(x); return exit_status; }