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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_zgeqlf (f08csc) Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.3, 2021.
 */

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

int main(void) {
  /* Scalars */
  Integer i, j, m, n, nrhs, pda, pdb;
  Integer exit_status = 0;
  /* Arrays */
  Complex *a = 0, *b = 0, *tau = 0;
  double *rnorm = 0;
  /* Nag Types */
  Nag_OrderType order;
  NagError fail;

#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
#define B(I, J) b[(J - 1) * pdb + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I - 1) * pda + J - 1]
#define B(I, J) b[(I - 1) * pdb + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_zgeqlf (f08csc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &m, &n, &nrhs);

#ifdef NAG_COLUMN_MAJOR
  pda = m;
  pdb = m;
#else
  pda = n;
  pdb = nrhs;
#endif

  /* Allocate memory */
  if (!(a = NAG_ALLOC(m * n, Complex)) || !(b = NAG_ALLOC(m * nrhs, Complex)) ||
      !(tau = NAG_ALLOC(n, Complex)) || !(rnorm = NAG_ALLOC(nrhs, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read A and B from data file */
  for (i = 1; i <= m; ++i)
    for (j = 1; j <= n; ++j)
      scanf(" ( %lf , %lf )", &A(i, j).re, &A(i, j).im);
  scanf("%*[^\n]");

  for (i = 1; i <= m; ++i)
    for (j = 1; j <= nrhs; ++j)
      scanf(" ( %lf , %lf )", &B(i, j).re, &B(i, j).im);
  scanf("%*[^\n]");

  /* nag_lapackeig_zgeqlf (f08csc).
   * Compute the QL factorization of A.
   */
  nag_lapackeig_zgeqlf(order, m, n, a, pda, tau, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zgeqlf (f08csc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_lapackeig_zunmql (f08cuc).
   * Compute C = (C1) = (Q^H)*B, storing the result in B.
   *             (C2)
   */
  nag_lapackeig_zunmql(order, Nag_LeftSide, Nag_ConjTrans, m, nrhs, n, a, pda,
                       tau, b, pdb, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zunmql (f08cuc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_lapacklin_ztrtrs (f07tsc).
   * Compute least squares solutions by back-substitution in
   * L*X = C2.
   */
  nag_lapacklin_ztrtrs(order, Nag_Lower, Nag_NoTrans, Nag_NonUnitDiag, n, nrhs,
                       &A(m - n + 1, 1), pda, &B(m - n + 1, 1), pdb, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapacklin_ztrtrs (f07tsc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_file_print_matrix_complex_gen_comp (x04dbc).
   * Print least squares solution(s).
   */
  fflush(stdout);
  nag_file_print_matrix_complex_gen_comp(
      order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, nrhs, &B(m - n + 1, 1), pdb,
      Nag_BracketForm, "%7.4f", "Least squares solution(s)", Nag_IntegerLabels,
      0, Nag_IntegerLabels, 0, 80, 0, 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_complex_gen_comp (x04dbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_blast_zge_norm (f16uac).
   * Compute and print estimates of the square roots of the residual
   * sums of squares.
   */
  for (j = 1; j <= nrhs; ++j) {
    nag_blast_zge_norm(order, Nag_FrobeniusNorm, m - n, 1, &B(1, j), pdb,
                       &rnorm[j - 1], &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_blast_zge_norm (f16uac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
  }

  printf("\nSquare root(s) of the residual sum(s) of squares\n");
  for (j = 0; j < nrhs; ++j)
    printf("%11.2e%s", rnorm[j],
           (j + 1) % 7 == 0 || j == nrhs - 1 ? "\n" : " ");

END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(tau);
  NAG_FREE(rnorm);

  return exit_status;
}

#undef A
#undef B