/*
	Vegas.c
		Vegas Monte-Carlo integration
		by Thomas Hahn
		last modified 15 Feb 11 th
*/


#include "decl.h"

/*#define Print(s) puts(s); fflush(stdout)*/
#define Print(s) mexPrintf(s);

/*********************************************************************/

static inline void DoSample(This *t, number n,
  creal *w, creal *x, real *f, cint iter)
{  
  t->neval += n;

  /* lines adopted to allow MATLAB vector calls
  while( n-- ) {
    if( t->integrand(&t->ndim, x, &t->ncomp, f, t->userdata,
          w++, &iter) == ABORT )
      longjmp(t->abort, -99);
    x += t->ndim;
    f += t->ncomp;
  }
  */
  char s[256];
  if( t->batchmode(1, t->userdata) == 0) {
    /* no batchmode: use original code */
    if( VERBOSE ) {
      sprintf(s, "\nEntered DoSample with %i evaluations (no batch).", n);
      Print(s);
    }
    while( n-- ) {
      if( t->integrand(&t->ndim, x, &t->ncomp, f, t->userdata,
            w++, &iter) == ABORT )
        longjmp(t->abort, -99);
      x += t->ndim;
      f += t->ncomp;
    }
  } else {
    if( VERBOSE ) {
    sprintf(s, "\nEntered DoSample with %i evaluations (batch).", n);
    Print(s);
    }
    /* enable batchmode: single integrand call */
    t->batchmode(n, t->userdata);
    t->integrand(&t->ndim, x, &t->ncomp, f, t->userdata,
            w++, &iter);
    n--;
    while( n-- ) w++;
  }
}

/*********************************************************************/

#include "common.c"

/* argumend appended for vectorized MATLAB coupling */
Extern void EXPORT(Vegas)(ccount ndim, ccount ncomp,
  Integrand integrand, void *userdata,
  creal epsrel, creal epsabs, cint flags, cint seed,
  cnumber mineval, cnumber maxeval,
  cnumber nstart, cnumber nincrease, cnumber nbatch,
  cint gridno, cchar *statefile,
  number *pneval, int *pfail,
  real *integral, real *error, real *prob, BatchModeSwitch batchmode)
{
  This t;
  t.ndim = ndim;
  t.ncomp = ncomp;
  t.integrand = integrand;
  t.userdata = userdata;
  t.epsrel = epsrel;
  t.epsabs = epsabs;
  t.flags = flags;
  t.seed = seed;
  t.mineval = mineval;
  t.maxeval = maxeval;
  t.nstart = nstart;
  t.nincrease = nincrease;
  t.nbatch = nbatch;
  t.gridno = gridno;
  t.statefile = statefile;
  t.neval = 0;
  t.batchmode = batchmode;

  *pfail = Integrate(&t, integral, error, prob);
  *pneval = t.neval;
}

/*********************************************************************/

/* argumend appended for vectorized MATLAB coupling */
Extern void EXPORT(vegas)(ccount *pndim, ccount *pncomp,
  Integrand integrand, void *userdata,
  creal *pepsrel, creal *pepsabs, cint *pflags, cint *pseed,
  cnumber *pmineval, cnumber *pmaxeval,
  cnumber *pnstart, cnumber *pnincrease, 
  cnumber *pnbatch, cint *pgridno, cchar *statefile,
  number *pneval, int *pfail,
  real *integral, real *error, real *prob, int statefilelen, BatchModeSwitch batchmode)
{
  char *s = NULL;
  This t;
  t.ndim = *pndim;
  t.ncomp = *pncomp;
  t.integrand = integrand;
  t.userdata = userdata;
  t.epsrel = *pepsrel;
  t.epsabs = *pepsabs;
  t.flags = *pflags;
  t.seed = *pseed;
  t.mineval = *pmineval;
  t.maxeval = *pmaxeval;
  t.nstart = *pnstart;
  t.nincrease = *pnincrease;
  t.nbatch = *pnbatch;
  t.gridno = *pgridno;
  t.neval = 0;

  if( statefile ) {
	/* strip trailing spaces */
    while( statefilelen > 0 && statefile[statefilelen - 1] == ' ' )
      --statefilelen;
    if( statefilelen > 0 && (s = malloc(statefilelen + 1)) ) {
      memcpy(s, statefile, statefilelen);
      s[statefilelen] = 0;
    }
  }
  t.statefile = s;

  *pfail = Integrate(&t, integral, error, prob);
  *pneval = t.neval;

  free(s);
}

