/* nag_zero_nonlin_eqns_deriv_expert (c05rcc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 25, 2014.
 */

#include <nag.h>
#include <nagx04.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <math.h>
#include <nagc05.h>
#include <nagx02.h>

#ifdef __cplusplus
extern "C" {
#endif
static void NAG_CALL fcn(Integer n, const double x[], double fvec[],
                         double fjac[], Nag_Comm *comm, Integer *iflag);
#ifdef __cplusplus
}
#endif

static Integer nprint = 0;

int main(void)
{
  static double ruser[1] = {-1.0};
  Integer  exit_status = 0, i, n = 9, maxfev, nfev, njev;
  double   *diag = 0, *fjac = 0, *fvec = 0, *qtf = 0, *r = 0, *x = 0;
  double   factor, xtol;
  /* Nag Types */
  NagError fail;
  Nag_Comm comm;
  Nag_ScaleType scale_mode;

  INIT_FAIL(fail);

  printf("nag_zero_nonlin_eqns_deriv_expert (c05rcc) "
         "Example Program Results\n");

  /* For communication with user-supplied functions: */
  comm.user = ruser;

  if (n > 0)
    {
      if (!(diag = NAG_ALLOC(n, double)) ||
          !(fjac = NAG_ALLOC(n*n, double)) ||
          !(fvec = NAG_ALLOC(n, double)) ||
          !(qtf = NAG_ALLOC(n, double)) ||
          !(r = NAG_ALLOC(n*(n+1)/2, double)) ||
          !(x = NAG_ALLOC(n, double)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }
    }
  else
    {
      printf("Invalid n.\n");
      exit_status = 1;
      goto END;
    }

  /* The following starting values provide a rough solution. */
  for (i = 0; i < n; i++)
    x[i] = -1.0;

  /* nag_machine_precision (x02ajc).
   * The machine precision
   */
  xtol = sqrt(nag_machine_precision);

  for (i = 0; i < n; i++)
    diag[i] = 1.0;

  maxfev = 2000;
  scale_mode = Nag_ScaleProvided;
  factor = 100.0;

  /* nag_zero_nonlin_eqns_deriv_expert (c05rcc).
   * Solution of a system of nonlinear equations (function
   * values only)
   */
  nag_zero_nonlin_eqns_deriv_expert(fcn, n, x, fvec, fjac, xtol, maxfev,
                                    scale_mode, diag, factor, nprint, &nfev,
                                    &njev, r, qtf, &comm, &fail);

  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_zero_nonlin_eqns_deriv_expert (c05rcc).\n%s\n",
             fail.message);
      exit_status = 1;
      if (fail.code != NE_TOO_MANY_FEVALS &&
          fail.code != NE_TOO_SMALL &&
          fail.code != NE_NO_IMPROVEMENT)
        goto END;
    }

  printf(fail.code == NE_NOERROR ? "Final approximate" : "Approximate");
  printf(" solution\n\n");
  for (i = 0; i < n;  i++)
    printf("%12.4f%s", x[i], (i%3 == 2 || i == n-1)?"\n":" ");

  if (fail.code != NE_NOERROR)
    exit_status = 2;

 END:
  NAG_FREE(diag);
  NAG_FREE(fjac);
  NAG_FREE(fvec);
  NAG_FREE(qtf);
  NAG_FREE(r);
  NAG_FREE(x);
  return exit_status;
}
static void NAG_CALL fcn(Integer n, const double x[], double fvec[],
                         double fjac[], Nag_Comm *comm, Integer *iflag)
{
  Integer j, k;

  if (comm->user[0] == -1.0)
    {
      printf("(User-supplied callback fcn, first invocation.)\n");
      comm->user[0] = 0.0;
    }
  if (*iflag==0)
    {
      if (nprint>0)
        {
          /* Insert print statements here if desired. */
        }
    }
  else if (*iflag != 2)
    {
      for (k = 0; k < n; k++)
        {
          fvec[k] = (3.0-x[k]*2.0) * x[k] + 1.0;
          if (k > 0) fvec[k] -= x[k-1];
          if (k < n-1) fvec[k] -= x[k+1] * 2.0;
        }
    }
  else
    {
      for (k = 0; k < n; k++)
        {
          for (j = 0; j < n; j++)
            fjac[j*n + k] = 0.0;
          fjac[k*n + k] = 3.0 - x[k] * 4.0;
          if (k > 0)
            fjac[(k-1)*n + k] = -1.0;
          if (k < n-1)
            fjac[(k+1)*n + k] = -2.0;
        }
    }
  /* Set iflag negative to terminate execution for any reason. */
  *iflag = 0;
}