/* nag_zpstrf (f07krc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

#include <nag.h>
#include <nag_stdlib.h>
#include <naga02.h>
#include <nagf07.h>
#include <nagx04.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_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_zpstrf (f07krc) which performs a Cholesky
   * factorization of complex Hermitian positive semidefinite matrix.
   */
  nag_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(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(0.0, 0.0);
  }
  else if (fail.code != NE_NOERROR) {
    printf("Error from nag_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_gen_complx_mat_print_comp (x04dbc).
   * Print complex general matrix (comprehensive)
   */
  fflush(stdout);
  nag_gen_complx_mat_print_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_gen_complx_mat_print_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;
}