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

NAG CL Interface Introduction
Example description
/* nag_opt_lsq_lincon_solve (e04ncc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 *
 *
 */

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

#define A(I, J) a[(I)*tda + J]
#define H(I, J) h[(I)*tdh + J]

int main(void) {
  Integer exit_status = 0;
  Integer i, j, *kx = 0, m, n, nbnd, nclin, tda, tdh;
  Nag_E04_Opt options;
  double *a = 0, *bl = 0, *bu = 0, *cvec = 0, *h = 0, objf, *x = 0;
  Nag_Comm comm;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_opt_lsq_lincon_solve (e04ncc) Example Program Results\n");
  fflush(stdout);
  scanf(" %*[^\n]"); /* Skip heading in data file */

  /* Read problem dimensions */
  scanf(" %*[^\n]");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &m, &n, &nclin);

  if (m > 0 && n > 0 && nclin >= 0) {
    nbnd = n + nclin;
    if (!(a = NAG_ALLOC(nclin * n, double)) ||
        !(bl = NAG_ALLOC(nbnd, double)) || !(bu = NAG_ALLOC(nbnd, double)) ||
        !(cvec = NAG_ALLOC(n, double)) || !(h = NAG_ALLOC(m * n, double)) ||
        !(x = NAG_ALLOC(n, double)) || !(kx = NAG_ALLOC(n, Integer))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    tda = n;
    tdh = n;
  } else {
    printf("Invalid m or n or nclin.\n");
    exit_status = 1;
    return exit_status;
  }

  /* We solve a QP2 type problem in this example */

  /* Read cvec, h, a, bl, bu and x from data file */
  scanf(" %*[^\n]");
  for (i = 0; i < m; ++i)
    scanf("%lf", &cvec[i]);
  scanf(" %*[^\n]");
  for (i = 0; i < m; ++i)
    for (j = 0; j < n; ++j)
      scanf("%lf", &H(i, j));

  if (nclin > 0) {
    scanf(" %*[^\n]");
    for (i = 0; i < nclin; ++i)
      for (j = 0; j < n; ++j)
        scanf("%lf", &A(i, j));
  }

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

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

    /* Read the initial point x */
  scanf(" %*[^\n]");
  for (i = 0; i < n; ++i)
    scanf("%lf", &x[i]);

  /* Change the problem type */
  /* nag_opt_init (e04xxc).
   * Initialization function for option setting
   */
  nag_opt_init(&options);
  options.prob = Nag_QP2;

  /* nag_opt_lsq_lincon_solve (e04ncc), see above. */
  nag_opt_lsq_lincon_solve(m, n, nclin, a, tda, bl, bu, cvec, (double *)0, h,
                           tdh, kx, x, &objf, &options, &comm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_opt_lsq_lincon_solve (e04ncc).\n%s\n", fail.message);
    exit_status = 1;
  }

  /* Free options memory */
  /* nag_opt_free (e04xzc).
   * Memory freeing function for use with option setting
   */
  nag_opt_free(&options, "all", &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_opt_free (e04xzc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

END:
  NAG_FREE(a);
  NAG_FREE(bl);
  NAG_FREE(bu);
  NAG_FREE(cvec);
  NAG_FREE(h);
  NAG_FREE(x);
  NAG_FREE(kx);

  return exit_status;
}