/* nag_real_general_eigensystem (f02bjc) Example Program. * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * Mark 8 revised, 2004. */ #include #include #include #include #include #include #include #define A(I, J) a[(I) *tda + J] #define B(I, J) b[(I) *tdb + J] #define Z(I, J) z[(I) *tdz + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Nag_Boolean matz; Complex *alfa = 0; Integer exit_status = 0, i, ip, *iter = 0, j, k, n, tda, tdb, tdz; double *a = 0, *b = 0, *beta = 0, eps1, *z = 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_general_eigensystem (f02bjc) Example Program Results\n"); fscanf(fpin, "%*[^\n]"); /* Skip heading in data file */ fscanf(fpin, "%ld", &n); if (n >= 1) { if (!(beta = NAG_ALLOC(n, double)) || !(a = NAG_ALLOC(n*n, double)) || !(b = NAG_ALLOC(n*n, double)) || !(z = NAG_ALLOC(n*n, double)) || !(iter = NAG_ALLOC(n, Integer)) || !(alfa = NAG_ALLOC(n, Complex))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tda = n; tdb = n; tdz = n; } else { fprintf(fpout, "Invalid n.\n"); exit_status = 1; return exit_status; } for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) fscanf(fpin, "%lf", &A(i, j)); for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) fscanf(fpin, "%lf", &B(i, j)); matz = Nag_TRUE; /* nag_machine_precision (x02ajc). * The machine precision */ eps1 = X02AJC; /* nag_real_general_eigensystem (f02bjc). * All eigenvalues and optionally eigenvectors of real * generalized eigenproblem, by QZ algorithm */ nag_real_general_eigensystem(n, a, tda, b, tdb, eps1, alfa, beta, matz, z, tdz, iter, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_real_general_eigensystem (f02bjc).\n%s\n", fail.message); exit_status = 1; goto END; } ip = 0; for (i = 0; i < n; ++i) { fprintf(fpout, "Eigensolution %4ld\n", i+1); fprintf(fpout, "alfa[%ld].re %7.3f", i, alfa[i].re); fprintf(fpout, " alfa[%ld].im %7.3f", i, alfa[i].im); fprintf(fpout, " beta[%ld] %7.3f\n", i, beta[i]); if (beta[i] == 0.0) fprintf(fpout, "lambda is infinite"); else if (alfa[i].im == 0.0) { fprintf(fpout, "lambda %7.3f\n", alfa[i].re/beta[i]); fprintf(fpout, "Eigenvector\n"); for (j = 0; j < n; ++j) fprintf(fpout, "%7.3f\n", Z(j, i)); } else { fprintf(fpout, "lambda %7.3f %7.3f\n", alfa[i].re/beta[i], alfa[i].im/beta[i]); fprintf(fpout, "Eigenvector\n"); k = (Integer) pow((double) -1, (double)(ip+2)); for (j = 0; j < n; ++j) { fprintf(fpout, "%7.3f", Z(j, i-ip)); fprintf(fpout, "%7.3f\n", k*Z(j, i-ip+1)); } ip = 1-ip; } } fprintf(fpout, "Number of iterations (machine-dependent)\n"); for (i = 0; i < n; ++i) fprintf(fpout, "%2ld", iter[i]); fprintf(fpout, "\n"); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (beta) NAG_FREE(beta); if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (z) NAG_FREE(z); if (iter) NAG_FREE(iter); if (alfa) NAG_FREE(alfa); return exit_status; }