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

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

int main(void)
{
  Integer exit_status = 0, i, j, m, n;
  NagError fail;
  double *u = 0, *v = 0, *x = 0;

  INIT_FAIL(fail);

  printf("nag_multiple_conjugate_hermitian (c06gqc) Example Program"
         " Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  while (scanf("%" NAG_IFMT "%" NAG_IFMT "", &m, &n) != EOF)
  {
    if (m >= 1 && n >= 1) {
      if (!(u = NAG_ALLOC(m * n, double)) ||
          !(v = NAG_ALLOC(m * n, double)) || !(x = NAG_ALLOC(m * n, double)))
      {
        printf("Allocation failure\n");
        exit_status = -1;
        goto END;
      }
    }
    else {
      printf("Invalid m or n.\n");
      exit_status = 1;
      return exit_status;
    }
    printf("\n\nm = %2" NAG_IFMT "  n = %2" NAG_IFMT "\n", m, n);
    /* Read in data and print out. */
    for (j = 0; j < m; ++j)
      for (i = 0; i < n; ++i)
        scanf("%lf", &x[j * n + i]);
    printf("\nOriginal data values\n\n");
    for (j = 0; j < m; ++j) {
      printf("    ");
      for (i = 0; i < n; ++i)
        printf("%10.4f%s", x[j * n + i],
               (i % 6 == 5 && i != n - 1 ? "\n     " : ""));
      printf("\n");
    }
    /* Convert Hermitian form data to full complex */
    /* nag_multiple_hermitian_to_complex (c06gsc).
     * Convert Hermitian sequences to general complex sequences
     */
    nag_multiple_hermitian_to_complex(m, n, x, u, v, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_multiple_hermitian_to_complex"
             " (c06gsc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

    printf("\nOriginal data written in full complex form\n\n");
    for (j = 0; j < m; ++j) {
      printf("Real");
      for (i = 0; i < n; ++i)
        printf("%10.4f%s", u[j * n + i],
               (i % 6 == 5 && i != n - 1 ? "\n     " : ""));
      printf("\nImag");
      for (i = 0; i < n; ++i)
        printf("%10.4f%s", v[j * n + i],
               (i % 6 == 5 && i != n - 1 ? "\n     " : ""));
      printf("\n\n");
    }
    /* Calculate conjugates of Hermitian data */
    /* nag_multiple_conjugate_hermitian (c06gqc).
     * Complex conjugate of multiple Hermitian sequences
     */
    nag_multiple_conjugate_hermitian(m, n, x, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_multiple_conjugate_hermitian (c06gqc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }

    printf("\nConjugated data values\n\n");
    for (j = 0; j < m; ++j) {
      printf("    ");
      for (i = 0; i < n; ++i)
        printf("%10.4f%s", x[j * n + i],
               (i % 6 == 5 && i != n - 1 ? "\n     " : ""));
      printf("\n");
    }
    /* Convert conjugated Hermitian data to full complex */
    /* nag_multiple_hermitian_to_complex (c06gsc), see above. */
    nag_multiple_hermitian_to_complex(m, n, x, u, v, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_multiple_hermitian_to_complex"
             " (c06gsc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

    printf("\nConjugated data written in full complex form\n\n");
    for (j = 0; j < m; ++j) {
      printf("Real");
      for (i = 0; i < n; ++i)
        printf("%10.4f%s", u[j * n + i],
               (i % 6 == 5 && i != n - 1 ? "\n     " : ""));
      printf("\nImag");
      for (i = 0; i < n; ++i)
        printf("%10.4f%s", v[j * n + i],
               (i % 6 == 5 && i != n - 1 ? "\n     " : ""));
      printf("\n\n");
    }
  END:
    NAG_FREE(u);
    NAG_FREE(v);
    NAG_FREE(x);
  }
  return exit_status;
}