/* nag_sum_fft_real_3d (c06pyc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

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

int main(void)
{
  /* Scalars */
  Integer exit_status = 0, k, n1, n2, n3;
  /* Arrays */
  Complex *y = 0;
  double *x = 0;
  char title[30];
  /* Nag Types */
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_sum_fft_real_3d (c06pyc) Example Program Results\n");
  fflush(stdout);

  /* Read dimensions of array from data file. */
  scanf("%*[^\n] %" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &n1, &n2,
        &n3);

  if (!(x = NAG_ALLOC(n1 * n2 * n3, double)) ||
      !(y = NAG_ALLOC((n1 / 2 + 1) * n2 * n3, Complex)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read array values from data file and print out. */
  for (k = 0; k < n1 * n2 * n3; k++)
    scanf("%lf", &x[k]);

  printf("\nBelow we define X(i,j,k)=x[k*n1*n2+j*n1+i]");
  printf(" where i and j are the row and column \n");
  printf("indices of the matrices printed.");
  printf(" Y is defined similarly (but having n1/2+1 rows\n");
  printf("only due to conjugate symmetry).\n");

  printf("\n Original data values\n");
  fflush(stdout);
  for (k = 0; k < n3; k++) {
    sprintf(title, "\n  X(i,j,k) for k = %" NAG_IFMT, k);
    nag_gen_real_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix,
                                Nag_NonUnitDiag, n1, n2, &x[k * n1 * n2], n1,
                                "%6.3f", title, Nag_NoLabels, 0,
                                Nag_NoLabels, 0, 80, 0, 0, &fail);
  }
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Compute three-dimensional real-to-complex discrete Fourier transform using
   * nag_sum_fft_real_3d (c06pyc) and print out.
   */
  nag_sum_fft_real_3d(n1, n2, n3, x, y, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sum_fft_real_3d (c06pyc).\n%s\n", fail.message);
    exit_status = 2;
    goto END;
  }

  printf("\n Components of discrete Fourier transform\n");
  fflush(stdout);
  for (k = 0; k < n3; k++) {
    sprintf(title, "\n  Y(i,j,k) for k = %" NAG_IFMT, k);
    /* nag_gen_complx_mat_print_comp (x04dbc).
     * Print complex general matrix (comprehensive) */
    nag_gen_complx_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix,
                                  Nag_NonUnitDiag, n1 / 2 + 1, n2,
                                  &y[k * (n1 / 2 + 1) * n2], n1 / 2 + 1,
                                  Nag_BracketForm, "%6.3f", title,
                                  Nag_NoLabels, 0, Nag_NoLabels, 0, 90, 0, 0,
                                  &fail);
  }
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
           fail.message);
    exit_status = 3;
    goto END;
  }

  /* Compute three-dimensional complex-to-real discrete Fourier transform using
   * nag_sum_fft_hermitian_3d (c06pzc) and print out.
   */
  nag_sum_fft_hermitian_3d(n1, n2, n3, y, x, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sum_fft_hermitian_3d (c06pzc).\n%s\n",
           fail.message);
    exit_status = 4;
    goto END;
  }

  printf("\n Original sequence as restored by inverse transform\n");
  fflush(stdout);
  for (k = 0; k < n3; k++) {
    sprintf(title, "\n  X(i,j,k) for k = %" NAG_IFMT, k);
    /* nag_gen_real_mat_print_comp (x04cbc).
     * Print out a real matrix (comprehensive) */
    nag_gen_real_mat_print_comp(Nag_ColMajor, Nag_GeneralMatrix,
                                Nag_NonUnitDiag, n1, n2, &x[k * n1 * n2], n1,
                                "%6.3f", title, Nag_NoLabels, 0,
                                Nag_NoLabels, 0, 80, 0, 0, &fail);
  }
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 5;
    goto END;
  }

END:
  NAG_FREE(x);
  NAG_FREE(y);
  return exit_status;
}