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

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

#include <nag.h>

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  Integer i, j, n, pda, rank;
  double tol;
  /* Arrays */
  double *a = 0;
  Integer *piv = 0;
  char nag_enum_arg[40];
  /* Nag Types */
  Nag_UploType uplo;
  Nag_OrderType order;
  Nag_MatrixType matrix;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_lapacklin_dpstrf (f07kdc) Example Program Results\n");
  /* Skip heading in data file and retrieve data */
  scanf("%*[^\n]%" NAG_IFMT "%39s%*[^\n]", &n, nag_enum_arg);
  uplo = (Nag_UploType)nag_enum_name_to_value(nag_enum_arg);
  if (!(a = NAG_ALLOC(n * n, double)) || !(piv = NAG_ALLOC(n, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

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

  /* Read triangular part of A from data file */
  if (uplo == Nag_Upper) {
    matrix = Nag_UpperMatrix;
    for (i = 1; i <= n; i++)
      for (j = i; j <= n; j++)
        scanf("%lf", &A(i, j));
  } else if (uplo == Nag_Lower) {
    matrix = Nag_LowerMatrix;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= i; j++)
        scanf("%lf", &A(i, j));
  } else {
    printf("Invalid uplo.\n");
    exit_status = 1;
    goto END;
  }
  scanf("%*[^\n]");

  tol = -1.0;

  /* Factorize A using nag_lapacklin_dpstrf (f07kdc) which performs a Cholesky
   * factorization of real symmetric positive semidefinite matrix.
   */
  nag_lapacklin_dpstrf(order, uplo, n, a, pda, piv, &rank, tol, &fail);

  if (fail.code == NW_NOT_POS_DEF) {
    /* A is not of full rank.
     * Zero out columns rank+1 to n.
     */
    if (uplo == Nag_Upper)
      for (j = rank + 1; j <= n; j++)
        for (i = rank + 1; i <= j; i++)
          A(i, j) = 0.0;
    else if (uplo == Nag_Lower)
      for (j = rank + 1; j <= n; j++)
        for (i = j; i <= n; i++)
          A(i, j) = 0.0;
  } else if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapacklin_dpstrf (f07kdc)\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print rank of A. */
  printf("\nComputed rank: %" NAG_IFMT "\n\n", rank);

  /* Print factorization using
   * nag_file_print_matrix_real_gen (x04cac).
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_file_print_matrix_real_gen(order, matrix, Nag_NonUnitDiag, n, n, a, pda,
                                 "Factor", 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;
    goto END;
  }

  /* Print pivot indices. */
  printf("\nPivots:\n");
  for (i = 0; i < n; i++)
    printf("%11" NAG_IFMT "", piv[i]);
  printf("\n");

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