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

NAG CL Interface Introduction
Example description
/* nag_blast_dtfsm (f16ylc) Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 *
 * Mark 29.2, 2023.
 */

#include <nag.h>

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  double alpha;
  Integer i, j, m, n, pda, pdb;
  /* Arrays */
  double *a = 0, *ar = 0, *b = 0;
  char nag_enum_arg[40];
  /* Nag Types */
  Nag_OrderType order;
  Nag_RFP_Store transr;
  Nag_SideType side;
  Nag_UploType uplo;
  Nag_TransType trans;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_blast_dtfsm (f16ylc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &m, &n);
  pda = m;

#ifdef NAG_COLUMN_MAJOR
  order = Nag_ColMajor;
  pdb = m;
#define A(I, J) a[(J - 1) * pda + I - 1]
#define B(I, J) b[(J - 1) * pdb + I - 1]
#else
  order = Nag_RowMajor;
  pdb = n;
#define A(I, J) a[(I - 1) * pda + J - 1]
#define B(I, J) b[(I - 1) * pdb + J - 1]
#endif

  if (!(a = NAG_ALLOC(pda * m, double)) ||
      !(ar = NAG_ALLOC((m * (m + 1)) / 2, double)) ||
      !(b = NAG_ALLOC(m * n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Nag_RFP_Store */
  scanf("%39s ", nag_enum_arg);
  transr = (Nag_RFP_Store)nag_enum_name_to_value(nag_enum_arg);
  /* Nag_SideType */
  scanf("%39s  %*[^\n] ", nag_enum_arg);
  side = (Nag_SideType)nag_enum_name_to_value(nag_enum_arg);
  /* Nag_UploType */
  scanf("%39s ", nag_enum_arg);
  uplo = (Nag_UploType)nag_enum_name_to_value(nag_enum_arg);
  /* Nag_TransType */
  scanf("%39s  %*[^\n] ", nag_enum_arg);
  trans = (Nag_TransType)nag_enum_name_to_value(nag_enum_arg);
  scanf("%lf%*[^\n] ", &alpha);
  /* Read upper or lower triangle of matrix A from data file */
  if (uplo == Nag_Lower) {
    for (i = 1; i <= m; i++) {
      for (j = 1; j <= i; j++) {
        scanf("%lf", &A(i, j));
      }
    }
  } else {
    for (i = 1; i <= m; i++) {
      for (j = i; j <= m; j++) {
        scanf("%lf", &A(i, j));
      }
    }
  }
  scanf("%*[^\n] ");

  /* Read matrix B from data file */
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      scanf("%lf", &B(i, j));
    }
  }
  /* Convert real triangular matrix A from full to rectangular full packed
   * storage format (stored in ar) using nag_matop_dtrttf (f01vec).
   */
  nag_matop_dtrttf(order, transr, uplo, m, a, pda, ar, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_matop_dtrttf (f01vec).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n");
  /* Solve AX = B, where real triangular matrix A is stored using RFP format
   * in ar, using nag_blast_dtfsm (f16ylc).
   */
  nag_blast_dtfsm(order, transr, side, uplo, trans, Nag_NonUnitDiag, m, n,
                  alpha, ar, b, pdb, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_blast_dtfsm (f16ylc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print the result using easy-to-use real general matrix printing routine
   * nag_file_print_matrix_real_gen (x04cac).
   */
  fflush(stdout);
  nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m,
                                 n, b, pdb, "The Solution", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (x04cac).\n%s\n",
           fail.message);
    exit_status = 1;
  }

END:
  NAG_FREE(a);
  NAG_FREE(ar);
  NAG_FREE(b);
  return exit_status;
}