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

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

#define IEMA(I, J)                                                             \
  iema[(order == Nag_RowMajor) ? ((I)*pdiema + (J)) : ((J)*pdiema + (I))]

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

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

  /* Double scalar and array declarations */
  double p, tau;
  double *iema = 0, *rcomm = 0, *sinit = 0, *t = 0, *x = 0, *z = 0;

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

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

  printf("nag_tsa_inhom_iema_all (g13mfc) Example Program Results\n\n");

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

  /* Read in the required order for the output matrix */
  scanf("%39s%*[^\n] ", corder);
  order = (Nag_OrderType)nag_enum_name_to_value(corder);

  /* 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 (!(sinit = NAG_ALLOC(m2 + 2, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < m2 + 2; i++) {
    scanf("%lf", &sinit[i]);
  }
  scanf("%*[^\n] ");

  miema = m2 - m1 + 1;

  /* Print some titles */
  for (i = 0; i < 20 + 5 * miema; i++)
    printf(" ");
  printf("Iteration\n");
  printf("              Time         ");
  for (i = m1; i <= m2; i++)
    printf("%2" NAG_IFMT "        ", i);
  printf("\n ");
  for (i = 0; i < 21 + 10 * miema; i++)
    printf("-");
  printf("\n");

  if (!(rcomm = NAG_ALLOC(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] ");

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

    /* Read in the data for this block */
    if (ftype != 3) {
      for (i = 0; i < nb; i++) {
        scanf("%lf%lf", &t[i], &z[i]);
      }
    } else {
      /* Reallocate X to the required size */
      NAG_FREE(x);
      if (!(x = NAG_ALLOC(nb, double))) {
        printf("Allocation failure\n");
        exit_status = -1;
        goto END;
      }
      for (i = 0; i < nb; i++) {
        scanf("%lf%lf%lf", &t[i], &z[i], &x[i]);
      }
    }
    scanf("%*[^\n] ");

    if (order == Nag_ColMajor) {
      pdiema = nb;
    } else {
      pdiema = miema;
    }

    /* Reallocate the output array */
    NAG_FREE(iema);
    if (!(iema = NAG_ALLOC(nb * miema, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

    /* Call nag_tsa_inhom_iema_all (g13mfc) to update the iterated EMA for
       this block of data */
    nag_tsa_inhom_iema_all(order, nb, z, iema, pdiema, t, tau, m1, m2, sinit,
                           inter, ftype, &p, x, &pn, rcomm, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_tsa_inhom_iema_all (g13mfc).\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    ", pn - nb + i + 1, t[i]);
      for (j = 0; j < miema; j++) {
        printf("  %8.3f", IEMA(i, j));
      }
      printf("\n");
    }
    printf("\n");
  }

END:
  NAG_FREE(iema);
  NAG_FREE(t);
  NAG_FREE(z);
  NAG_FREE(x);
  NAG_FREE(sinit);
  NAG_FREE(rcomm);

  return (exit_status);
}