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

NAG CL Interface Introduction
Example description
/* nag_lapacklin_zgbequ (f07btc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */

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

int main(void) {

  /* Scalars */
  double amax, big, colcnd, rowcnd, small;
  Integer exit_status = 0, i, j, kl, ku, n, pdab;

  /* Arrays */
  Complex *ab = 0;
  double *c = 0, *r = 0;

  /* Nag Types */
  NagError fail;
  Nag_OrderType order;
  Nag_Boolean scaled = Nag_FALSE;

#ifdef NAG_COLUMN_MAJOR
#define AB(I, J) ab[(J - 1) * pdab + ku + I - J]
  order = Nag_ColMajor;
#else
#define AB(I, J) ab[(I - 1) * pdab + kl + J - I]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapacklin_zgbequ (f07btc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &n, &kl, &ku);
  if (n < 0 || kl < 0 || ku < 0) {
    printf("Invalid n or kl or ku\n");
    exit_status = 1;
    goto END;
  }
  /* Allocate memory */
  if (!(ab = NAG_ALLOC((kl + ku + 1) * n, Complex)) ||
      !(c = NAG_ALLOC(n, double)) || !(r = NAG_ALLOC(n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  pdab = kl + ku + 1;

  /* Read the band matrix A from data file */
  for (i = 1; i <= n; ++i)
    for (j = MAX(i - kl, 1); j <= MIN(i + ku, n); ++j)
      scanf(" ( %lf , %lf )", &AB(i, j).re, &AB(i, j).im);
  scanf("%*[^\n]");

  /* Print the matrix A using nag_file_print_matrix_complex_band (x04dec). */
  fflush(stdout);
  nag_file_print_matrix_complex_band(order, n, n, kl, ku, ab, pdab, "Matrix A",
                                     0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_complex_band (x04dec).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\n");

  /* Compute row and column scaling factors using nag_lapacklin_zgbequ (f07btc).
   */
  nag_lapacklin_zgbequ(order, n, n, kl, ku, ab, pdab, r, c, &rowcnd, &colcnd,
                       &amax, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapacklin_zgbequ (f07btc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print rowcnd, colcnd, amax and the scale factors */
  printf("rowcnd = %10.1e, colcnd = %10.1e, amax = %10.1e\n\n", rowcnd, colcnd,
         amax);
  printf("Row scale factors\n");
  for (i = 1; i <= n; ++i)
    printf("%11.2e%s", r[i - 1], i % 7 == 0 ? "\n" : " ");

  printf("\n\nColumn scale factors\n");
  for (i = 1; i <= n; ++i)
    printf("%11.2e%s", c[i - 1], i % 7 == 0 ? "\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 (colcnd < 0.1) {
    scaled = Nag_TRUE;
    /* column scale A */
    for (j = 1; j <= n; ++j)
      for (i = MAX(1, j - ku); i <= MIN(n, j + kl); ++i) {
        AB(i, j).re *= c[j - 1];
        AB(i, j).im *= c[j - 1];
      }
  }
  if (rowcnd < 0.1 || amax < small || amax > big) {
    /* row scale A */
    scaled = Nag_TRUE;
    for (j = 1; j <= n; ++j)
      for (i = MAX(1, j - ku); i <= MIN(n, j + kl); ++i) {
        AB(i, j).re *= r[i - 1];
        AB(i, j).im *= r[i - 1];
      }
  }
  if (scaled) {
    /* Print the row and column scaled matrix using
     * nag_file_print_matrix_complex_band (x04dec).
     */
    fflush(stdout);
    nag_file_print_matrix_complex_band(order, n, n, kl, ku, ab, pdab,
                                       "Scaled matrix", 0, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_file_print_matrix_complex_band (x04dec).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  }
END:
  NAG_FREE(ab);
  NAG_FREE(c);
  NAG_FREE(r);

  return exit_status;
}

#undef AB