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

NAG CL Interface Introduction
Example description
/* nag_wav_dim2_init (c09abc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 */

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

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  Integer i, j, lenc, m, n, nf, nwcm, nwcn, nwct, nwlmax, pda, pdb;
  Integer want_level, want_coeffs;
  /* Arrays */
  char mode[24], wavnam[20], title[50];
  double *a = 0, *b = 0, *c = 0, *d = 0;
  Integer *dwtlevm = 0, *dwtlevn = 0;
  Integer icomm[180];
  /* Nag Types */
  Nag_Wavelet wavnamenum;
  Nag_WaveletMode modenum;
  Nag_MatrixType matrix = Nag_GeneralMatrix;
  Nag_OrderType order = Nag_ColMajor;
  Nag_DiagType diag = Nag_NonUnitDiag;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_wav_dim2_init (c09abc) Example Program Results\n\n");

  /* Skip heading in data file and read problem parameters */
  scanf("%*[^\n] %" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &m, &n);
  pda = m;
  pdb = m;
  scanf("%19s%23s%*[^\n] ", wavnam, mode);
  if (!(a = NAG_ALLOC(pda * n, double)) || !(b = NAG_ALLOC(pdb * n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  printf(" Parameters read from file :: \n");
  printf(" MLDWT :: Wavelet  : %s\n", wavnam);
  printf("          End mode : %s\n", mode);
  printf("          m        : %" NAG_IFMT "\n", m);
  printf("          n        : %" NAG_IFMT "\n\n", n);
  fflush(stdout);

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

  /* Read data array and write it out */
#define A(I, J) a[(J - 1) * pda + I - 1]
  for (i = 1; i <= m; i++)
    for (j = 1; j <= n; j++)
      scanf("%lf", &A(i, j));

  nag_file_print_matrix_real_gen_comp(
      order, matrix, diag, m, n, a, pda, "%8.4f",
      "Input Data     A :", Nag_NoLabels, 0, Nag_NoLabels, 0, 80, 0, 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 0;
    goto END;
  }

  /* nag_wav_dim2_init (c09abc).
   * Two-dimensional wavelet filter initialization
   */
  nag_wav_dim2_init(wavnamenum, Nag_MultiLevel, modenum, m, n, &nwlmax, &nf,
                    &nwct, &nwcn, icomm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_wav_dim2_init (c09abc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  lenc = nwct;
  if (!(c = NAG_ALLOC(lenc, double)) ||
      !(dwtlevm = NAG_ALLOC(nwlmax, Integer)) ||
      !(dwtlevn = NAG_ALLOC(nwlmax, Integer))) {
    printf("Allocation failure\n");
    exit_status = 2;
    goto END;
  }

  /* nag_wav_dim2_multi_fwd (c09ecc).
   * Two-dimensional multi-level discrete wavelet transform
   */
  nag_wav_dim2_multi_fwd(m, n, a, pda, lenc, c, nwlmax, dwtlevm, dwtlevn, icomm,
                         &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_wav_dim2_multi_fwd (c09ecc).\n%s\n", fail.message);
    exit_status = 3;
    goto END;
  }

  /* Print decomposition */
  printf("\n Length of wavelet filter : %12s%" NAG_IFMT " \n", "", nf);
  printf("\n Number of Levels : %" NAG_IFMT "\n", nwlmax);
  printf(" Number of coefficients in 1st dimension for each level :\n");
  for (j = 0; j < nwlmax; j++)
    printf("%8" NAG_IFMT "%s", dwtlevm[j], (j + 1) % 8 ? " " : "\n");

  printf("\n Number of coefficients in 2nd dimension for each level :\n");
  for (j = 0; j < nwlmax; j++)
    printf("%8" NAG_IFMT "%s", dwtlevn[j], (j + 1) % 8 ? " " : "\n");

  printf("\n Total number of wavelet coefficients : ");
  printf("%10" NAG_IFMT " \n\n", nwct);
  printf(" Wavelet coefficients c :  \n");
  for (j = 0; j < nwct; j++)
    printf("%8.4f%s", c[j], (j + 1) % 8 ? " " : "\n");
  printf("\n");

  /* Now select a nominated matrix of coefficients at a nominated level.
   * Remember that level 0 is input data, 1 first coeffs and so on up to nwlmax,
   * which is the deepest level and contains approx. coefficients.
   */
  want_level = nwlmax - 1;
  /* Print only vertical detail coeffs at selected level. */
  want_coeffs = 1;
  nwcm = dwtlevm[nwlmax - want_level];
  nwcn = dwtlevn[nwlmax - want_level];
  if (!(d = NAG_ALLOC(nwcm * nwcn, double))) {
    printf("Allocation failure\n");
    exit_status = 4;
    goto END;
  }

  /* nag_wav_dim2_coeff_ext (c09aec).
   * Extract the selected set of coefficients.
   */
  nag_wav_dim2_coeff_ext(want_level, want_coeffs, lenc, c, d, nwcm, icomm,
                         &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_wav_dim2_coeff_ext (c09aec).\n%s\n", fail.message);
    exit_status = 5;
    goto END;
  }

  /* Print out the selected coefficients */
  printf("\n");
  fflush(stdout);
  sprintf(title,
          "Type %" NAG_IFMT " coefficients at selected wavelet level "
          "%" NAG_IFMT " :",
          want_coeffs, want_level);
  nag_file_print_matrix_real_gen_comp(order, matrix, diag, nwcm, nwcn, d, nwcm,
                                      "%8.4f", title, Nag_NoLabels, 0,
                                      Nag_NoLabels, 0, 80, 0, 0, &fail);

  /* nag_wav_dim2_multi_inv (c09edc).
   * Two-dimensional inverse multi-level discrete wavelet transform
   */
  nag_wav_dim2_multi_inv(nwlmax, lenc, c, m, n, b, pdb, icomm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_wav_dim2_multi_inv (c09edc).\n%s\n", fail.message);
    exit_status = 6;
    goto END;
  }

  /* Print reconstruction */
  printf("\n");
  fflush(stdout);
  strcpy(title, "Reconstruction           B :");
  nag_file_print_matrix_real_gen_comp(order, matrix, diag, m, n, b, pdb,
                                      "%8.4f", title, Nag_NoLabels, 0,
                                      Nag_NoLabels, 0, 80, 0, 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 7;
    goto END;
  }
END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(c);
  NAG_FREE(d);
  NAG_FREE(dwtlevm);
  NAG_FREE(dwtlevn);
  return exit_status;
}