/* nag_fft_2d_complex (c06fuc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 2 revised, 1992.
 * Mark 8 revised, 2004.
 */

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

int main(void)
{
  Integer  exit_status = 0, i, j, m, n;
  NagError fail;
  double   *trigm = 0, *trign = 0, *x = 0, *y = 0;

  INIT_FAIL(fail);

  printf("nag_fft_2d_complex (c06fuc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  while (scanf("%ld%ld", &m, &n) != EOF)
    {
      if (m*n >= 1)
        {
          if (!(trigm = NAG_ALLOC(2*m, double)) ||
              !(trign = NAG_ALLOC(2*n, double)) ||
              !(x = NAG_ALLOC(m*n, double)) ||
              !(y = NAG_ALLOC(m*n, double)))
            {
              printf("Allocation failure\n");
              exit_status = -1;
              goto END;
            }
        }
      else
        {
          printf("Invalid m or n.\n");
          exit_status = 1;
          return exit_status;
        }
      printf("\n\nm = %2ld  n = %2ld\n", m, n);
      /* Read in complex data and print out. */
      for (j = 0; j < m; ++j)
        {
          for (i = 0; i < n; ++i)
            scanf("%lf", &x[j*n + i]);
          for (i = 0; i < n; ++i)
            scanf("%lf", &y[j*n + i]);
        }
      printf("\nOriginal data values\n\n");
      for (j = 0; j < m; ++j)
        {
          printf("Real");
          for (i = 0; i < n; ++i)
            printf("%10.4f%s", x[j*n + i],
                    (i%6 == 5 && i != n-1?"\n     ":""));
          printf("\nImag");
          for (i = 0; i < n; ++i)
            printf("%10.4f%s", y[j*n + i],
                    (i%6 == 5 && i != n-1?"\n     ":""));
          printf("\n\n");
        }
      /* Initialize trig arrays */
      /* nag_fft_init_trig (c06gzc).
       * Initialization function for other c06 functions
       */
      nag_fft_init_trig(m, trigm, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_fft_init_trig (c06gzc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      /* nag_fft_init_trig (c06gzc), see above. */
      nag_fft_init_trig(n, trign, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_fft_init_trig (c06gzc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      /* Compute transform */
      /* nag_fft_2d_complex (c06fuc).
       * Two-dimensional complex discrete Fourier transform
       */
      nag_fft_2d_complex(m, n, x, y, trigm, trign, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_fft_2d_complex (c06fuc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      printf("\nComponents of discrete Fourier transforms\n\n");
      for (j = 0; j < m; ++j)
        {
          printf("Real");
          for (i = 0; i < n; ++i)
            printf("%10.4f%s", x[j*n + i],
                    (i%6 == 5 && i != n-1?"\n     ":""));
          printf("\nImag");
          for (i = 0; i < n; ++i)
            printf("%10.4f%s", y[j*n + i],
                    (i%6 == 5 && i != n-1?"\n     ":""));
          printf("\n\n");
        }
      /* Compute inverse transform */
      /* nag_conjugate_complex (c06gcc).
       * Complex conjugate of complex sequence
       */
      nag_conjugate_complex(m*n, y, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_conjugate_complex (c06gcc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      /* nag_fft_2d_complex (c06fuc), see above. */
      nag_fft_2d_complex(m, n, x, y, trigm, trign, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_fft_2d_complex (c06fuc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      /* nag_conjugate_complex (c06gcc), see above. */
      nag_conjugate_complex(m*n, y, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_conjugate_complex (c06gcc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      printf("\nOriginal data as restored by inverse transform\n\n");
      for (j = 0; j < m; ++j)
        {
          printf("Real");
          for (i = 0; i < n; ++i)
            printf("%10.4f%s", x[j*n + i],
                    (i%6 == 5 && i != n-1?"\n     ":""));
          printf("\nImag");
          for (i = 0; i < n; ++i)
            printf("%10.4f%s", y[j*n + i],
                    (i%6 == 5 && i != n-1?"\n     ":""));
          printf("\n\n");
        }
 END:
      NAG_FREE(trigm);
      NAG_FREE(trign);
      NAG_FREE(x);
      NAG_FREE(y);
    }
  return exit_status;
}