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

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

int main(void)
{

  /* Scalars */
  double abserr, beta, y0, eps;
  Integer exit_status, i, j, l1, l2, lmax, n, pda, pdb, pdsigma;
  NagError fail;
  Nag_OrderType order;

  /* Arrays */
  double *a = 0, *b = 0, *c = 0, *ela = 0, *emu = 0, *rmom = 0;
  double *sigma = 0;

#ifdef NAG_COLUMN_MAJOR
#define A(I, J)     a[(J-1)*pda + I - 1]
#define B(I, J)     b[(J-1)*pdb + I - 1]
#define SIGMA(I, J) sigma[(J-1)*pdsigma + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J)     a[(I-1)*pda + J - 1]
#define B(I, J)     b[(I-1)*pdb + J - 1]
#define SIGMA(I, J) sigma[(I-1)*pdsigma + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_stat_moments_ratio_quad_forms (g01nbc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%lf%lf%*[^\n] ", &beta, &y0);
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &n, &l1, &l2);

  /* Allocate memory */
  if (!(a = NAG_ALLOC(n * n, double)) ||
      !(b = NAG_ALLOC(n * n, double)) ||
      !(c = NAG_ALLOC(n * n, double)) ||
      !(ela = NAG_ALLOC(n, double)) ||
      !(emu = NAG_ALLOC(n, double)) ||
      !(rmom = NAG_ALLOC(l2 - l1 + 1, double)) ||
      !(sigma = NAG_ALLOC(n * n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  pda = n;
  pdb = n;
  pdsigma = n;

  /* Compute A, EMU, and SIGMA for simple autoregression */

  for (i = 1; i <= n; ++i) {
    for (j = i; j <= n; ++j) {
      A(j, i) = 0.0;
      B(j, i) = 0.0;
    }
  }
  for (i = 1; i <= n - 1; ++i) {
    A(i + 1, i) = 0.5;
    B(i, i) = 1.0;
  }
  emu[0] = y0 * beta;
  for (i = 1; i <= n - 1; ++i)
    emu[i] = beta * emu[i - 1];
  SIGMA(1, 1) = 1.0;
  for (i = 2; i <= n; ++i)
    SIGMA(i, i) = beta * beta * SIGMA(i - 1, i - 1) + 1.0;
  for (i = 1; i <= n; ++i) {
    for (j = i + 1; j <= n; ++j)
      SIGMA(j, i) = beta * SIGMA(j - 1, i);
  }

  eps = 0.0;
  /* nag_stat_moments_ratio_quad_forms (g01nbc).
   * Moments of ratios of quadratic forms in Normal variables,
   * and related statistics
   */
  nag_stat_moments_ratio_quad_forms(order, Nag_RatioMoments, Nag_MeanInclude, n,
                               a, n, b, n, c, n, ela, emu, sigma, n, l1, l2,
                               &lmax, rmom, &abserr, eps, &fail);

  if (fail.code == NE_NOERROR || fail.code == NE_SOME_MOMENTS
      || fail.code == NE_ACCURACY) {
    printf("\n");
    printf(" n = %3" NAG_IFMT " beta = %6.3f y0 = %6.3f\n", n, beta, y0);
    printf("\n");
    printf("      Moments\n");
    printf("\n");

    j = 0;
    for (i = l1; i <= lmax; ++i) {
      ++j;
      printf("%3" NAG_IFMT "%12.3e\n", i, rmom[j - 1]);
    }
  }
  else {
    printf("Error from nag_stat_moments_ratio_quad_forms (g01nbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(c);
  NAG_FREE(ela);
  NAG_FREE(emu);
  NAG_FREE(rmom);
  NAG_FREE(sigma);

  return exit_status;
}