/* nag_rand_corr_matrix (g05pyc) Example Program. * * Copyright 2008, Numerical Algorithms Group. * * Mark 9, 2009. */ /* Pre-processor includes */ #include #include #include #include #include #include #define C(I, J) c[J*pdc + I] int main(int argc, char *argv[]) { FILE *fpin, *fpout; /* Integer scalar and array declarations */ Integer exit_status = 0; Integer i, j, lstate, n, c_size; Integer *state = 0; Integer pdc; /* NAG structures */ NagError fail; /* Double scalar and array declarations */ double *c = 0, *d = 0; /* Set tolerance */ double eps = 0.00001e0; /* Choose the base generator */ Nag_BaseRNG genid = Nag_Basic; Integer subid = 0; /* Set the seed */ Integer seed[] = { 1762543 }; Integer lseed = 1; /* Initialise the error structure */ 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_rand_corr_matrix (g05pyc) Example Program Results\n\n"); /* Get the length of the state array */ lstate = -1; nag_rand_init_repeatable(genid, subid, seed, lseed, state, &lstate, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_init_repeatable (g05kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Read data from a file */ /* Skip heading*/ fscanf(fpin, "%*[^\n] "); /* Read in initial parameters */ fscanf(fpin, "%ld%*[^\n] ", &n); pdc = n; c_size = pdc * n; /* Allocate arrays */ if (!(c = NAG_ALLOC(c_size, double)) || !(d = NAG_ALLOC(n, double)) || !(state = NAG_ALLOC(lstate, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read in the eigenvalues */ for (i = 0; i < n; i++) fscanf(fpin, "%lf ", &d[i]); fscanf(fpin, "%*[^\n] "); /* Initialise the generator to a repeatable sequence */ nag_rand_init_repeatable(genid, subid, seed, lseed, state, &lstate, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_init_repeatable (g05kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Generate the correlation matrix*/ nag_rand_corr_matrix(n, d, eps, state, c, pdc, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_corr_matrix (g05pyc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Display the results */ for (i = 0; i < n; i++) { fprintf(fpout, " "); for (j = 0; j < n; j++) fprintf(fpout, "%8.3f%s", C(i, j), (j+1)%10?" ":"\n"); if (n%10) fprintf(fpout, "\n"); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (c) NAG_FREE(c); if (d) NAG_FREE(d); if (state) NAG_FREE(state); return exit_status; }