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

NAG CL Interface Introduction
Example description
/* nag_blast_ztfsm (f16zlc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 */

#include <nag.h>

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  Complex alpha;
  Integer i, j, m, n, pda, pdb;
  /* Arrays */
  Complex *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_ztfsm (f16zlc) 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, Complex)) ||
      !(ar = NAG_ALLOC((m * (m + 1)) / 2, Complex)) ||
      !(b = NAG_ALLOC(m * n, Complex))) {
    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 , %lf ) %*[^\n] ", &alpha.re, &alpha.im);
  /* 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 , %lf ) ", &A(i, j).re, &A(i, j).im);
      }
    }
  } else {
    for (i = 1; i <= m; i++) {
      for (j = i; j <= m; j++) {
        scanf(" ( %lf , %lf ) ", &A(i, j).re, &A(i, j).im);
      }
    }
  }
  scanf("%*[^\n] ");

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

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

  /* Print the result using easy-to-use complex general matrix printing routine
   * nag_file_print_matrix_complex_gen (x04dac).
   */
  fflush(stdout);
  nag_file_print_matrix_complex_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_complex_gen (x04dac).\n%s\n",
           fail.message);
    exit_status = 1;
  }

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