/* nag_robust_m_regsn_param_var (g02hfc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

#include <math.h>
#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg02.h>

#ifdef __cplusplus
extern "C"
{
#endif
  static double NAG_CALL psi(double t, Nag_Comm *comm);
  static double NAG_CALL psp(double t, Nag_Comm *comm);
#ifdef __cplusplus
}
#endif

int main(void)
{

  /* Scalars */
  double sigma;
  Integer exit_status, i, j, k, m, n;
  Integer pdc, pdx;
  NagError fail;
  Nag_OrderType order;
  Nag_Comm comm;

  /* Arrays */
  static double ruser[2] = { -1.0, -1.0 };
  double *cov = 0, *rs = 0, *wgt = 0, *comm_arr = 0, *x = 0;

#ifdef NAG_COLUMN_MAJOR
#define COV(I, J) cov[(J-1)*pdc + I - 1]
#define X(I, J)   x[(J-1)*pdx + I - 1]
  order = Nag_ColMajor;
#else
#define COV(I, J) cov[(I-1)*pdc + J - 1]
#define X(I, J)   x[(I-1)*pdx + J - 1]
  order = Nag_RowMajor;
#endif
  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_robust_m_regsn_param_var (g02hfc) Example Program Results\n");

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

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Read in the dimensions of X */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &n, &m);

  /* Allocate memory */
  if (!(cov = NAG_ALLOC(m * m, double)) ||
      !(rs = NAG_ALLOC(n, double)) ||
      !(wgt = NAG_ALLOC(n, double)) ||
      !(comm_arr = NAG_ALLOC(m * (n + m + 1) + 2 * n, double)) ||
      !(x = NAG_ALLOC(n * m, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

#ifdef NAG_COLUMN_MAJOR
  pdc = m;
  pdx = n;
#else
  pdc = m;
  pdx = m;
#endif

  printf("\n");

  /* Read in the X matrix */
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= m; ++j) {
      scanf("%lf", &X(i, j));
    }
    scanf("%*[^\n] ");
  }

  /* Read in sigma */
  scanf("%lf%*[^\n] ", &sigma);

  /* Read in weights and residuals */
  for (i = 1; i <= n; ++i) {
    scanf("%lf%lf%*[^\n] ", &wgt[i - 1], &rs[i - 1]);
  }

  /* Set parameters for Schweppe type regression */
  /* nag_robust_m_regsn_param_var (g02hfc).
   * Robust regression, variance-covariance matrix following
   * nag_robust_m_regsn_user_fn (g02hdc)
   */
  nag_robust_m_regsn_param_var(order, psi, psp, Nag_SchweppeReg,
                               Nag_CovMatAve, sigma, n, m, x, pdx, rs, wgt,
                               cov, pdc, comm_arr, &comm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_robust_m_regsn_param_var (g02hfc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  printf("Covariance matrix\n");
  for (j = 1; j <= m; ++j) {
    for (k = 1; k <= m; ++k) {
      printf("%10.4f%s", COV(j, k), k % 6 == 0 || k == m ? "\n" : " ");
    }
  }

END:
  NAG_FREE(cov);
  NAG_FREE(rs);
  NAG_FREE(wgt);
  NAG_FREE(comm_arr);
  NAG_FREE(x);

  return exit_status;
}

static double NAG_CALL psi(double t, Nag_Comm *comm)
{
  double ret_val;

  if (comm->user[0] == -1.0) {
    printf("(User-supplied callback psi, first invocation.)\n");
    comm->user[0] = 0.0;
  }

  if (t <= -1.5) {
    ret_val = -1.5;
  }
  else if (fabs(t) < 1.5) {
    ret_val = t;
  }
  else {
    ret_val = 1.5;
  }
  return ret_val;
}

static double NAG_CALL psp(double t, Nag_Comm *comm)
{
  double ret_val;

  if (comm->user[1] == -1.0) {
    printf("(User-supplied callback psp, first invocation.)\n");
    comm->user[1] = 0.0;
  }

  ret_val = 0.0;
  if (fabs(t) < 1.5) {
    ret_val = 1.0;
  }
  return ret_val;
}