/* nag_fft_3d (c06pxc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

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

static Integer writex(Integer n1, Integer n2, Integer n3, Complex *x)
{
  /* Routine to print 3D matrix in 2D slices. */
  Integer k;
  NagError fail;

  INIT_FAIL(fail);

  for (k = 1; k <= n3; k++) {
    char title[30];
    sprintf(title, "X(i,j,k) for k = %" NAG_IFMT, k);
    fflush(stdout);
    nag_gen_complx_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix,
                                  Nag_NonUnitDiag, n1, n2,
                                  &x[(k - 1) * n1 * n2], n1, Nag_BracketForm,
                                  "%6.3f", title, Nag_NoLabels, 0,
                                  Nag_NoLabels, 0, 90, 0, 0, &fail);
    printf("\n");
  }
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
           fail.message);
    return 1;
  }
  return 0;
}

int main(void)
{
  /* Scalars */
  Integer i, j, k, n1, n2, n3;
  Integer exit_status = 0;
  NagError fail;
  /* Arrays */
  Complex *x = 0;
#define X(I, J, K) x[(K-1)*n2*n1 + (J-1)*n1 + I - 1]

  INIT_FAIL(fail);

  printf("nag_fft_3d (c06pxc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "", &n1, &n2, &n3);
  scanf("%*[^\n]");
  if (n1 * n2 * n3 >= 1) {
    /* Allocate memory */
    if (!(x = NAG_ALLOC(n1 * n2 * n3, Complex)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

    /* Read in complex data and print out. */
    for (k = 1; k <= n3; ++k) {
      for (i = 1; i <= n1; ++i) {
        for (j = 1; j <= n2; ++j) {
          scanf(" ( %lf, %lf ) ", &X(i, j, k).re, &X(i, j, k).im);
        }
      }
    }
    printf("\nOriginal data values\n\n");
    exit_status = writex(n1, n2, n3, x);
    if (exit_status != 0) {
      goto END;
    }
    /* Compute transform */
    /* nag_fft_3d (c06pxc).
     * Three-dimensional complex discrete Fourier transform,
     * complex data format
     */
    nag_fft_3d(Nag_ForwardTransform, n1, n2, n3, x, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_fft_3d (c06pxc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

    printf("\nComponents of discrete Fourier transforms\n\n");
    exit_status = writex(n1, n2, n3, x);
    if (exit_status != 0) {
      goto END;
    }
    /* Compute inverse transform */
    /* nag_fft_3d (c06pxc), see above. */
    nag_fft_3d(Nag_BackwardTransform, n1, n2, n3, x, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_fft_3d (c06pxc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
    printf("\nOriginal data as restored by inverse transform\n\n");
    exit_status = writex(n1, n2, n3, x);
  }
  else
    printf("\nInvalid value of n1, n2 or n3.\n");

END:
  NAG_FREE(x);

  return exit_status;
}