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

NAG CL Interface Introduction
Example description
/* nag_lapacklin_dppequ (f07gfc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */
#include <nag.h>
#include <stdio.h>

int main(void) {

  /* Scalars */
  double amax, big, scond, small;
  Integer exit_status = 0, i, j, n;

  /* Arrays */
  double *ap = 0, *s = 0;
  char nag_enum_arg[40];

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

#ifdef NAG_COLUMN_MAJOR
#define A_UPPER(I, J) ap[J * (J - 1) / 2 + I - 1]
#define A_LOWER(I, J) ap[(2 * n - J) * (J - 1) / 2 + I - 1]
  order = Nag_ColMajor;
#else
#define A_LOWER(I, J) ap[I * (I - 1) / 2 + J - 1]
#define A_UPPER(I, J) ap[(2 * n - I) * (I - 1) / 2 + J - 1]
  order = Nag_RowMajor;
#endif
  INIT_FAIL(fail);

  printf("nag_lapacklin_dppequ (f07gfc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%*[^\n]", &n);
  if (n < 0) {
    printf("Invalid n\n");
    exit_status = 1;
    goto END;
  }
  scanf(" %39s%*[^\n]", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  uplo = (Nag_UploType)nag_enum_name_to_value(nag_enum_arg);

  /* Allocate memory */
  if (!(ap = NAG_ALLOC(n * (n + 1) / 2, double)) ||
      !(s = NAG_ALLOC(n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read the upper or lower triangular part of the matrix A from data file */
  if (uplo == Nag_Upper)
    for (i = 1; i <= n; ++i)
      for (j = i; j <= n; ++j)
        scanf("%lf", &A_UPPER(i, j));
  else if (uplo == Nag_Lower)
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= i; ++j)
        scanf("%lf", &A_LOWER(i, j));
  scanf("%*[^\n]");

  /* Print the matrix A using nag_file_print_matrix_real_packed (x04ccc). */
  fflush(stdout);
  nag_file_print_matrix_real_packed(order, uplo, Nag_NonUnitDiag, n, ap,
                                    "Matrix A", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_packed (x04ccc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\n");

  /* Compute diagonal scaling factors using nag_lapacklin_dppequ (f07gfc). */
  nag_lapacklin_dppequ(order, uplo, n, ap, s, &scond, &amax, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapacklin_dppequ (f07gfc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print scond, amax and the scale factors */
  printf("scond = %10.1e, amax = %10.1e\n\n", scond, amax);
  printf("Diagonal scaling factors\n");
  for (i = 0; i < n; ++i)
    printf("%11.1e%s", s[i], i % 7 == 6 ? "\n" : " ");
  printf("\n\n");

  /* Compute values close to underflow and overflow using
   * nag_machine_real_safe (x02amc), nag_machine_precision (x02ajc) and
   * nag_machine_model_base (x02bhc)
   */
  small =
      nag_machine_real_safe / (nag_machine_precision * nag_machine_model_base);
  big = 1. / small;
  if (scond < 0.1 || amax < small || amax > big) {
    /* Scale A */
    if (uplo == Nag_Upper)
      for (j = 1; j <= n; ++j)
        for (i = 1; i <= j; ++i)
          A_UPPER(i, j) *= s[i - 1] * s[j - 1];
    else
      for (j = 1; j <= n; ++j)
        for (i = j; i <= n; ++i)
          A_LOWER(i, j) *= s[i - 1] * s[j - 1];

    /* Print the scaled matrix using nag_file_print_matrix_real_packed (x04ccc).
     */
    fflush(stdout);
    nag_file_print_matrix_real_packed(order, uplo, Nag_NonUnitDiag, n, ap,
                                      "Scaled matrix", 0, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_file_print_matrix_real_packed (x04ccc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  }
END:
  NAG_FREE(ap);
  NAG_FREE(s);

  return exit_status;
}

#undef A_UPPER
#undef A_LOWER