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

NAG CL Interface Introduction
Example description
/* nag_correg_corrmat_h_weight (g02ajc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.3, 2024.
 */

#include <nag.h>
#include <stdio.h>

int main(void) {

#define G(I, J) g[(J - 1) * pdg + I - 1]
#define H(I, J) h[(J - 1) * pdh + I - 1]

  /*  Scalars */
  Integer exit_status = 0;
  Integer one = 1;
  double alpha, errtol, norm;
  Integer i, j, iter, maxit, n, pdg, pdh, pdx;

  /*  Arrays */
  double *eig = 0, *g = 0, *h = 0, *x = 0;

  /* Nag Types */
  Nag_OrderType order;
  NagError fail;

  INIT_FAIL(fail);

  /* Output preamble */
  printf("nag_correg_corrmat_h_weight (g02ajc)");
  printf(" Example Program Results\n\n");
  fflush(stdout);

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Read in the problem size and alpha */
  scanf("%" NAG_IFMT "%lf%*[^\n] ", &n, &alpha);

  pdg = n;
  pdh = n;
  pdx = n;
  if (!(g = NAG_ALLOC(pdg * n, double)) || !(h = NAG_ALLOC(pdh * n, double)) ||
      !(x = NAG_ALLOC(pdx * n, double)) || !(eig = NAG_ALLOC(n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read in the matrix g */
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      scanf("%lf", &G(i, j));
  scanf("%*[^\n] ");

  /* Read in the matrix h */
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      scanf("%lf", &H(i, j));
  scanf("%*[^\n] ");

  /* Use the defaults for ERRTOL and MAXIT */
  errtol = 0.0;
  maxit = 0;

  /*
   * nag_correg_corrmat_h_weight (g02ajc).
   * Calculate nearest correlation matrix with element-wise weighting
   */
  nag_correg_corrmat_h_weight(g, pdg, n, alpha, h, pdh, errtol, maxit, x, pdx,
                              &iter, &norm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_correg_corrmat_h_weight (g02ajc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Display results */

  order = Nag_ColMajor;
  /*
   * nag_file_print_matrix_real_gen (x04cac).
   * Prints real general matrix
   */
  nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
                                 n, h, pdh, "Returned h Matrix", NULL, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (x04cac).\n%s\n",
           fail.message);
    exit_status = 2;
    goto END;
  }

  printf("\n");
  fflush(stdout);
  nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
                                 n, x, pdx, "Nearest Correlation Matrix x",
                                 NULL, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (x04cac).\n%s\n",
           fail.message);
    exit_status = 3;
    goto END;
  }

  printf("\n%s %11" NAG_IFMT " \n\n", "Number of iterations:", iter);
  printf("Norm value:%27.4f \n\n", norm);
  printf("%s %30.4f \n", "alpha: ", alpha);

  /*
   * nag_lapackeig_dsyev (f08fac).
   * Computes all eigenvalues and, optionally, eigenvectors of a real
   * symmetric matrix
   */
  nag_lapackeig_dsyev(order, Nag_EigVals, Nag_Upper, n, x, pdx, eig, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dsyev (f08fac).\n%s\n", fail.message);
    exit_status = 4;
    goto END;
  }

  printf("\n");
  fflush(stdout);
  /*
   * nag_file_print_matrix_real_gen (x04cac).
   * Print real general matrix (easy-to-use)
   */
  nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, one,
                                 n, eig, one, "Eigenvalues of x", NULL, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (x04cac).\n%s\n",
           fail.message);
    exit_status = 5;
    goto END;
  }

END:
  NAG_FREE(eig);
  NAG_FREE(g);
  NAG_FREE(h);
  NAG_FREE(x);
  return exit_status;
}