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

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

int main(void) {
  /* Constants */
  Integer licomm = 100;
  /*Integer scalar and array declarations */
  Integer exit_status = 0;
  Integer i, n, nf, nwc, nwl;
  Integer *icomm = 0;
  NagError fail;
  Nag_Wavelet wavnamenum;
  Nag_WaveletMode modenum;
  /*Double scalar and array declarations */
  double *ca = 0, *cd = 0, *x = 0, *y = 0;
  /*Character scalar and array declarations */
  char mode[24], wavnam[20];

  INIT_FAIL(fail);

  printf("nag_wav_dim1_mxolap_fwd (c09dac) Example Program Results\n\n");
  fflush(stdout);

  /*     Skip heading in data file */
  scanf("%*[^\n] ");
  /*     Read n */
  scanf("%" NAG_IFMT "%*[^\n] ", &n);
  if (!(x = NAG_ALLOC(n, double)) || !(y = NAG_ALLOC(n, double)) ||
      !(icomm = NAG_ALLOC(licomm, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /*     Read wavnam, mode */
  scanf("%19s%23s%*[^\n] ", wavnam, mode);
  /*
   * nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  wavnamenum = (Nag_Wavelet)nag_enum_name_to_value(wavnam);
  modenum = (Nag_WaveletMode)nag_enum_name_to_value(mode);
  if (n >= 2) {
    printf("MODWT :: \n");
    printf("     Wavelet  :%16s\n", wavnam);
    printf("     End mode :%16s\n", mode);
    printf("     N        :%16" NAG_IFMT "\n\n", n);
    /*        Read array */
    printf("%s\n", "Input Data                  X :");
    for (i = 0; i < n; i++) {
      scanf("%lf ", &x[i]);
      printf("%8.4f%s", x[i], (i + 1) % 8 ? " " : "\n");
    }
    printf("\n");
    /*
     * nag_wav_dim1_init (c09aac)
     * Wavelet filter query
     */
    nag_wav_dim1_init(wavnamenum, Nag_MODWTSingle, modenum, n, &nwl, &nf, &nwc,
                      icomm, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_wav_dim1_init (c09aac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
    if (!(ca = NAG_ALLOC(nwc, double)) || !(cd = NAG_ALLOC(nwc, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    /*
     * nag_wav_dim1_mxolap_fwd (c09dac)
     * one-dimensional discrete wavelet transform (modwt)
     */
    nag_wav_dim1_mxolap_fwd(n, x, nwc, ca, cd, icomm, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_wav_dim1_mxolap_fwd (c09dac).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }

    printf("Approximation coefficients CA : \n");
    for (i = 0; i < nwc; i++)
      printf("%8.4f%s", ca[i], (i + 1) % 8 ? " " : "\n");
    printf("\n");
    printf("Detail coefficients        CD : \n");
    for (i = 0; i < nwc; i++)
      printf("%8.4f%s", cd[i], (i + 1) % 8 ? " " : "\n");
    printf("\n\n");
    /*
     * nag_wav_dim1_mxolap_inv (c09dbc)
     * one-dimensional inverse discrete wavelet transform (IMODWT)
     */
    nag_wav_dim1_mxolap_inv(nwc, ca, cd, n, y, icomm, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_wav_dim1_mxolap_inv (c09dbc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
    printf("Reconstruction              Y : \n");
    for (i = 0; i < n; i++)
      printf("%8.4f%s", y[i], (i + 1) % 8 ? " " : "\n");
  }

END:
  NAG_FREE(ca);
  NAG_FREE(cd);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(icomm);

  return exit_status;
}