/* nag_fft_hermitian (c06ebc) 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   *u = 0, *v = 0, *x = 0, *xx = 0;

  INIT_FAIL(fail);

  printf("nag_fft_hermitian (c06ebc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  while (scanf("%ld", &n) != EOF)
    {
      if (n > 1)
        {
          if (!(u = NAG_ALLOC(n, double)) ||
              !(v = 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;
          return exit_status;
        }

      for (j = 0; j < n; j++)
        {
          scanf("%lf", &x[j]);
          xx[j] = x[j];
        }
      /* Calculate full complex form of Hermitian sequence */
      u[0] = x[0];
      v[0] = 0.0;
      n2 = (n-1)/2;
      for (j = 1; j <= n2; j++)
        {
          nj = n - j;
          u[j] = x[j];
          u[nj] = x[j];
          v[j] = x[nj];
          v[nj] = -x[nj];
        }
      if (n % 2 == 0)
        {
          u[n2+1] = x[n2+1];
          v[n2+1] = 0.0;
        }
      printf("\nOriginal and corresponding complex sequence\n");
      printf("\n         Data       Real       Imag \n\n");
      for (j = 0; j < n; j++)
        printf("%3ld %10.5f %10.5f %10.5f\n", j, x[j], u[j], v[j]);
      /* Calculate transform */
      /* 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("\nComponents of discrete Fourier transform\n\n");
      for (j = 0; j < n; j++)
        printf("%3ld %10.5f\n", j, x[j]);
      /* Calculate inverse 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;
        }
      /* 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;
        }
      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(u);
      NAG_FREE(v);
      NAG_FREE(x);
      NAG_FREE(xx);
    }
  return exit_status;
}