NAG Library Manual, Mark 27.2
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_matop_dtpttf (f01vjc) Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.2, 2021.
 */

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

int main(void) {
  /* Scalars */
  Integer exit_status = 0, indent = 0, ncols = 80;
  Integer i, j, k, lar1, lar2, lenap, lenar, n, pdar, q;
  /* Arrays */
  double *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 KU(I, J) (I + J * (J + 1) / 2)
#define KL(I, J) (J * (n - 1) - J * (J - 1) / 2 + I)
#else
  order = Nag_RowMajor;
#define KL(I, J) (J + I * (I + 1) / 2)
#define KU(I, J) (I * (n - 1) - I * (I - 1) / 2 + J)
#endif

  INIT_FAIL(fail);

  printf("nag_matop_dtpttf (f01vjc) 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, double)) || !(ar = NAG_ALLOC(lenar, double))) {
    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);

  /* Read and print the packed vector ap using macros KL or KU. */
  printf(" Packed Array ap (printed using KL/KU macros):\n\n");
  for (i = 0; i < n; i++) {
    if (uplo == Nag_Upper) {
      for (j = i; j < n; j++)
        scanf("%lf ", &ap[KU(i, j)]);
      for (j = 0; j < i; j++)
        printf("%6s", " ");
      for (j = i; j < n; j++)
        printf("%6.2f", ap[KU(i, j)]);
    } else {
      for (j = 0; j <= i; j++)
        scanf("%lf ", &ap[KL(i, j)]);
      for (j = 0; j <= i; j++)
        printf("%6.2f", ap[KL(i, j)]);
    }
    printf("\n");
  }
  printf("\n");

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

  /* Print the Rectangular Full Packed array
   * showing how the elements are arranged.
   */
  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;
  }
  /* nag_file_print_matrix_real_gen_comp (x04cbc).
   * Print real general matrix (comprehensive).
   */
  fflush(stdout);
  nag_file_print_matrix_real_gen_comp(
      order, Nag_GeneralMatrix, Nag_NonUnitDiag, lar1, lar2, ar, pdar, form,
      "RFP Packed Array AR "
      "(structural representation):",
      Nag_IntegerLabels, NULL, Nag_IntegerLabels, NULL, ncols, indent, NULL,
      &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 1;
  }

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