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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_dtgsyl (f08yhc) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.3, 2022.
 */

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

int main(void) {
  /* Scalars */
  double dif, scale;
  Integer i, ijob, j, m, n, pda, pdb, pdc, pdd, pde, pdf;
  Integer exit_status = 0;

  /* Arrays */
  double *a = 0, *b = 0, *c = 0, *d = 0, *e = 0, *f = 0;
  char nag_enum_arg[40];

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

  /* K(I,J) maps matrix element (I,J) to array storage element k */
#ifdef NAG_COLUMN_MAJOR
#define K(I, J, PD) (J - 1) * PD + I - 1
  order = Nag_ColMajor;
#else
#define K(I, J, PD) (I - 1) * PD + J - 1
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_dtgsyl (f08yhc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &m, &n);
  scanf("%" NAG_IFMT "%*[^\n]", &ijob);
  if (m < 0 || n < 0 || ijob < 0 || ijob > 4) {
    printf("Invalid m, n or ijob\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
   */
  trans = (Nag_TransType)nag_enum_name_to_value(nag_enum_arg);

  pda = m;
  pdb = n;
  pdd = m;
  pde = n;
#ifdef NAG_COLUMN_MAJOR
  pdc = m;
  pdf = m;
#else
  pdc = n;
  pdf = n;
#endif

  /* Allocate memory */
  if (!(a = NAG_ALLOC(m * m, double)) || !(b = NAG_ALLOC(n * n, double)) ||
      !(c = NAG_ALLOC(m * n, double)) || !(d = NAG_ALLOC(m * m, double)) ||
      !(e = NAG_ALLOC(n * n, double)) || !(f = NAG_ALLOC(m * n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read A, B, D, E, C and F from data file */
  for (i = 1; i <= m; ++i)
    for (j = 1; j <= m; ++j)
      scanf("%lf", &a[K(i, j, pda)]);
  scanf("%*[^\n]");
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf("%lf", &b[K(i, j, pdb)]);
  scanf("%*[^\n]");
  for (i = 1; i <= m; ++i)
    for (j = 1; j <= m; ++j)
      scanf("%lf", &d[K(i, j, pdd)]);
  scanf("%*[^\n]");
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf("%lf", &e[K(i, j, pde)]);
  scanf("%*[^\n]");
  for (i = 1; i <= m; ++i)
    for (j = 1; j <= n; ++j)
      scanf("%lf", &c[K(i, j, pdc)]);
  scanf("%*[^\n]");
  for (i = 1; i <= m; ++i)
    for (j = 1; j <= n; ++j)
      scanf("%lf", &f[K(i, j, pdf)]);
  scanf("%*[^\n]");

  /* Solve the Sylvester equations:
   * A*R - L*B = scale*C
   * D*R - L*E = scale*F
   *                     for R and L using  nag_lapackeig_dtgsyl (f08yhc).
   */
  nag_lapackeig_dtgsyl(order, trans, ijob, m, n, a, pda, b, pdb, c, pdc, d, pdd,
                       e, pde, f, pdf, &scale, &dif, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dtgsyl (f08yhc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print the solution matrices R and L */
  fflush(stdout);
  nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m,
                                 n, c, pdc, "Solution matrix R", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (x04cac).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\n");
  fflush(stdout);
  nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m,
                                 n, f, pdf, "Solution matrix L", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (x04cac).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\nscale = %11.2e\n", scale);

  if (ijob > 0 && scale > 0.0) {
    printf("\ndif   = %11.2e\n\n", dif);
    printf("This estimate of Dif((A,D),(B,E)) was computed based on the ");
    if (ijob == 1 || ijob == 3) {
      printf("Frobenius norm.\n");
    } else {
      printf("one norm.\n");
    }
  }

END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(c);
  NAG_FREE(d);
  NAG_FREE(e);
  NAG_FREE(f);

  return exit_status;
}