/* nag_fft_complex (c06ecc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 1, 1990.
 * Mark 8 revised, 2004.
 */

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

int main(void)
{
  Integer  exit_status = 0, j, n;
  NagError fail;
  double   *x = 0, *xx = 0, *y = 0, *yy = 0;

  INIT_FAIL(fail);

  printf("nag_fft_complex (c06ecc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  while (scanf("%ld", &n) != EOF)
    {
      if (n > 1)
        {
          if (!(x = NAG_ALLOC(n, double)) ||
              !(y = NAG_ALLOC(n, double)) ||
              !(xx = NAG_ALLOC(n, double)) ||
              !(yy = NAG_ALLOC(n, double)))
            {
              printf("Allocation failure\n");
              exit_status = -1;
              goto END;
            }
        }
      else
        {
          printf("Invalid n.\n");
          exit_status = 1;
          return exit_status;
        }
      for (j = 0; j < n; ++j)
        {
          scanf("%lf%lf", &x[j], &y[j]);
          xx[j] = x[j];
          yy[j] = y[j];
        }
      /* Compute transform */
      /* nag_fft_complex (c06ecc).
       * Single one-dimensional complex discrete Fourier transform
       */
      nag_fft_complex(n, x, y, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_fft_complex (c06ecc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
      printf("\nComponents of discrete Fourier transform\n\n");
      printf("         Real       Imag\n\n");
      for (j = 0; j < n; ++j)
        printf("%3ld %10.5f %10.5f\n", j, x[j], y[j]);
      /* Compute inverse transform */
      /* Conjugate the transform */
      /* nag_conjugate_complex (c06gcc).
       * Complex conjugate of complex sequence
       */
      nag_conjugate_complex(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;
        }
      /* Transform */
      /* nag_fft_complex (c06ecc), see above. */
      nag_fft_complex(n, x, y, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_fft_complex (c06ecc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
      /* Conjugate to give inverse transform */
      /* nag_conjugate_complex (c06gcc), see above. */
      nag_conjugate_complex(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 sequence as restored by inverse transform\n");
      printf("\n             Original                  Restored\n");
      printf("         Real       Imag           Real       Imag\n\n");
      for (j = 0; j < n; ++j)
        printf("%3ld %10.5f %10.5f     %10.5f %10.5f\n",
                j, xx[j], yy[j], x[j], y[j]);
 END:
      NAG_FREE(x);
      NAG_FREE(y);
      NAG_FREE(xx);
      NAG_FREE(yy);
    }
  return exit_status;
}