NAG Library Manual, Mark 30.1
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_rand_resample (g05nfc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */
/* Pre-processor includes */
#include <math.h>
#include <nag.h>
#include <stdio.h>

#define ISAMPL(I, J) isampl[(J)*pdisampl + I]

int main(void) {
  /* Integer scalar and array declarations */
  Integer i, j, pdisampl, lipop, lstate, m, n, nrs, otype, rtype, subid;
  Integer exit_status = 0, lseed = 1;
  Integer seed[1];
  Integer *ipop = 0, *isampl = 0, *state = 0;

  /* Nag Types */
  NagError fail;
  Nag_BaseRNG genid;

  /* Double array declarations */
  double *wt = 0;

  /* Character scalar and array declarations */
  char sgenid[40];

  /* Initialize the error structure */
  INIT_FAIL(fail);

  printf("nag_rand_resample (g05nfc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Read in the base generator information and seed */
  scanf("%39s%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", sgenid, &subid, &seed[0]);

  /* Convert string of enum name to enum value */
  genid = (Nag_BaseRNG) nag_enum_name_to_value(sgenid);

  /* Initial call to the initializer to get the size 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;
  }

  /* Allocate arrays */
  if (!(state = NAG_ALLOC(lstate, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* 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;
  }

  /* Read in the required resampling method and output type */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &rtype, &otype);

  /* Read in original sample size, resample size, number of samples
   * and population length */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &n, &m,
        &nrs, &lipop);

  if (otype == 1) {
    pdisampl = m;
  } else {
    pdisampl = n;
  }

  if (!(ipop = NAG_ALLOC(lipop, Integer)) || !(wt = NAG_ALLOC(n, double)) ||
      !(isampl = NAG_ALLOC(pdisampl * nrs, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  if (lipop == n) {
    /* Read in the population and weights */
    for (i = 0; i < n; ++i) {
      scanf("%" NAG_IFMT "%lf%*[^\n] ", &ipop[i], &wt[i]);
    }
  } else {
    /* Read in just the weights */
    for (i = 0; i < n; ++i) {
      scanf("%lf%*[^\n] ", &wt[i]);
    }
  }

  /* Resample from the population using the supplied weights */
  nag_rand_resample(rtype, n, wt, ipop, m, nrs, otype, isampl, pdisampl, state,
                    &fail);
  if (fail.code == NW_POTENTIAL_PROBLEM) {
    printf("Warning from nag_rand_resample (g05nfc).\n%s\n", fail.message);
  } else if (fail.code != NE_NOERROR) {
    printf("Error from nag_rand_resample (g05nfc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Display the results */
  if (otype == 2) {
    for (j = 0; j < 3 * nrs + 9; ++j)
      printf(" ");
    printf("Count for Sample\n");
    printf("  Value        ");
    for (j = 0; j < nrs; ++j)
      printf(" %5" NAG_IFMT "", j + 1);
    printf("\n");
    for (j = 0; j < ((nrs < 2) ? 30 : 6 * nrs + 18); ++j)
      printf("-");
    printf("\n");
    for (i = 0; i < n; ++i) {
      printf(" %5" NAG_IFMT "         ", (lipop == 0) ? i + 1 : ipop[i]);
      for (j = 0; j < nrs; ++j)
        printf(" %5" NAG_IFMT "", ISAMPL(i, j));
      printf("\n");
    }
  } else {
    for (j = 0; j < 3 * nrs; ++j)
      printf(" ");
    printf("Sample\n");
    for (j = 0; j < nrs; ++j)
      printf(" %5" NAG_IFMT "", j + 1);
    printf("\n");
    for (j = 0; j < 6 * nrs + 3; ++j)
      printf("-");
    printf("\n");
    for (i = 0; i < m; ++i) {
      for (j = 0; j < nrs; ++j)
        printf(" %5" NAG_IFMT "", ISAMPL(i, j));
      printf("\n");
    }
  }

END:

  /* Cleanup the memory */
  if (state)
    NAG_FREE(state);
  if (ipop)
    NAG_FREE(ipop);
  if (wt)
    NAG_FREE(wt);
  if (isampl)
    NAG_FREE(isampl);

  return exit_status;
}