[R-sig-dyn-mod] deSolve with compiled code and varying size of parms

Rampal S. Etienne rampaletienne at gmail.com
Tue Mar 28 17:12:04 CEST 2017


Dear all,

I have been using the deSolve package with R functions, but now want to
speed up the calculations by using compiled C code. I read the deSolve
vignette on this, and it works when you specify the size of the parms
vector in the C-code. My questions is: how can I infer this size from
parms in the c-code rather than set it?

For example, in the code pasted below I must define lx to be 10 in the
second line. I would like lx to be inferred from either the size of
parms, or - if necessary - as an additional, first, element of the parms
vector. Interestingly, the derivs function does have an argument like
this, namely *neq, but initmod1 does not.

Kind regards,

Rampal Etienne

/* file mymod1.c */
#include <R.h>
#define lx 10
static double parms[lx * lx];

/* initializer */
void initmod1(void (* odeparms)(int *, double *))
{
   int N = lx * lx;
   odeparms(&N, parms);
}

/* Derivatives */
void derivs (int *neq, double *t, double *y, double *ydot, double *yout,
double *ip)
{
  int i, j;
  for(i = 0; i < *neq; i++) {
    ydot[i] = 0;
    for(j = 0; j < *neq; j++) {
      ydot[i] = ydot[i] + parms[i * (*neq) + j] * y[j];
    }
  }
}
/* END file mymod1.c */



More information about the R-sig-dynamic-models mailing list