/* nag_rand_quasi_normal_bydim (g05yqc) Example Program.
*
* Copyright 2023 Numerical Algorithms Group.
*
* Mark 29.1, 2023.
*/
/* Pre-processor includes */
#include <nag.h>
#include <stdio.h>
#define QUAS(I,J) quas[(J)*pdquas+I]
int main(void) {
/* Integer scalar and array declarations */
Integer dn, fdim, i, j, idim, iskip, ldim, pdquas, liref, n, rdim;
Integer *iref = 0;
Integer exit_status = 0;
/* Nag Types */
NagError fail;
Nag_OrderType order;
Nag_QuasiRandom_Sequence genid;
/* char scalar and array declarations */
char genid_str[40];
/* Double scalar and array declarations */
double *quas = 0, *xmean = 0, *std = 0;
/* Initialize the error structure */
INIT_FAIL(fail);
printf("nag_rand_quasi_normal_bydim (g05yqc) Example Program Results\n\n");
/* Skip heading in data file */
scanf("%*[^\n] ");
/* Fix the order = Nag_ColMajor, so QUAS(1:N,:) */
order = Nag_ColMajor;
/* Read in the generator to use */
scanf("%39s%*[^\n] ", genid_str);
/* Convert the string to the associated enum */
genid = (Nag_QuasiRandom_Sequence)nag_enum_name_to_value(genid_str);
/* Read in the problem size */
scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &n, &idim, &iskip);
/* Calculate the size of the reference vector */
liref = (genid == Nag_QuasiRandom_Faure) ? 407 : 32 * idim + 7;
/* Because we are calling nag_rand_quasi_uniform_bydim (g05ypc), IREF needs
* to be IDIM elements bigger than the minimum length
*/
liref += idim;
if (!(iref = NAG_ALLOC(liref, Integer)) ||
!(xmean = NAG_ALLOC(idim, double)) ||
!(std = NAG_ALLOC(idim, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read in the mean and standard deviation */
for (i = 0; i < idim; ++i) {
scanf("%lf", &xmean[i]);
}
scanf("%*[^\n] ");
for (i = 0; i < idim; ++i) {
scanf("%lf", &std[i]);
}
scanf("%*[^\n] ");
/* Read in the dimensions that we require */
scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &fdim, &ldim);
rdim = ldim - fdim + 1;
pdquas = n;
if (!(quas = NAG_ALLOC(pdquas*rdim, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Initialize the generator */
nag_quasi_init(genid,idim,iref,liref,iskip,&fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_quasi_init (g05ylc).\n%s\n",
fail.message);
exit_status = -1;
goto END;
}
/* Generate N quasi-random variates for dimensions FDIM to LDIM */
nag_rand_quasi_normal_bydim(order,n,xmean,std,fdim,ldim,quas,pdquas,iref,
&fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_rand_quasi_normal_bydim (g05yqc).\n%s\n",
fail.message);
exit_status = -1;
goto END;
}
/* Read in number of variates to display */
scanf("%" NAG_IFMT "%*[^\n] ", &dn);
/* Display the first dn variates */
printf("\n");
printf("First %" NAG_IFMT " variates for dimensions %" NAG_IFMT
" to %" NAG_IFMT "\n", dn, fdim, ldim);
printf(" ");
for (i = fdim; i <= ldim; ++i)
printf(" %4" NAG_IFMT "",i);
printf("\n");
for (i = 0; i < n; ++i) {
printf("%5" NAG_IFMT " ", i+1);
for (j = 0; j < rdim; ++j)
printf(" %8.4f", QUAS(i,j));
printf("\n");
}
END:
NAG_FREE(iref);
NAG_FREE(quas);
NAG_FREE(xmean);
NAG_FREE(std);
return (exit_status);
}