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

NAG CL Interface Introduction
Example description
/* nag_det_real_sym (f03bfc) Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.3, 2021.
 */
#include <math.h>
#include <nag.h>

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  Integer i, id, j, n, pda;
  double d;
  /* Arrays */
  char nag_enum_arg[40];
  double *a = 0;
  /* NAG types */
  NagError fail;
  Nag_UploType uplo;
  Nag_OrderType order;
  Nag_MatrixType matrix;
  Nag_DiagType diag = Nag_NonUnitDiag;

  printf("nag_det_real_sym (f03bfc) Example Program Results\n\n");

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

  if (!(a = NAG_ALLOC(n * n, double))) {
    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", &A(i, j));
  scanf("%*[^\n] ");
  scanf("%39s %*[^\n] ", nag_enum_arg);
  uplo = (Nag_UploType)nag_enum_name_to_value(nag_enum_arg);
  if (uplo == Nag_Lower) {
    matrix = Nag_LowerMatrix;
  } else {
    matrix = Nag_UpperMatrix;
  }

  INIT_FAIL(fail);

  /* nag_lapacklin_dpotrf (f07fdc)
   * Cholesky factorization of real symmetric positive definite matrix
   */
  nag_lapacklin_dpotrf(order, uplo, n, a, pda, &fail);
  if (fail.code != NE_NOERROR) {
    printf("%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_file_print_matrix_real_gen (x04cac)
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_file_print_matrix_real_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;
  }

  /* nag_det_real_sym (f03bfc)
   * determinant of factorized real symmetric positive definite matrix
   */
  nag_det_real_sym(order, n, a, pda, &d, &id, &fail);
  if (fail.code != NE_NOERROR) {
    printf("%s\n", fail.message);
    exit_status = 3;
    goto END;
  }

  printf("\nd = %12.5f  id = %12" NAG_IFMT "\n", d, id);
  printf("Value of determinant = %12.5e\n", d * pow(2.0, id));

END:
  NAG_FREE(a);

  return exit_status;
}