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

NAG CL Interface Introduction
Example description
/* nag_matop_dtrttp (f01vac) Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 *
 * Mark 29.0, 2023.
 */

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

int main(void) {
  /* Scalars */
  Integer exit_status = 0, indent = 0, ncols = 80, i, j, pda;
  Integer lenap, n;
  /* Arrays */
  double *a = 0, *ap = 0;
  char nag_enum_arg[40], form[] = "%5.2f";
  /* Nag Types */
  Nag_MatrixType matrix;
  Nag_OrderType order;
  Nag_UploType uplo;
  NagError fail;

#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[J * pda + I]
  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
#define A(I, J) a[I * pda + J]
  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_dtrttp (f01vac) 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, double)) || !(ap = NAG_ALLOC(lenap, double))) {
    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. */
  for (i = 0; i < n; i++) {
    if (uplo == Nag_Upper) {
      for (j = i; j < n; j++)
        scanf("%lf", &A(i, j));
      scanf("%*[^\n] ");
    } else {
      for (j = 0; j <= i; j++)
        scanf("%lf", &A(i, j));
      scanf("%*[^\n] ");
    }
  }

  /* Print the unpacked matrix. */
  matrix = (uplo == Nag_Upper ? Nag_UpperMatrix : Nag_LowerMatrix);

  /* nag_file_print_matrix_real_gen_comp (x04cbc).
   * Print real general matrix (comprehensive).
   */
  fflush(stdout);
  nag_file_print_matrix_real_gen_comp(
      order, matrix, Nag_NonUnitDiag, n, n, a, pda, form,
      "Unpacked Matrix A:", 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;
    goto END;
  }
  printf("\n");

  /* Convert to triangular matrix from full to packed vector form using
   * nag_matop_dtrttp (f01vac).
   */
  nag_matop_dtrttp(order, uplo, n, a, pda, ap, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_matop_dtrttp (f01vac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print the packed vector using macros KL or KU. */
  printf(" Packed Array 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("%6s", " ");
      for (j = i; j < n; j++)
        printf("%6.2f", ap[KU(i, j)]);
    } else {
      for (j = 0; j <= i; j++)
        printf("%6.2f", ap[KL(i, j)]);
    }
    printf("\n");
  }

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