/* nag_fft_real (c06eac) 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, n2, nj;
  NagError fail;
  double   *a = 0, *b = 0, *x = 0, *xx = 0;

  INIT_FAIL(fail);

  printf("nag_fft_real (c06eac) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  while (scanf("%ld", &n) != EOF)
    {
      if (n > 1)
        {
          if (!(a = NAG_ALLOC(n, double)) ||
              !(b = NAG_ALLOC(n, double)) ||
              !(x = NAG_ALLOC(n, double)) ||
              !(xx = NAG_ALLOC(n, double)))
            {
              printf("Allocation failure\n");
              exit_status = -1;
              goto END;
            }
        }
      else
        {
          printf("Invalid n.\n");
          exit_status = 1;
        }
      for (j = 0; j < n; j++)
        {
          scanf("%lf", &x[j]);
          xx[j] = x[j];
        }
      /* Calculate transform */
      /* nag_fft_real (c06eac).
       * Single one-dimensional real discrete Fourier transform
       */
      nag_fft_real(n, x, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_fft_real (c06eac).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
      /* Calculate full complex form of Hermitian result */
      a[0] = x[0];
      b[0] = 0.0;
      n2 = (n-1)/2;
      for (j = 1; j <= n2; j++)
        {
          nj = n - j;
          a[j] = x[j];
          a[nj] = x[j];
          b[j] = x[nj];
          b[nj] = -x[nj];
        }
      if (n % 2 == 0)
        {
          a[n2+1] = x[n2+1];
          b[n2+1] = 0.0;
        }
      printf("\nComponents of discrete Fourier transform\n");
      printf("\n         Real       Imag \n\n");
      for (j = 0; j < n; j++)
        printf("%3ld %10.5f %10.5f\n", j, a[j], b[j]);
      /* Calculate inverse transform */
      /* nag_conjugate_hermitian (c06gbc).
       * Complex conjugate of Hermitian sequence
       */
      nag_conjugate_hermitian(n, x, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_conjugate_hermitian (c06gbc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
      /* nag_fft_hermitian (c06ebc).
       * Single one-dimensional Hermitian discrete Fourier
       * transform
       */
      nag_fft_hermitian(n, x, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_fft_hermitian (c06ebc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
      printf("\nOriginal sequence as restored by inverse transform\n");
      printf("\n       Original   Restored\n\n");
      for (j = 0; j < n; j++)
        printf("%3ld %10.5f %10.5f\n", j, xx[j], x[j]);
 END:
      NAG_FREE(a);
      NAG_FREE(b);
      NAG_FREE(x);
      NAG_FREE(xx);
    }
  return exit_status;
}