Example description
/* nag_lapackeig_dgeqrt (f08abc) Example Program.
 *
 * Copyright 2020 Numerical Algorithms Group.
 *
 * Mark 27.1, 2020.
 */

#include <nag.h>

int main(void) {
  /* Scalars */
  double rnorm;
  Integer exit_status = 0;
  Integer pda, pdb, pdt;
  Integer i, j, m, n, nb, nrhs;
  /* Arrays */
  double *a = 0, *b = 0, *t = 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_dgeqrt (f08abc) Example Program Results\n\n");
  fflush(stdout);

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &m, &n, &nrhs);
  nb = MIN(m, n);
  if (!(a = NAG_ALLOC(m * n, double)) || !(b = NAG_ALLOC(m * nrhs, double)) ||
      !(t = NAG_ALLOC(nb * MIN(m, n), double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
#ifdef NAG_COLUMN_MAJOR
  pda = m;
  pdb = m;
  pdt = nb;
#else
  pda = n;
  pdb = nrhs;
  pdt = MIN(m, n);
#endif

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

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

  /* nag_lapackeig_dgeqrt (f08abc).
   * Compute the QR factorization of A by recursive algorithm.
   */
  nag_lapackeig_dgeqrt(order, m, n, nb, a, pda, t, pdt, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dgeqrt (f08abc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_lapackeig_dgemqrt (f08acc).
   * Compute C = (C1) = (Q^T)*B, storing the result in B
   *             (C2)
   * by applying Q^T from left.
   */
  nag_lapackeig_dgemqrt(order, Nag_LeftSide, Nag_Trans, m, nrhs, n, nb, a, pda,
                        t, pdt, b, pdb, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dgemqrt (f08acc).\n%s\n", fail.message);
    exit_status = 2;
    goto END;
  }

  /* nag_lapacklin_dtrtrs (f07tec).
   * Compute least squares solutions by back-substitution in R*X = C1.
   */
  nag_lapacklin_dtrtrs(order, Nag_Upper, Nag_NoTrans, Nag_NonUnitDiag, n, nrhs,
                       a, pda, b, pdb, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapacklin_dtrtrs (f07tec).\n%s\n", fail.message);
    exit_status = 3;
    goto END;
  }

  /* nag_file_print_matrix_real_gen (x04cac).
   * Print least squares solutions.
   */
  nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
                                 nrhs, b, pdb, "Least squares solution(s)", 0,
                                 &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (x04cac).\n%s\n",
           fail.message);
    exit_status = 4;
    goto END;
  }

  printf("\n Square root(s) of the residual sum(s) of squares\n");
  for (j = 1; j <= nrhs; j++) {
    /* nag_blast_dge_norm (f16rac).
     * Compute and print estimate of the square root of the residual
     * sum of squares.
     */
    nag_blast_dge_norm(order, Nag_FrobeniusNorm, m - n, 1, &B(n + 1, j), pdb,
                       &rnorm, &fail);
    if (fail.code != NE_NOERROR) {
      printf("\nError from nag_blast_dge_norm (f16rac).\n%s\n", fail.message);
      exit_status = 5;
      goto END;
    }
    printf("  %11.2e ", rnorm);
  }
  printf("\n");

END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(t);

  return exit_status;
}