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

Daniel Kaschek daniel.kaschek at physik.uni-freiburg.de
Tue Mar 28 22:14:38 CEST 2017


Dear Rampal,

I am not sure whether it is possible to do what you want. At least not
if you rely on the odeparms function. Perhaps Thomas and Karline will
know better.

You could write a small R function based on sink() to generate your C
code with the correct value for lx "on the fly". This would be a
compromise.

By the way, there are some packages around working with deSolve and
autogenerating C code or FORTRAN code, e.g., cOde and rodeo.

Best regards,
Daniel


On Tue, 2017-03-28 at 17:12 +0200, Rampal S. Etienne wrote:
> 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 */
> 
> _______________________________________________
> R-sig-dynamic-models mailing list
> R-sig-dynamic-models at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-dynamic-models



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