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

NAG CL Interface Introduction
Example description
/* nag_det_complex_gen (f03bnc) Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 *
 * Mark 29.3, 2023.
 */
#include <math.h>
#include <nag.h>

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  Integer i, j, n, pda;
  Complex d;
  /* Arrays */
  Integer *ipiv = 0;
  Integer id[2];
  Complex *a = 0;
  /* NAG types */
  NagError fail;
  Nag_OrderType order;
  Nag_MatrixType matrix = Nag_GeneralMatrix;
  Nag_DiagType diag = Nag_NonUnitDiag;

  printf("nag_det_complex_gen (f03bnc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%*[^\n]", &n);
  pda = n;

  if (!(a = NAG_ALLOC((n) * (n), Complex)) ||
      !(ipiv = NAG_ALLOC((n), Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Define matrix element A_ij in terms of elements of array a[k] */
#ifdef NAG_COLUMN_MAJOR
  order = Nag_ColMajor;
#define A(I, J) a[(J - 1) * pda + (I - 1)]
#else
  order = Nag_RowMajor;
#define A(J, I) a[(J - 1) * pda + (I - 1)]
#endif
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      scanf(" ( %lf , %lf ) ", &A(i, j).re, &A(i, j).im);
  scanf("%*[^\n] ");

  INIT_FAIL(fail);
  /* Factorize A using nag_lapacklin_zgetrf (f07arc)
   * LU factorization of complex m by n matrix
   */
  nag_lapacklin_zgetrf(order, n, n, a, pda, ipiv, &fail);
  if (fail.code != NE_NOERROR) {
    printf("%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_file_print_matrix_complex_gen (x04dac)
   * Print complex general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_file_print_matrix_complex_gen(order, matrix, diag, n, n, a, pda,
                                    "Array A after factorization", NULL, &fail);
  if (fail.code != NE_NOERROR) {
    printf("%s\n", fail.message);
    exit_status = 2;
    goto END;
  }

  printf("Pivots:\n  ");
  for (j = 0; j < n; j++)
    printf("%12" NAG_IFMT, ipiv[j]);
  printf("\n");

  /* nag_det_complex_gen (f03bnc) - Determinant of complex matrix */
  nag_det_complex_gen(order, n, a, pda, ipiv, &d, id, &fail);
  if (fail.code != NE_NOERROR) {
    printf("%s\n", fail.message);
    exit_status = 3;
    goto END;
  }

  printf("d = (%9.5f, %9.5f)    id = (%2" NAG_IFMT ", %2" NAG_IFMT ")\n", d.re,
         d.im, id[0], id[1]);
  printf("Value of determinant = (%12.5e, %12.5e)\n", pow(2.0, id[0]) * (d.re),
         pow(2.0, id[1]) * (d.im));

END:
  NAG_FREE(a);
  NAG_FREE(ipiv);

  return exit_status;
}