/* nag_rand_matrix_corr (g05pyc) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* Mark 30.3, 2024.
*/
/* Pre-processor includes */
#include <math.h>
#include <nag.h>
#include <stdio.h>
#define C(I, J) c[J * pdc + I]
int main(void) {
/* 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;
/* Initialize the error structure */
INIT_FAIL(fail);
printf("nag_rand_matrix_corr (g05pyc) Example Program Results\n\n");
/* Get the length of the state array */
lstate = -1;
nag_rand_init_repeat(genid, subid, seed, lseed, state, &lstate, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_rand_init_repeat (g05kfc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* Read data from a file */
/* Skip heading */
scanf("%*[^\n] ");
/* Read in initial parameters */
scanf("%" NAG_IFMT "%*[^\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))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read in the eigenvalues */
for (i = 0; i < n; i++)
scanf("%lf ", &d[i]);
scanf("%*[^\n] ");
/* Initialize the generator to a repeatable sequence */
nag_rand_init_repeat(genid, subid, seed, lseed, state, &lstate, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_rand_init_repeat (g05kfc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* Generate the correlation matrix */
nag_rand_matrix_corr(n, d, eps, state, c, pdc, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_rand_matrix_corr (g05pyc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* Display the results */
for (i = 0; i < n; i++) {
printf(" ");
for (j = 0; j < n; j++)
printf("%8.3f%s", C(i, j), (j + 1) % 10 ? " " : "\n");
if (n % 10)
printf("\n");
}
END:
NAG_FREE(c);
NAG_FREE(d);
NAG_FREE(state);
return exit_status;
}