Example description
/* nag_opt_bnd_lin_lsq (e04pcc) Example Program.
 *
 * Copyright 2019 Numerical Algorithms Group.
 *
 * Mark 27.0, 2019.
 */

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

#define A(I,J) a[(J)*pda + I]

int main(void)
{
  Integer exit_status = 0;
  double tol = 0.0;
  Nag_RegularizedType itype = Nag_NotRegularized;
  double rnorm;
  Integer i, j, m, n, nfree, pda;
  double *a = 0, *b = 0, *bl = 0, *bu = 0, *w = 0, *x = 0;
  Integer *indx = 0;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_opt_bnd_lin_lsq (e04pcc) Example Program Results\n\n");
  scanf("%*[^\n] "); /* Skip heading in data file */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &m, &n);

  if (m < 0 || n < 0) {
    printf("Invalid m or n.\n");
    exit_status = 1;
    goto END;
  }

  pda = m;

  if (!(a = NAG_ALLOC(pda * n, double)) ||
      !(b = NAG_ALLOC(m, double)) ||
      !(w = NAG_ALLOC(n, double)) ||
      !(bl = NAG_ALLOC(n, double)) ||
      !(bu = NAG_ALLOC(n, double)) ||
      !(x = NAG_ALLOC(n, double)) || !(indx = NAG_ALLOC(n, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read the matrix A */
  for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
      scanf("%lf", &A(i, j));
  scanf("%*[^\n] "); /* Remove remainder of line */

  /* Read the right-hand side vector b */
  for (j = 0; j < m; j++)
    scanf("%lf", &b[j]);
  scanf("%*[^\n] ");

  /* Read the lower bounds vector bl */
  for (i = 0; i < n; i++)
    scanf("%lf", &bl[i]);
  scanf("%*[^\n] ");

  /* Read the upper bounds vector bu */
  for (i = 0; i < n; i++)
    scanf("%lf", &bu[i]);
  scanf("%*[^\n] ");

  /* nag_opt_bnd_lin_lsq (e04pcc). Computes the least squares solution
     to a set of linear equations subject to fixed upper and lower
     bounds on the variables */
  nag_opt_bnd_lin_lsq(itype, m, n, a, pda, b, bl, bu, tol, x, &rnorm, &nfree,
                      w, indx, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_opt_bnd_lin_lsq (e04pcc).\n%s\n", fail.message);
    exit_status = 2;
    goto END;
  }

  printf("Solution vector\n");
  for (i = 0; i < n; i++)
    printf("%9.4f", x[i]);
  printf("\n\n");

  printf("Dual Solution\n");
  for (i = 0; i < n; i++)
    printf("%9.4f", w[i]);
  printf("\n\n");

  printf("Residual %9.4f\n", rnorm);

END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(bl);
  NAG_FREE(bu);
  NAG_FREE(w);
  NAG_FREE(x);
  NAG_FREE(indx);

  return exit_status;
}