/* nag_rgsn_matrix_multi_normal (g05lyc) Example Program. * * Copyright 2004 Numerical Algorithms Group. * * Mark 8, 2004. */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpout; /* Scalars */ Integer exit_status, i, igen, j, lr, m, mode, n, pdc, pdx; /* Arrays */ double *c = 0, *r = 0, *x = 0, *xmu = 0; Integer iseed[4]; /* Nag types */ NagError fail; Nag_OrderType order; /* Set the number of variables and variates */ m = 4; n = 10; lr = m * (m + 1) + 2; pdc = m; #ifdef NAG_COLUMN_MAJOR #define C(I, J) c[(J-1)*pdc + I - 1] #define X(I, J) x[(J-1)*pdx + I - 1] order = Nag_ColMajor; pdx = n; #else #define C(I, J) c[(I-1)*pdc + J - 1] #define X(I, J) x[(I-1)*pdx + J - 1] order = Nag_RowMajor; pdx = m; #endif #define XMU(I) xmu[I - 1] INIT_FAIL(fail); /* Check for command-line IO options */ fpout = nag_example_file_io(argc, argv, "-results", NULL); exit_status = 0; /* Allocate memory */ if (!(c = NAG_ALLOC(m * m, double)) || !(r = NAG_ALLOC(lr, double)) || !(x = NAG_ALLOC(n * m, double)) || !(xmu = NAG_ALLOC(m, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } fprintf(fpout, "nag_rgsn_matrix_multi_normal (g05lyc) Example Program Results\n\n"); /* Initialise the seed to a repeatable sequence */ iseed[0] = 1762543; iseed[1] = 9324783; iseed[2] = 42344; iseed[3] = 742355; /* Choose the random generator to use */ igen = 1; /* Initialise the random generator */ /* nag_rngs_init_repeatable (g05kbc). * Initialize seeds of a given generator for random number * generating functions (that pass seeds explicitly) to give * a repeatable sequence */ nag_rngs_init_repeatable(&igen, iseed); /* Input the upper triangle portion of the covariance matrix */ C(1, 1) = 1.69; C(1, 2) = 0.39; C(1, 3) = -1.86; C(1, 4) = 0.07; C(2, 2) = 98.01; C(2, 3) = -7.07; C(2, 4) = -0.71; C(3, 3) = 11.56; C(3, 4) = 0.03; C(4, 4) = 0.01; /* Input the means */ XMU(1) = 1.0; XMU(2) = 2.0; XMU(3) = -3.0; XMU(4) = 0.0; /* Set the mode */ mode = 0; /* Set up reference vector and generate n numbers */ /* nag_rgsn_matrix_multi_normal (g05lyc). * Generates a matrix of random numbers from a multivariate * Normal distribution, seeds and generator passed * explicitly */ nag_rgsn_matrix_multi_normal(order, mode, m, xmu, c, pdc, n, x, pdx, igen, iseed, r, lr, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rgsn_matrix_multi_normal (g05lyc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Display the results */ for (i = 1; i <= n; ++i) { for (j = 1; j <= m; ++j) { fprintf(fpout, "%10.4f ", X(i, j)); } fprintf(fpout, "\n"); } END: if (fpout != stdout) fclose(fpout); if (c) NAG_FREE(c); if (r) NAG_FREE(r); if (x) NAG_FREE(x); if (xmu) NAG_FREE(xmu); return exit_status; }