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

NAG CL Interface Introduction
Example description
/* nag_lapacklin_zpstrf (f07krc) Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.3, 2021.
 */

#include <nag.h>

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  Integer i, j, n, pda, rank;
  double tol;
  /* Arrays */
  Complex *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_zpstrf (f07krc) 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, Complex)) || !(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 , %lf ) ", &A(i, j).re, &A(i, j).im);
  } else if (uplo == Nag_Lower) {
    matrix = Nag_LowerMatrix;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= i; j++)
        scanf(" ( %lf , %lf ) ", &A(i, j).re, &A(i, j).im);
  } else {
    printf("Invalid uplo.\n");
    exit_status = 1;
    goto END;
  }
  scanf("%*[^\n]");

  tol = -1.0;

  /* Factorize A using nag_lapacklin_zpstrf (f07krc) which performs a Cholesky
   * factorization of complex Hermitian positive semidefinite matrix.
   */
  nag_lapacklin_zpstrf(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) = nag_complex_create(0.0, 0.0);
    else if (uplo == Nag_Lower)
      for (j = rank + 1; j <= n; j++)
        for (i = j; i <= n; i++)
          A(i, j) = nag_complex_create(0.0, 0.0);
  } else if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapacklin_zpstrf (f07krc)\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_complex_gen_comp (x04dbc).
   * Print complex general matrix (comprehensive)
   */
  fflush(stdout);
  nag_file_print_matrix_complex_gen_comp(
      order, matrix, Nag_NonUnitDiag, n, n, a, n, Nag_BracketForm, "%5.2f",
      "Factor", Nag_IntegerLabels, NULL, Nag_IntegerLabels, NULL, 80, 0, 0,
      &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_complex_gen_comp (x04dbc).\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;
}