/* nag_trans_hessenberg_controller (g13exc) Example Program. * * Copyright 1993 Numerical Algorithms Group * * Mark 3, 1993 * Mark 8 revised, 2004. */ #include #include #include #include #include #define A(I, J) a[(I) *tda + J] #define B(I, J) b[(I) *tdb + J] #define U(I, J) u[(I) *tdu + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, j, m, n, tda, tdb, tdu; double *a = 0, *b = 0, one = 1.0, *u = 0, zero = 0.0; Nag_ControllerForm reduceto; 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_trans_hessenberg_controller (g13exc) Example Program Results\n"); /* Skip the heading in the data file and read the data. */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld%ld", &n, &m); if (n >= 1 || m >= 1) { if (!(a = NAG_ALLOC(n*n, double)) || !(b = NAG_ALLOC(n*m, double)) || !(u = NAG_ALLOC(n*n, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tda = n; tdb = m; tdu = n; } else { fprintf(fpout, "Invalid n or m.\n"); exit_status = 1; return exit_status; } reduceto = Nag_UH_Controller; for (j = 0; j < n; ++j) for (i = 0; i < n; ++i) fscanf(fpin, "%lf", &A(i, j)); for (j = 0; j < m; ++j) for (i = 0; i < n; ++i) fscanf(fpin, "%lf", &B(i, j)); if (u) /* Initialise U as the identity matrix. */ for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) U(i, j) = zero; U(i, i) = one; } /* Reduce the pair (B,A) to reduceto controller Hessenberg form. */ /* nag_trans_hessenberg_controller (g13exc). * Unitary state-space transformation to reduce (BA) to * lower or upper controller Hessenberg form */ nag_trans_hessenberg_controller(n, m, reduceto, a, tda, b, tdb, u, tdu, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_trans_hessenberg_controller (g13exc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\nThe transformed state transition matrix is\n\n"); for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) fprintf(fpout, "%8.4f ", A(i, j)); fprintf(fpout, "\n"); } fprintf(fpout, "\nThe transformed input matrix is\n\n"); for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) fprintf(fpout, "%8.4f ", B(i, j)); fprintf(fpout, "\n"); } if (u) { fprintf(fpout, "\nThe matrix that reduces (B,A) to "); fprintf(fpout, "controller Hessenberg form is\n\n"); for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) fprintf(fpout, "%8.4f ", U(i, j)); fprintf(fpout, "\n"); } } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (u) NAG_FREE(u); return exit_status; }