/* nag_ztfttp (f01vmc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 25, 2014.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf01.h>
#include <nagx04.h>

int main(void)
{
  /* Scalars */
  Integer       exit_status = 0, inc1 = 1, indent = 0, ncols = 80;
  Integer       i, j, k, lar1, lar2, lenap, lenar, mx, n, nx, pdar, q;
  /* Arrays */
  Complex       *ap = 0, *ar = 0;
  char          nag_enum_transr[40], nag_enum_uplo[40], form[] = "%5.2f";
  /* Nag Types */
  Nag_OrderType order;
  Nag_RFP_Store transr;
  Nag_UploType  uplo;
  NagError      fail;

#ifdef NAG_COLUMN_MAJOR
  order = Nag_ColMajor;
#define AR(I,J) ar[J*pdar + I]
#define KU(I,J,N) (I + J*(J+1)/2)
#define KL(I,J,N) (J*(N-1) - J*(J-1)/2 + I)
#else
  order = Nag_RowMajor;
#define AR(I,J) ar[I*pdar + J]
#define KL(I,J,N) (J + I*(I+1)/2)
#define KU(I,J,N) (I*(N-1) - I*(I-1)/2 + J)
#endif

  INIT_FAIL(fail);

  printf("nag_ztfttp (f01vmc) Example Program Results\n\n");
  /* Skip heading in data file*/
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &n);
  scanf("%39s ", nag_enum_transr);
  scanf("%39s  %*[^\n] ", nag_enum_uplo);
  lenap = (n * (n + 1))/2;
  lenar = lenap;
  if (!(ap = NAG_ALLOC(lenap, Complex)) || !(ar = NAG_ALLOC(lenar, Complex))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  transr = (Nag_RFP_Store) nag_enum_name_to_value(nag_enum_transr);
  uplo = (Nag_UploType) nag_enum_name_to_value(nag_enum_uplo);

  k = n/2;
  q = n - k;
  if (transr==Nag_RFP_Normal) {
    lar1 = 2*k+1;
    lar2 = q;
  } else {
    lar1 = q;
    lar2 = 2*k+1;
  }
  if (order==Nag_RowMajor) {
    pdar = lar2;
  } else {
    pdar = lar1;
  }
  /* Read an RFP matrix into array AR. */
  for (i = 0; i < lar1; i++) {
    for (j = 0; j < lar2; j++) 
      scanf(" ( %lf , %lf )", &AR(i,j).re, &AR(i,j).im);
  }

  /* Print the packed Rectangular Full Packed array */
  if (order==Nag_RowMajor) {
    mx = inc1;
    nx = lenar;
  } else {
    mx = lenar;
    nx = inc1;
  }
  fflush(stdout);
  nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, mx,
                              nx, ar, lenar, Nag_BracketForm, form,
                              "RFP Packed Array AR:", Nag_IntegerLabels, NULL,
                              Nag_NoLabels, NULL, ncols, indent, NULL, 
                              &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
           fail.message);
    exit_status = 1;
  }
  printf("\n");

  /* Convert real triangular matrix from Rectangular Full Packed to
   * packed vector form using nag_ztfttp (f01vmc).
   */
  nag_ztfttp(order, transr, uplo, n, ar, ap, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from  nag_ztfttp (f01vmc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print the packed vector */
  if (order==Nag_RowMajor) {
    mx = inc1;
    nx = lenap;
  } else {
    mx = lenap;
    nx = inc1;
  }
  fflush(stdout);
  nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, mx,
                              nx, ap, lenap, Nag_BracketForm, form,
                              "Packed Array AP:", Nag_IntegerLabels, NULL,
                              Nag_NoLabels, NULL, ncols, indent, NULL, 
                              &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
           fail.message);
    exit_status = 1;
  }
  printf("\n");

  /* Print the packed vector using macros KL or KU. */
  printf(" Packed Matrix AP (printed using KL/KU macros):\n\n");
  for (i = 0; i < n; i++) {
    printf("  ");
    if (uplo==Nag_Upper) {
      for (j = 0; j < i; j++) printf("%15s"," ");
      for (j = i; j < n; j++) 
        printf("  (%5.2f,%5.2f)", ap[KU(i,j,n)].re, ap[KU(i,j,n)].im);
    } else {
      for (j = 0; j <= i; j++)
        printf("  (%5.2f,%5.2f)", ap[KU(i,j,n)].re, ap[KU(i,j,n)].im);
    }
    printf("\n");
  }

 END:
  NAG_FREE(ap);
  NAG_FREE(ar);
  return exit_status;
}