[R] use "integrate" for functions defined in C, not R

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue May 10 06:51:25 CEST 2005

On Mon, 9 May 2005, Zhijin Wu wrote:

> Dear all,
> I am trying to use the C code for "integrate" function ( that calls
> Rdqagi and Rdqags) so that I can integrate a function defined in C,
> instead of passing from R.
>  Is there a way for doing this?
> My unsuccessful attempt:
>  I looked into the files (including integrate.c, Applic.h) and
> 1. modified the definition of
> "integr_fn" by droping the environment "*ex",
>        void integr_fn(double *x, int n)
> and dropped all use of "ex" used in the code
> 2. defined my checker function f1 and the vectorizing function "Cintfn" in
> place of "Rintfn"
>          double f1(double x){ return(x);}
>          static void Cintfn(double *x, int n)
>           {
>           int i;
>           for(i = 0; i < n; i++)
>           x[i] = f1(x[i]);
>           return;
>           }
> 3. Similar to  "call_dqags", I define a C function "my_call_dqags" that
> has it's own parameters of "lower, upper"  and etc define in C, instead of
> parsing from R. And I call
>  Rdqags(Cintfn,
>         &lower, &upper, &epsabs, &epsrel, &result,
>         &abserr, &neval, &ier, &limit, &lenw, &last, iwork, work);
> instead of
>  Rdqags(Rintfn, (void*)&is,
>           &lower, &upper, &epsabs, &epsrel, &result,
>           &abserr, &neval, &ier, &limit, &lenw, &last, iwork, work);
> I am not passing (void*)&is because I no longer have the "environment".
> The code compiles fine with R CMD SHLIB. But it returns 5.3e-317 for my
> checker function f(x)=x, integration interval (1,2).
> Thanks for any hint!

1) This is the wrong list: please read the posting guide.

2) You cannot just leave out arguments in C calls, so it seems that you 
need help with C programming rather than R.  If you include the 
appropriate headers this will be checked, so I guess you have not.

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