/* nag_ztrttp (f01vbc) 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, i, j, pda;
  Integer        lenap, mx, n, nx;
  /* Arrays */
  Complex        *a = 0, *ap = 0;
  char           nag_enum_arg[40], form[] = "%5.2f";
  /* Nag Types */
  Nag_OrderType  order;
  Nag_UploType   uplo;
  Nag_MatrixType matrix;
  NagError       fail;

#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[J*pda + I]
  order = Nag_ColMajor;
#define KU(I,J,N) (I + J*(J+1)/2)
#define KL(I,J,N) (J*(N-1) - J*(J-1)/2 + I)
#else
#define A(I, J) a[I*pda + J]
  order = Nag_RowMajor;
#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_ztrttp (f01vbc) Example Program Results\n\n");
  /* Skip heading in data file*/
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &n);
  scanf("%39s  %*[^\n] ", nag_enum_arg);
  pda = n;
  lenap = (n * (n + 1))/2;
  if (!(a = NAG_ALLOC(pda*n, Complex)) || !(ap = NAG_ALLOC(lenap, Complex))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  uplo = (Nag_UploType) nag_enum_name_to_value(nag_enum_arg);
  /* Read a triangular matrix of order n. */
  if (uplo==Nag_Upper) {
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) scanf(" ( %lf , %lf ) ", &A(i, j).re,
                                     &A(i, j).im);
    }
  } else {
    for (i = 0; i < n; i++) {
      for (j = 0; j <= i; j++) scanf(" ( %lf , %lf ) ", &A(i, j).re,
                                     &A(i, j).im);
    }
  }

  /* Print the unpacked matrix. */
  matrix = (uplo == Nag_Upper ? Nag_UpperMatrix : Nag_LowerMatrix);
  /* nag_gen_complx_mat_print_comp (x04dbc).
   * Print complex general matrix (comprehensive).
   */
  fflush(stdout);
  nag_gen_complx_mat_print_comp(order, matrix, Nag_NonUnitDiag, n, n, a, pda,
                                Nag_BracketForm, form, "Unpacked Matrix A:",
                                Nag_IntegerLabels, NULL, Nag_IntegerLabels,
                                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;
    goto END;
  }
  printf("\n");

  /* Convert to  triangular matrix from full to packed vector form using
   * nag_ztrttp (f01vbc).
   */
  nag_ztrttp(order, uplo, n, a, pda, ap, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_ztrttp (f01vbc).\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*/
  printf(" Packed Array AP (printed using KL/KU macros):\n\n");
  for (i = 0; i < n; i++) {
    if (uplo==Nag_Upper) {
      printf("  ");
      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(a);
  NAG_FREE(ap);
  return exit_status;
}