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

NAG CL Interface Introduction
Example description
/* nag_tsa_multi_diff (g13dlc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */

#include <nag.h>
#include <stdio.h>

int main(void) {
  /* Scalars */
  Integer exit_status, i, j, k, maxd, mind, n, nd, nw, pdw, pddelta, kmax;
  NagError fail;

  /* Arrays */
  double *delta = 0, *w = 0, *z = 0;
  Integer *id = 0, *tr = 0;

#define W(I, J) w[(J - 1) * pdw + I - 1]
#define DELTA(I, J) delta[(J - 1) * pddelta + I - 1]
#define Z(I, J) z[(J - 1) * kmax + I - 1]

  INIT_FAIL(fail);

  exit_status = 0;

  printf("nag_tsa_multi_diff (g13dlc) Example Program Results\n");

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

  if (k > 0 && n > 0) {
    kmax = k;
    /* Allocate array id */
    if (!(id = NAG_ALLOC(k, Integer))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

    for (i = 1; i <= k; ++i)
      scanf("%" NAG_IFMT "", &id[i - 1]);
    scanf("%*[^\n] ");

    mind = 0;
    maxd = 0;
    for (i = 1; i <= k; ++i) {
      mind = MIN(mind, id[i - 1]);
      maxd = MAX(maxd, id[i - 1]);
    }

    if (mind >= 0) {
      /* Allocate arrays */
      nw = n - maxd;
      if (!(tr = NAG_ALLOC(k, Integer)) ||
          !(delta = NAG_ALLOC(kmax * maxd, double)) ||
          !(w = NAG_ALLOC(kmax * nw, double)) ||
          !(z = NAG_ALLOC(kmax * n, double))) {
        printf("Allocation failure\n");
        exit_status = -1;
        goto END;
      }

      pdw = kmax;
      pddelta = kmax;

      for (i = 1; i <= k; ++i) {
        for (j = 1; j <= n; ++j)
          scanf("%lf", &Z(i, j));
        scanf("%*[^\n] ");
      }

      for (i = 1; i <= k; ++i)
        scanf("%" NAG_IFMT "", &tr[i - 1]);
      scanf("%*[^\n] ");

      if (maxd > 0) {
        for (i = 1; i <= k; ++i) {
          for (j = 1; j <= id[i - 1]; ++j)
            scanf("%lf", &DELTA(i, j));
          scanf("%*[^\n] ");
        }
      }

      /* nag_tsa_multi_diff (g13dlc).
       * Multivariate time series, differences and/or transforms
       */
      nag_tsa_multi_diff(k, n, z, tr, id, delta, w, &nd, &fail);

      if (fail.code != NE_NOERROR) {
        printf("Error from nag_tsa_multi_diff (g13dlc).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }
      printf("\n");
      printf(" Transformed/Differenced series\n");
      printf(" ------------------------------\n");

      for (i = 1; i <= k; ++i) {
        printf("\n");
        printf(" Series %2" NAG_IFMT "\n", i);
        printf(" -----------\n");
        printf("\n");
        printf(" Number of differenced values = %6" NAG_IFMT "\n", nd);
        printf("\n");
        for (j = 1; j <= nd; ++j) {
          printf("%10.3f", W(i, j));
          if (j % 8 == 0 || j == nd)
            printf("\n");
        }
      }
    }
  }

END:
  NAG_FREE(tr);
  NAG_FREE(delta);
  NAG_FREE(w);
  NAG_FREE(z);
  NAG_FREE(id);

  return exit_status;
}