/* nag_fft_multid_full (c06pjc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 7, 2002.
 */

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <nagc06.h>
#include <nagx04.h>

int main(void)
{
  /* Scalars */
  Integer  i, n, ndim;
  Integer  exit_status = 0;
  NagError fail;
  /* Arrays */
  Complex  *x = 0;
  Integer  *nd = 0;

  INIT_FAIL(fail);

  printf("nag_fft_multid_full (c06pjc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld%ld", &ndim, &n);
  if (n >= 1)
    {
      /* Allocate memory */
      if (!(x = NAG_ALLOC(n, Complex)) ||
          !(nd = NAG_ALLOC(ndim, Integer)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }

      for (i = 0; i < ndim; ++i)
        {
          scanf("%ld", &nd[i]);
        }
      /* Read in complex data and print out. */
      scanf("%*[^\n]");
      for (i = 0; i < n; ++i)
        {
          scanf(" ( %lf, %lf ) ", &x[i].re, &x[i].im);
        }
      scanf("%*[^\n]");
      printf("\n");
      /* nag_gen_complx_mat_print_comp (x04dbc).
       * Print complex general matrix (comprehensive)
       */
      fflush(stdout);
      nag_gen_complx_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix,
                                    Nag_NonUnitDiag, nd[0], n/nd[0], x, nd[0],
                                    Nag_BracketForm, "%6.3f",
                                    "Original data values", Nag_NoLabels, 0,
                                    Nag_NoLabels, 0, 80, 0, 0, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf(
                  "Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      /* Compute transform */
      /* nag_fft_multid_full (c06pjc).
       * Multi-dimensional complex discrete Fourier transform of
       * multi-dimensional data (using complex data type)
       */
      nag_fft_multid_full(Nag_ForwardTransform, ndim, nd, n, x, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_fft_multid_full (c06pjc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
      printf("\n");
      /* nag_gen_complx_mat_print_comp (x04dbc), see above. */
      fflush(stdout);
      nag_gen_complx_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix,
                                    Nag_NonUnitDiag, nd[0], n/nd[0], x, nd[0],
                                    Nag_BracketForm, "%6.3f",
                                    "Components of discrete Fourier transform",
                                    Nag_NoLabels, 0, Nag_NoLabels, 0, 80, 0,
                                    0, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf(
                  "Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      /* Compute inverse transform */
      /* nag_fft_multid_full (c06pjc), see above. */
      nag_fft_multid_full(Nag_BackwardTransform, ndim, nd, n, x, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_fft_multid_full (c06pjc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
      printf("\n");
      /* nag_gen_complx_mat_print_comp (x04dbc), see above. */
      fflush(stdout);
      nag_gen_complx_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix,
                                    Nag_NonUnitDiag, nd[0], n/nd[0], x, nd[0],
                                    Nag_BracketForm, "%6.3f",
                                    "Original data as restored by inverse "
                                    "transform", Nag_NoLabels, 0, Nag_NoLabels,
                                    0, 80, 0, 0, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf(
                  "Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
    }
  else
    {
      printf("\nInvalid value of n.\n");
    }
 END:
  NAG_FREE(x);
  NAG_FREE(nd);

  return exit_status;
}