/* nag_conjugate_hermitian (c06gbc) 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;

  INIT_FAIL(fail);

  printf("nag_conjugate_hermitian (c06gbc) 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)))
            {
              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]);
      /* Calculate the Fourier transform of data */
      /* 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 conjugates of Hermitian result to */
      /* give inverse tranform */
      /* 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;
        }

      /* Expand conjugated Hermitian sequence to full complex */
      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 inverse 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]);
 END:
      NAG_FREE(a);
      NAG_FREE(b);
      NAG_FREE(x);
    }
  return exit_status;
}