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

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

int main(void) {
  /* Integer scalar and array declarations */
  Integer i, m1, m2, nb, pn, ierr, lsinit;
  Integer exit_status = 0;

  /* NAG structures and types */
  NagError fail;
  Nag_TS_Interpolation inter[2];
  Nag_TS_Transform ftype;

  /* Double scalar and array declarations */
  double p, tau;
  double *ma = 0, *rcomm = 0, *sinit = 0, *t = 0, *wma = 0;

  /* Character scalar and array declarations */
  char cinter[40], cftype[40];

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

  printf("nag_tsa_inhom_ma (g13mgc) Example Program Results\n\n");

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

  /* Read in the problem size */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &m1, &m2);

  /* Read in the transformation function and its parameter */
  scanf("%39s", cftype);
  ftype = (Nag_TS_Transform)nag_enum_name_to_value(cftype);
  scanf("%lf", &p);

  /* Read in the interpolation method to use */
  scanf("%39s", cinter);
  inter[0] = (Nag_TS_Interpolation)nag_enum_name_to_value(cinter);
  scanf("%39s", cinter);
  inter[1] = (Nag_TS_Interpolation)nag_enum_name_to_value(cinter);

  /* Read in the decay parameter */
  scanf("%lf%*[^\n] ", &tau);

  /* Read in the initial values */
  if (ftype == Nag_AbsDiff || ftype == Nag_AbsDiffScaled) {
    lsinit = 2 * m2 + 3;
  } else {
    lsinit = m2 + 2;
  }
  if (!(sinit = NAG_ALLOC(lsinit, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < lsinit; i++) {
    scanf("%lf", &sinit[i]);
  }
  scanf("%*[^\n] ");

  /* Print some titles */
  printf("              Time          MA\n");
  printf(" --------------------------------\n");

  if (!(rcomm = NAG_ALLOC(2 * m2 + 20, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  for (pn = 0;;) {
    /* Read in the number of observations in this block */
    ierr = scanf("%" NAG_IFMT, &nb);
    if (ierr == EOF || ierr < 1)
      break;
    scanf("%*[^\n] ");

    /* Allocate MA, T and WMA to the required size */
    NAG_FREE(ma);
    NAG_FREE(t);
    NAG_FREE(wma);
    if (!(ma = NAG_ALLOC(nb, double)) || !(t = NAG_ALLOC(nb, double)) ||
        !(wma = NAG_ALLOC(nb, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

    /* Read in the data for this block */
    for (i = 0; i < nb; i++) {
      scanf("%lf%lf", &t[i], &ma[i]);
    }
    scanf("%*[^\n] ");

    /* Call nag_tsa_inhom_ma (g13mgc) to update the moving average
       operator for this block of data. The routine overwrites the
       input data */
    nag_tsa_inhom_ma(nb, ma, t, tau, m1, m2, sinit, inter, ftype, &p, &pn, wma,
                     rcomm, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_tsa_inhom_ma (g13mgc).\n%s\n", fail.message);
      exit_status = -1;
      goto END;
    }

    /* Display the results for this block of data */
    for (i = 0; i < nb; i++) {
      printf(" %3" NAG_IFMT "    %10.1f    %10.3f\n", pn - nb + i + 1, t[i],
             ma[i]);
    }
    printf("\n");
  }

END:
  NAG_FREE(ma);
  NAG_FREE(wma);
  NAG_FREE(t);
  NAG_FREE(sinit);
  NAG_FREE(rcomm);

  return (exit_status);
}