[R] Foreign code problem

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Oct 6 13:22:13 CEST 2004


On Wed, 6 Oct 2004, Rolf Wester wrote:

> Hello,
> 
> I wanted to test the odesolve package and tried to use compiled C-code.
> But when I do:
> 
> erg <- lsoda(y, times, "mond", parms, rtol, atol, tcrit=NULL, jacfunc=NULL,
>                verbose=FALSE, dllname="mond", hmin=0, hmax=Inf)
> 
> I get the error message:
> 
> Error in lsoda(y, times, "mond", parms, rtol, atol, tcrit = NULL,             
>                jacfunc = NULL,  : 
>     Unable to find mond in mond

Well, ?lsoda says

          If 'func' is a string, then 'dllname' must give the name of
          the shared library (without extension) which must be loaded
          before 'lsoda()' is called.

Have you loaded it (with dyn.load)?  The example in 
dynload/c/testdynload.R may help you.

> The C code is:
> 
> #include <math.h>
> 
> void mond(long int *neq, double *t, double *y, double *ydot)
> {
> 	double a = sqrt(y[2]*y[2] + y[4]*y[4]);
> 	a = -1.0/(a*a*a);
> 	ydot[0] = a*y[1];
> 	ydot[1] = y[0]; 
> 	ydot[2] = a*y[3];
> 	ydot[3] = y[2];
> }
> 
> and mond.so is build by:
> 
> gcc -O3 -shared mond.c -lm -o mond.so
> 
> I have set LD_LIBRARY_PATH to the correct directory and also tried to
> use the full path of mond.so in the call to lsoda but got the same error
> message. I would be very appreciative for help in order to understand
> how compiled code can be used from within R.


> Besides this I have a further question. Am I right that every function
> argument is copyied when calling a function, even if the argument is a
> huge matrix? Wouldn't that be very inefficient?

It would be.  It is marked for potential copying, but the copy only occurs 
when the potential copy is changed

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list