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

NAG CL Interface Introduction
Example description
/* nag_opt_handle_set_nlnls (e04rmc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */

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

#ifdef __cplusplus
extern "C" {
#endif
static void NAG_CALL objfun(Integer nvar, const double x[], Integer nres,
                            double rx[], Integer *inform, Nag_Comm *comm);
#ifdef __cplusplus
}
#endif

int main(void) {
  int nvar, nres, isparse, nnzrd;
  Integer icolrd[] = {1, 2, 3, 1, 2, 3}, irowrd[] = {1, 2, 1, 2, 1, 2};
  double x[] = {2.0, 2.0};
  double rinfo[100], stats[100];
  double *rx;
  void *handle;
  int exit_status = 0;

  /* Nag Types */
  Nag_Comm comm;
  NagError fail;

  printf("nag_opt_handle_set_nlnls (e04rmc) Example Program Results\n\n");
  fflush(stdout);

  /* Fill the problem data structure */
  nvar = 2;
  nres = 3;

  /* Allocate memory */
  if (!(rx = NAG_ALLOC(nres, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* nag_opt_handle_init (e04rac).
   * Initialize the handle
   */
  nag_opt_handle_init(&handle, nvar, NAGERR_DEFAULT);

  /* nag_opt_handle_set_nlnls (e04rmc)
   * Define residuals structure,
   */
  isparse = 1;
  nnzrd = 6;
  nag_opt_handle_set_nlnls(handle, nres, isparse, nnzrd, irowrd, icolrd,
                           NAGERR_DEFAULT);

  /* nag_opt_handle_opt_set (e04zmc)
   * Set options
   */
  /* Relax the main convergence criteria a bit */
  nag_opt_handle_opt_set(handle, "DFLS Trust Region Tolerance = 1.0e-3",
                         NAGERR_DEFAULT);
  /* Deactivate the slow convergence detection */
  nag_opt_handle_opt_set(handle, "DFLS Maximum slow steps = 0", NAGERR_DEFAULT);
  /* Turn off option printing */
  nag_opt_handle_opt_set(handle, "Print Options = NO", NAGERR_DEFAULT);
  /* Print the solution */
  nag_opt_handle_opt_set(handle, "Print Solution = YES", NAGERR_DEFAULT);
  /* Deactivate iteration log */
  nag_opt_handle_opt_set(handle, "Print Level = 1", NAGERR_DEFAULT);

  /* nag_opt_handle_solve_dfls (e04ffc)
   * Call the solver
   */
  INIT_FAIL(fail);
  nag_opt_handle_solve_dfls(handle, objfun, NULL, nvar, x, nres, rx, rinfo,
                            stats, &comm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_opt_handle_solve_dfls (e04ffc).\n%s\n",
           fail.message);
    exit_status = 1;
  }

END:
  /* Clean data */
  if (handle)
    /* nag_opt_handle_free (e04rzc).
     * Destroy the problem handle and deallocate all the memory used
     */
    nag_opt_handle_free(&handle, NAGERR_DEFAULT);
  NAG_FREE(rx);

  return exit_status;
}

static void NAG_CALL objfun(Integer nvar, const double x[], Integer nres,
                            double rx[], Integer *inform, Nag_Comm *comm) {
  /* Interrupt the solver if the dimensions does not correspond to the
   * problem
   */
  if (nvar != 2 || nres != 3) {
    *inform = -1;
    return;
  }

  rx[0] = x[0] + x[1] - 1.1;
  rx[1] = 2.0 * x[0] + x[1] - 1.9;
  rx[2] = 3.0 * x[0] + x[1] - 3.0;
}