/* nag_sum_fft_complex_1d (c06pcc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 24, 2013.
 */

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

int main(void)
{
  /* Scalars */
  Integer   exit_status = 0, i, n;
  /* Arrays */
  Complex   *x = 0, *z = 0, *x_back = 0;
  /* Nag Types */
  NagError  fail;

  INIT_FAIL(fail);

  printf("nag_sum_fft_complex_1d (c06pcc) Example Program Results\n");

  /* Read dimensions of array and array values from data file. */
  scanf("%*[^\n] %ld%*[^\n]", &n);
  if (!(x = NAG_ALLOC(n, Complex)) ||
      !(z = NAG_ALLOC(n, Complex)) ||
      !(x_back = NAG_ALLOC(n, Complex)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  for (i = 0; i < n; ++i) {
    scanf(" ( %lf, %lf )", &x[i].re, &x[i].im);
    z[i] = x[i];
  }

  /* Compute discrete Fourier transform of complex array x using
   * nag_sum_fft_complex_1d (c06pcc).
   */
  nag_sum_fft_complex_1d(Nag_ForwardTransform, z, n, &fail);
   if (fail.code != NE_NOERROR)
     {
       printf("Error from nag_sum_fft_complex_1d (c06pcc).\n%s\n",
              fail.message);
       exit_status = 1;
       goto END;
     }

   for (i = 0; i < n; ++i)
     x_back[i] = z[i];

   /* Compute inverse discrete Fourier transform of complex array z using
    * nag_sum_fft_complex_1d (c06pcc).
    */
   nag_sum_fft_complex_1d(Nag_BackwardTransform, x_back, n, &fail);
   if (fail.code != NE_NOERROR)
     {
       printf("Error from nag_sum_fft_complex_1d (c06pcc).\n%s\n",
              fail.message);
       exit_status = 2;
       goto END;
     }
   
   printf("\n%2s%13s%28s%22s\n","i","x","z = FFT(x)","InvFFT(z)");
   for (i=0;i<n;i++)
     printf("%2ld (%8.5f,  %8.5f ) (%8.5f,  %8.5f ) (%8.5f,  %8.5f )\n",
            i, x[i].re, x[i].im, z[i].re, z[i].im, x_back[i].re, x_back[i].im);
 END:
  NAG_FREE(x);
  NAG_FREE(z);
  NAG_FREE(x_back);

  return exit_status;
}